小白学ES 07 - Elasticsearch的多种查询方式
2019-11-25

目录

1 Query String Search(查询串检索)2 Query DSL(ES特定语法检索)3 Query Filter(过滤检索)4 Full Text Search(全文检索)5 Phrase Search(短语检索)6 Highlight Search(高亮显示检索结果)

1 Query String Search(查询串检索)

这种方法通过HTTP请求的Query String携带查询参数, 因此得名.

适用于临时性的查询请求, 比如在终端检查基础信息:

# 检索name中包含Java的文档, 并按价格降序排序: curl -XGET "http://localhost:9200/shop/it_book/_search?q=name:Java&sort=price:desc"

生产环境中很少使用, 因为请求参数都封装到Query String中, 难以构建复杂的查询.

查询全部商品:

GET /shop/it_book/_search

响应结果:

{ "took": 2, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 3, "max_score": 1, "hits": [ { "_index": "shop", "_type": "it_book", "_id": "2", "_score": 1, "_source": { "name": "深入理解Java虚拟机:JVM高级特性与最佳实践", "author": "周志明", "desc": "Java图书领域公认的经典著作", "price": 79, "date": "2013-06", "publisher": "机械工业出版社", "tags": [ "Java", "虚拟机", "最佳实践" ] } }, // 省略其余2条记录 ] }}

响应结果中各参数说明:

took: 此次检索耗费的时间, 单位: 毫秒.timed_out: 是否超出规定的检索时间, 这里没有设置, 后续会讲解此参数._shards: 被查询的index被分解成5个分片, 所以搜索请求会分发到所有的primary shard(或primary shard对应的某个replica shard)上.hits.total: 符合查询请求的文档数, 即hit(命中)数.hits.max_score: Lucene底层对检索到的文档的相关度的评分, 相关度越高, 说明越匹配, score的值也就越高.hits.hits: 命中的所有document的详细数据.

2 Query DSL(ES特定语法检索)

DSL: Domain Specified Language, 特定领域的语言.

这种查询方式将查询参数构建为JSON格式的数据, 并封装到HTTP请求的Request Body(请求体)中, 可以构建各类复杂的查询语法, 比Query String Search强大很多.

查询全部商品:

GET /shop/it_book/_search{ "query": { "match_all": {} }}

查询name中包含Java的商品, 并按price降序排序

GET /shop/it_book/_search{ "query": { "match": { "name": "Java" } }, "sort": [ { "price": "desc" } ]}

分页查询商品 - 每页显示1条, 显示第3页:

GET /shop/it_book/_search{ "query": { "match_all": {} }, "from": 2, "size": 1}GET /ecommerce/product/_search{ "query": { "match_all": {} }, "_source": ["name", "price"]}

只查询商品的名称和价格:

GET /shop/it_book/_search{ "query": {"match_all": {}}, "_source": ["name", "price"]}

3 Query Filter(过滤检索)

过滤查询, 如: 查询name包含Java, 且price大于80元的商品:

GET /shop/it_book/_search{ "query": { "bool": { "must": { "match": {"name": "Java"} }, "filter": { "range": { "price": {"gt": 80} } } } }}

4 Full Text Search(全文检索)

查询desc中包含"Java图书"的文档, 只显示name和desc的值:

GET /shop/it_book/_search{ "query": { "match": {"desc": "Java图书"} }, "_source": ["name", "desc"]}

全文检索过程说明:

ES会对字段"desc"的内容进行分词, 并建立倒排索引.

这里将 "Java图书" 分词为 "Java"、"图"、"书" 3个, 检索时将匹配desc中含有 "Java"、"图"、"书" 中任意一个分词的文档.

—— 对于中文分词, 后续会使用IK分词器, 彼时会将 "Java图书" 分词为 "Java"、"图书" 2个.

响应结果:

{ "took": 2, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 2, "max_score": 0.7594807, "hits": [ { "_index": "shop", "_type": "it_book", "_id": "2", "_score": 0.7594807, "_source": { "name": "深入理解Java虚拟机:JVM高级特性与最佳实践", "desc": "Java图书领域公认的经典著作" } }, { "_index": "shop", "_type": "it_book", "_id": "1", "_score": 0.25316024, "_source": { "name": "Java编程思想", "desc": "Java学习必读经典,殿堂级著作!" } } ] }}

5 Phrase Search(短语检索)

Full Text Search会将检索文本作分词处理, 然后从倒排索引中作匹配查询, 如果某个文档的对应field中存在任意一个分解后的词, 该文档就算匹配检索条件.

Phrase Search不会对检索串进行分词处理, 只有一个文档的对应field中包含与检索文本完全一致的内容, 该文档才算匹配检索条件, 也才能作为结果返回.

精确查询desc中包含"Java图书"的文档:

GET /shop/it_book/_search{ "query": { "match_phrase": { "desc": "Java图书" } }, "_source": ["name", "desc"]}

响应结果:

{ "took": 13, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 1, "max_score": 0.7594808, "hits": [ { "_index": "shop", "_type": "it_book", "_id": "2", "_score": 0.7594808, "_source": { "name": "深入理解Java虚拟机:JVM高级特性与最佳实践", "desc": "Java图书领域公认的经典著作" } } ] }}

6 Highlight Search(高亮显示检索结果)

分页查询desc中包含"Java图书"的文档, 页大小为1, 显示第3页, 并对搜索条件高亮处理:

GET /shop/it_book/_search{ "query": { "match": {"desc": "Java图书"} }, "from": 0, "size": 1, "highlight": { "fields": {"desc": {}} }, "_source": ["name", "desc"]}

响应结果:

{ "took": 4, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 2, "max_score": 0.7594807, "hits": [ { "_index": "shop", "_type": "it_book", "_id": "2", "_score": 0.7594807, "_source": { "name": "深入理解Java虚拟机:JVM高级特性与最佳实践", "desc": "Java图书领域公认的经典著作" }, "highlight": { "desc": [ "<em>Java</em><em>图</em><em>书</em>领域公认的经典著作" // 高亮显示的内容: 默认添加<em>标签 ] } } ] }}

版权声明

作者: ma_shoufeng(马瘦风)

出处: 博客园 马瘦风的博客

您的支持是对博主的极大鼓励, 感谢您的阅读.

本文版权归博主所有, 欢迎转载, 但未经博主同意必须保留此段声明, 且在文章页面明显位置给出原文链接, 否则博主保留追究相关人员法律责任的权利.