Es學習筆記

1、全文搜索(一種傳統數據庫很難實現的功能)數據庫

咱們將會搜索全部喜歡「rock climbing」的員工:
GET /megacorp/employee/_search
{
    "query" : {
        "match" : {
            "about" : "rock climbing"
        }
    }
}

你能夠看到咱們使用了以前的match查詢,從about字段中搜索"rock climbing",咱們獲得了兩個匹配文檔:json

{
   ...
   "hits": {
      "total":      2,
      "max_score":  0.16273327,
      "hits": [
         {
            ...
            "_score":         0.16273327, <1>
            "_source": {
               "first_name":  "John",
               "last_name":   "Smith",
               "age":         25,
               "about":       "I love to go rock climbing",
               "interests": [ "sports", "music" ]
            }
         },
         {
            ...
            "_score":         0.016878016, <2>
            "_source": {
               "first_name":  "Jane",
               "last_name":   "Smith",
               "age":         32,
               "about":       "I like to collect rock albums",
               "interests": [ "music" ]
            }
         }
      ]
   }
}

默認狀況下,Elasticsearch根據結果相關性評分來對結果集進行排序, 結果相關性評分就是文檔與查詢條件的匹配程度。設計

可是爲何Jane Smith也會出如今結果裏呢?緣由是「rock」在她的abuot字段中被說起了。由於只有「rock」被說起而「climbing」沒有,因此她的_score要低於John。rest

2、短語搜索code

全文搜索容許咱們在字段中搜索單獨的一個詞,這挺好的,可是有時候你想要確切的匹配若干個單詞或者短語(phrases)。例如咱們想要查詢同時包含"rock"和"climbing"(而且是相鄰的)的員工記錄。orm

GET /megacorp/employee/_search
{
    "query" : {
        "match_phrase" : {
            "about" : "rock climbing"
        }
    }
}

毫無疑問,該查詢返回John Smith的文檔:排序

{
   ...
   "hits": {
      "total":      1,
      "max_score":  0.23013961,
      "hits": [
         {
            ...
            "_score":         0.23013961,
            "_source": {
               "first_name":  "John",
               "last_name":   "Smith",
               "age":         25,
               "about":       "I love to go rock climbing",
               "interests": [ "sports", "music" ]
            }
         }
      ]
   }
}

 

3、查詢高亮顯示文檔

Elasticsearch中高亮片斷是很是容易的,讓咱們在以前的語句上增長highlight參數:it

{
  "query": {
    "match_phrase": {
      "company_name": "苗會"
    }
  },
  "highlight": {
    "fields": {
      "company_name": {}
    }
  }
}

當咱們運行這個語句時,會命中與以前相同的結果,可是在返回結果中會有一個新的部分叫作highlight,而且用<em></em>來標識匹配到的單詞。ast

{
"_index": "miaotu_formal",
"_type": "company_info_and_type",
"_id": "234",
"_score": 3.3942661,
"_source": {
"company_name": "中苗會",
"short_name": "中苗會",
"type_name": "景觀設計"
},
"highlight": {
"company_name": [
"中<em>苗</em><em>會</em>"
]
}
}
相關文章
相關標籤/搜索