天天看點

4-檢索、排序、分頁、高亮、multi_match

一、相關度評分

1、當沒有指定評分規則時,會依據相關度分數進行排序。一旦指定了排序規則,就不會計算相關度評分,而按照指定指定排序順序進行顯示

2、相關度評分規則

  ①詞頻:關鍵詞在每個doc中出現的次數。越高相關度分數越高

  ②反詞頻:關鍵詞在整個索引中出現的次數。反詞頻越高,相關度分數越低

  ③每個doc長度越長,相關度越低

二、中繼資料 _source

1、相當于sql語句中要顯示的字段,如select name from table where。。。  name就是中繼資料

2、用法

1 GET index/_search
 2 {
 3   "_source": {
 4     "includes": ["",""],
 5     "excludes": ["",""]
 6   }
 7 }或
 8 GET index/_search
 9 {
10   "_source": false,
11   "query": {}
12 }      

三、全文檢索

1、match:match是會被分詞的

2、match_all:查詢所有資料,會被分詞。相當于select * from table;

3、multi_match:

4、match_phrase:會被分詞

  被檢索字段必須包含match_phrase中的所有詞項并且順序必須相同

  被檢索字段包含的match_phrase中的詞項之間不能有其他問題

在考試時要注意題目中是否有phrase關鍵字,如果有就要用match_phrase

四、分頁、排序

從第幾條開始,每次查詢多少條

按照什麼順序進行排序

1 GET index/_search
 2 {
 3   "from": 20,
 4   "size": 20,
 5   "query": {},
 6   "sort": [
 7     {
 8       "price": {
 9         "order": "desc"
10       }
11     },
12     {
13       "_score": {
14         "order": "asc"
15       }
16     }
17   ]
18 }      

五、高亮

1 GET index/_search
 2 {
 3   "query": {
 4     "term": {
 5       "name": {
 6         "value": "xiaomi"
 7       }
 8     }
 9   },
10   "highlight": {
11     "fields": {
12       "name": {
13         "pre_tags": [
14           "<b>"
15         ],
16         "post_tags": [
17           "</b>"
18         ]
19       }
20     }
21   }
22 }隻針對name字段進行高亮,用<b></b>标簽進行包裝      
1 GET index/_search
 2 {
 3   "query": {
 4     "term": {
 5       "name": {
 6         "value": "xiaomi"
 7       }
 8     }
 9   },
10   "highlight": {
11     "pre_tags": [
12       "<b>"
13     ],
14     "post_tags": [
15       "</b>"
16     ],
17     "fields": {
18       "name": {}
19     }
20   }
21 }對所有字段進行包裝      

六、精準查詢

1、term和keyword的差別

  term搜尋不會将搜尋詞進行分詞

  keyword在中繼資料建立索引時不會進行分詞

2、match_phrase:會被分詞,順序必須相同且中間不能有其他字元

3、範圍查詢

1 GET index/_search
 2 {
 3   "query": {
 4     "range": {
 5       "price": {
 6         "gte": 10,
 7         "lte": 20
 8       }
 9     }
10   }
11 }gt:大于 lt:小于 gte:大于等于 lte:小于等于      

七、組合查詢

1、must相當于and

2、should相當于or

3、must_not相當于!and

4、filter相當于match,與match不同的是filter不會使用相關度評分進行排序

注意:當must與should需要同時滿足時,以must為主。因為should有一個minimum_should_match參數進行控制

  在僅使用should時,minimum_should_match為1,代表必須查詢出一條結果

  當should與must或filter聯合使用時,minimum_should_match會自動變為0,表示滿足must時就會查出資料

1 GET index/_search
 2 {
 3   "query": {
 4     "bool": {
 5       "must": [
 6         {
 7           "match_phrase": {
 8             "name": "chiji shouji"
 9           }
10         }
11       ],
12       "should": [
13         {
14           "term": {
15             "price": {
16               "value": 3999
17             }
18           }
19         }
20       ],
21       "minimum_should_match": 1
22     }
23   }
24 }
25 若must條件符合,should條件不符合,minimum_should_match設定為1查不出來結果
26 若must條件符合,should條件不符合,minimum_should_match不設定可以查出來結果,不設定時預設值為0      

八、multi_match

1、multi_match:從哪些字段中檢索,指的是查詢條件

   _source:查詢的結果包含哪些字段,指的是中繼資料