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:查询的结果包含哪些字段,指的是元数据

作者:http://cnblogs.com/lyc-code/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权力。

原文地址:https://www.cnblogs.com/lyc-code/p/15490789.html