ElasticSearch高级查询
总阅读次
ElasticSearch高级查询
高级查询包含:子条件查询、复合条件查询
运行环境
|
|
准备测试数据
向ES添加如下数据
子条件查询
特定字段查询所指特定值,子查询又分为:Query Context、Filter Context
Query Context
概念:在查询过程中,除了判断文档是否满足查询条件外,ES还会计算一个_score
来标识匹配的程度,旨在判断目标文档和查询条件匹配的程度。
Query Context有如下的常用查询:
全文本查询 针对文本类型查询
模糊查询
请求地址:
http://192.168.137.8:9200/book/_search
请求方式:
POST
请求数据:
1234567{"query":{"match":{ //模糊匹配关键字"title":"ElasticSearch" //匹配条件 查询title包含ElasticSearch}}}响应结果,
title
字段中包含ElasticSearch
的全部查询出来了:123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263{"took": 58,"timed_out": false,"_shards": {"total": 3,"successful": 3,"failed": 0},"hits": {"total": 4,"max_score": 1.3555917,"hits": [{"_index": "book","_type": "novel","_id": "4","_score": 1.3555917,"_source": {"author": "李四","title": "ElasticSearch大法好","wordcount": 1000,"publish_date": "2010-10-01"}},{"_index": "book","_type": "novel","_id": "9","_score": 1.1001158,"_source": {"author": "很胖的瓦力","title": "ElasticSearch精通","wordcount": 3000,"publish_date": "2017-08-15"}},{"_index": "book","_type": "novel","_id": "8","_score": 0.19856805,"_source": {"author": "瓦力","title": "ElasticSearch入门","wordcount": 3000,"publish_date": "2017-08-20"}},{"_index": "book","_type": "novel","_id": "12","_score": 0.155468,"_source": {"title": "瓦力教我们学ElasticSearch","author": "瓦力","wordcount": 1000,"publish_date": "2017-08-01"}}]}}习语匹配查询
请求地址和请求方式不变,请求参数修改如下:
1234567{"query":{"match_phrase":{ //习语匹配关键字"title":"ElasticSearch入门" //查询条件}}}响应结果如下,只有
title
字段包含ElasticSearch入门:123456789101112131415161718192021222324252627{"took": 36,"timed_out": false,"_shards": {"total": 3,"successful": 3,"failed": 0},"hits": {"total": 1,"max_score": 0.7594808,"hits": [{"_index": "book","_type": "novel","_id": "8","_score": 0.7594808,"_source": {"author": "瓦力","title": "ElasticSearch入门","wordcount": 3000,"publish_date": "2017-08-20"}}]}}通过下面的例子可以更好的理解习语匹配:
请求地址和请求方式不变,请求数据如下:
1234567{"query":{"match":{ //模糊匹配"title":"ElasticSearch入门"}}}响应结果如下,ES将查询条件
ElasticSearch入门
拆分为两个关键字ElasticSearch
和入门
,然后匹配所有title包含这些关键字的记录:123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687{"took": 44,"timed_out": false,"_shards": {"total": 3,"successful": 3,"failed": 0},"hits": {"total": 6,"max_score": 2.7111833,"hits": [{"_index": "book","_type": "novel","_id": "2","_score": 2.7111833,"_source": {"author": "王五","title": "Java入门","wordcount": 2000,"publish_date": "2010-10-01"}},{"_index": "book","_type": "novel","_id": "3","_score": 2.2002316,"_source": {"author": "张三","title": "Python入门","wordcount": 2000,"publish_date": "2005-10-01"}},{"_index": "book","_type": "novel","_id": "8","_score": 1.7083936,"_source": {"author": "瓦力","title": "ElasticSearch入门","wordcount": 3000,"publish_date": "2017-08-20"}},{"_index": "book","_type": "novel","_id": "4","_score": 1.3555917,"_source": {"author": "李四","title": "ElasticSearch大法好","wordcount": 1000,"publish_date": "2010-10-01"}},{"_index": "book","_type": "novel","_id": "9","_score": 1.1001158,"_source": {"author": "很胖的瓦力","title": "ElasticSearch精通","wordcount": 3000,"publish_date": "2017-08-15"}},{"_index": "book","_type": "novel","_id": "12","_score": 0.155468,"_source": {"title": "瓦力教我们学ElasticSearch","author": "瓦力","wordcount": 1000,"publish_date": "2017-08-01"}}]}}多字段查询
请求地址和请求方式不变,请求数据如下:
12345678{"query":{"multi_match":{ //多字段查询关键字"query":"瓦力", //查询条件"fields":["author","title"] //查询字段}}}响应结果如下,
author
字段等于瓦力
,title
字段包含瓦力
:123456789101112131415161718192021222324252627282930313233343536373839{"took": 132,"timed_out": false,"_shards": {"total": 3,"successful": 3,"failed": 0},"hits": {"total": 2,"max_score": 1.1821114,"hits": [{"_index": "book","_type": "novel","_id": "12","_score": 1.1821114,"_source": {"title": "瓦力教我们学ElasticSearch","author": "瓦力","wordcount": 1000,"publish_date": "2017-08-01"}},{"_index": "book","_type": "novel","_id": "8","_score": 0.18232156,"_source": {"author": "瓦力","title": "ElasticSearch入门","wordcount": 3000,"publish_date": "2017-08-20"}}]}}语法查询
请求地址和请求方式不变,请求数据如下:
1234567{"query":{"query_string":{ //语法查询关键字"query":"ElasticSearch AND 大法" //查询条件包含ElasticSearch和大法的记录,AND区分大小写}}}响应结果如下:
123456789101112131415161718192021222324252627{"took": 25,"timed_out": false,"_shards": {"total": 3,"successful": 3,"failed": 0},"hits": {"total": 1,"max_score": 4.444701,"hits": [{"_index": "book","_type": "novel","_id": "4","_score": 4.444701,"_source": {"author": "李四","title": "ElasticSearch大法好","wordcount": 1000,"publish_date": "2010-10-01"}}]}}还支持如下的查询,请求格式如下:
1234567{"query":{"query_string":{"query":"(ElasticSearch AND 大法) OR (入门)"}}}响应结果如下:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263{"took": 59,"timed_out": false,"_shards": {"total": 3,"successful": 3,"failed": 0},"hits": {"total": 4,"max_score": 4.444701,"hits": [{"_index": "book","_type": "novel","_id": "4","_score": 4.444701,"_source": {"author": "李四","title": "ElasticSearch大法好","wordcount": 1000,"publish_date": "2010-10-01"}},{"_index": "book","_type": "novel","_id": "2","_score": 2.9631343,"_source": {"author": "李三","title": "Java入门","wordcount": 2000,"publish_date": "2010-10-01"}},{"_index": "book","_type": "novel","_id": "3","_score": 2.4089072,"_source": {"author": "张三","title": "Python入门","wordcount": 2000,"publish_date": "2005-10-01"}},{"_index": "book","_type": "novel","_id": "8","_score": 1.4266168,"_source": {"author": "瓦力","title": "ElasticSearch入门","wordcount": 3000,"publish_date": "2017-08-20"}}]}}还支持多字段的查询,请求数据如下:
12345678{"query":{"query_string":{"query":"李四 OR ElasticSearch", //查询author和title字段包含 李四、ElasticSearch的记录"fields":["title","author"]}}}响应结果如下:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263{"took": 14,"timed_out": false,"_shards": {"total": 3,"successful": 3,"failed": 0},"hits": {"total": 4,"max_score": 2.8960366,"hits": [{"_index": "book","_type": "novel","_id": "4","_score": 2.8960366,"_source": {"author": "李四","title": "ElasticSearch大法好","wordcount": 1000,"publish_date": "2010-10-01"}},{"_index": "book","_type": "novel","_id": "9","_score": 1.1001158,"_source": {"author": "很胖的瓦力","title": "ElasticSearch精通","wordcount": 3000,"publish_date": "2017-08-15"}},{"_index": "book","_type": "novel","_id": "8","_score": 0.19856805,"_source": {"author": "瓦力","title": "ElasticSearch入门","wordcount": 3000,"publish_date": "2017-08-20"}},{"_index": "book","_type": "novel","_id": "12","_score": 0.155468,"_source": {"title": "瓦力教我们学ElasticSearch","author": "瓦力","wordcount": 1000,"publish_date": "2017-08-01"}}]}}
字段级别查询 针对结构化数据,如数字、日期等
请求地址:
http://192.168.137.8:9200/book/_search
请求方式:
POST
请求数据:
1234567{"query":{"term":{ //字段查询"wordcount":"5000" //查询wordcount字段的值等于5000的记录}}}响应结果如下:
123456789101112131415161718192021222324252627{"took": 81,"timed_out": false,"_shards": {"total": 3,"successful": 3,"failed": 0},"hits": {"total": 1,"max_score": 1,"hits": [{"_index": "book","_type": "novel","_id": "5","_score": 1,"_source": {"author": "王五","title": "菜谱","wordcount": 5000,"publish_date": "2002-10-01"}}]}}查询范围:
请求地址和请求方式不变,请求参数如下:
12345678910{"query":{"range":{"wordcount":{ //查询wordcount字段"gte":3000, //大于等于3000 e表示equal"lte":5000 //小于等于}}}}响应结果如下:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051{"took": 25,"timed_out": false,"_shards": {"total": 3,"successful": 3,"failed": 0},"hits": {"total": 3,"max_score": 1,"hits": [{"_index": "book","_type": "novel","_id": "5","_score": 1,"_source": {"author": "王五","title": "菜谱","wordcount": 5000,"publish_date": "2002-10-01"}},{"_index": "book","_type": "novel","_id": "8","_score": 1,"_source": {"author": "瓦力","title": "ElasticSearch入门","wordcount": 3000,"publish_date": "2017-08-20"}},{"_index": "book","_type": "novel","_id": "9","_score": 1,"_source": {"author": "很胖的瓦力","title": "ElasticSearch精通","wordcount": 3000,"publish_date": "2017-08-15"}}]}}还可以对日期字段进行范围查询:
请求地址和请求方式不变,请求参数如下:
12345678910{"query":{"range":{"publish_date":{ //出版日期,使用now关键表示当前时间"gte":"2017-01-01","lte":"2017-12-31"}}}}响应结果如下:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051{"took": 59,"timed_out": false,"_shards": {"total": 3,"successful": 3,"failed": 0},"hits": {"total": 3,"max_score": 1,"hits": [{"_index": "book","_type": "novel","_id": "8","_score": 1,"_source": {"author": "瓦力","title": "ElasticSearch入门","wordcount": 3000,"publish_date": "2017-08-20"}},{"_index": "book","_type": "novel","_id": "12","_score": 1,"_source": {"title": "瓦力教我们学ElasticSearch","author": "瓦力","wordcount": 1000,"publish_date": "2017-08-01"}},{"_index": "book","_type": "novel","_id": "9","_score": 1,"_source": {"author": "很胖的瓦力","title": "ElasticSearch精通","wordcount": 3000,"publish_date": "2017-08-15"}}]}}
Filter Context
概念:在查询过程中,只判断该文档是否满足条件,只有YES或NO。必须结合关键字bool
一起使用,并且ES对Filter查询会进行缓存。示例如下:
请求地址:http://192.168.137.8:9200/book/_search
请求方式:POST
请求数据:
|
|
响应结果如下:
|
|
复合条件查询
以一定的逻辑组合子条件查询;包含固定分数查询、布尔查询等
固定分数查询,这里的固定分数指的就是上面的匹配度
请求地址:http://192.168.137.8:9200/_search
请求方式:POST
请求数据:
|
|
响应结果如下:
|
|
指定固定分数:
|
|
布尔查询,使用bool
关键字
请求地址:http://192.168.137.8:9200/_search
请求方式:POST
请求数据:
|
|
响应结果:
|
|
还可以使用must
关键字
请求地址和请求方式不变,请求参数如下:
|
|
响应结果如下:
|
|
也可以结合filter
关键一起使用,请求地址和方式不变,请求数据如下:
|
|
响应结果如下:
|
|
还有must_not
关键,请求地址和请求方式不变,请求数据如下:
|
|
响应数据如下:
|
|