介绍
ES中提供了一种强大的检索数据方式,这种检索方式称之为Query DSL <Domain Specified Language>
Query DSL是利用Rest API传递JSON格式的请求体(RequestBody)数据与ES进行交互,这种方式的丰富查询语法让Es检索变得更强大,更简洁。
语法
查询所有[match_all]
shell
复制代码
# GET /索引/_search {json}
- 测试
bash
复制代码
# query DSL 语法 查询所有 match_all GET /products/_search { "query":{ "match_all":{"boost": 1} } }
输出结果
json
复制代码
{ "took": 0, # 花费时间,单位ms "timed_out": false, # 是否超时 "_shards": { # 分片信息 "total": 1, "successful": 1, "skipped": 0, "failed": 0 }, "hits": { # 命中 "total": { # 命中总数 "value": 3, "relation": "eq" }, "max_score": 1, # 得分 "hits": [ # 真正的文档信息 { "_index": "products", "_id": "1", "_score": 1, "_source": { "id": 1, "title": "我是cfddfc", "price": 1, "created_at": "2012-11-12", "description": "小老鼠真好吃" } }, { "_index": "products", "_id": "4", "_score": 1, "_source": { "id": 4, "title": "好吃的薯片", "price": 100, "created_at": "2012-11-12", "description": "薯片真香!" } }, { "_index": "products", "_id": "3", "_score": 1, "_source": { "id": 3, "title": "与馒头", "price": 0.5, "created_at": "2012-11-12", "description": "馒头太好吃了吧" } } ] } }
关键词查询[term]
trem:使用关键词查询
- 在 ES 中除了 text 类型分词,其余类型均不分词
- 在 ES 中默认使用标准分词器 中文单字分词 英文单词分词
分词就是查询的依据,如果查询 value 出现某个分词,就会返回该查询 不分词说明,查询这个键是否匹配,必须要完全匹配。
bash
复制代码
# term 基于关键词查询 GET /products/_mapping # keyword 类型 日后搜索使用 全部内容搜索 # text 类型 默认 es 标准分词器 中文单字分词 英文单词分词 # keyword integer double date 不分词 GET /products/_search { "query": { "term": { "id": { "value": 1 } } } }
范围查询[ranger]
range关键字:用来指定查询指定范围内的文档
bash
复制代码
# 范围查询 ranger GET /products/_search { "query": { "range": { "price": { "gte": 1, "lte": 20 } } } }
前缀查询[prefix]
prefix 关键字:用来检索含有指定前缀的关键词的相关文档
bash
复制代码
# 前缀查询 GET /products/_search { "query": { "prefix": { "description": { "value": "小老鼠" } } } } GET /products/_search { "query": { "prefix": { "description": { "value": "小" } } } }
因为中文默认分词器是一个字一个字,所以查询不到,结果为空
csharp
复制代码
# pre { "took": 0, "timed_out": false, "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 0, "relation": "eq" }, "max_score": null, "hits": [] } } # suf { "took": 0, "timed_out": false, "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 1, "relation": "eq" }, "max_score": 1, "hits": [ { "_index": "products", "_id": "1", "_score": 1, "_source": { "id": 1, "title": "我是cfddfc", "price": 1, "created_at": "2012-11-12", "description": "小老鼠真好吃" } } ] } }
通配符查询[wildcard]
wildcard关键字:通配符查询
?
用来匹配一个任意字符*
用来匹配多个任意字符
bash
复制代码
# 通配符查询 * 多个 ? 一个 GET /products/_search { "query": { "wildcard": { "description": { "value": "小*" } } } }
多id查询[ids]
ids关键字:值为数组类型,用来根据一组id获取多个对应的文档
bash
复制代码
# 多id查询 查询一组符合条件的id GET /products/_search { "query": { "ids": { "values": [3,4,2] } } }
模糊查询[fuzzy]
fuzzy关键字:用来模糊查询含有指定关键字的文档
bash
复制代码
# fuzzy 模糊查询 GET /products/_search { "query": { "fuzzy": { "title": "鱼馒头" } } }
结果
json
复制代码
{ "took": 0, "timed_out": false, "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 1, "relation": "eq" }, "max_score": 0.65388614, "hits": [ { "_index": "products", "_id": "3", "_score": 0.65388614, "_source": { "id": 3, "title": "与馒头", "price": 0.5, "created_at": "2012-11-12", "description": "馒头太好吃了吧" } } ] } }
注意:fuzzy模糊查询最大模糊错误必须在 8 - 2 之间 搜索关键词长度为 2 不允许存在模糊 搜索关键词长度为 3 - 5 允许一次模糊搜索关键词长度大于 5 允许最大 2 模糊
布尔查询[bool]
bool关键字:用来组合多个条件实现复杂查询 must:相当于 && 同时成立 should:相当于 || 成立一个就行 must_not:相当于 ! 不能满足任何一个
bash
复制代码
# bool 查询 GET /products/_search { "query": { "bool": { "must": [ { "ids": { "values": [1] } },{ "term": { "title": { "value": "我是cfddfc" } } } ] } } }
多字段查询[multi_match]
bash
复制代码
# 多字段查询 multi_match # 注意:query 输入关键词 输入一段文本 GET /products/_search { "query": { "multi_match": { "query": "老鼠馒头", "fields": ["title","description"] } } }
注意:字段类型分词,将查询条件分词之后进行查询改字段 如果该字段不分词就会将查询条件作为整体进行查询
json
复制代码
{ "took": 1, "timed_out": false, "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 2, "relation": "eq" }, "max_score": 1.9155619, "hits": [ { "_index": "products", "_id": "1", "_score": 1.9155619, "_source": { "id": 1, "title": "我是cfddfc", "price": 1, "created_at": "2012-11-12", "description": "小老鼠真好吃" } }, { "_index": "products", "_id": "3", "_score": 1.7894151, "_source": { "id": 3, "title": "与馒头", "price": 0.5, "created_at": "2012-11-12", "description": "馒头太好吃了吧" } } ] } }
默认字段分词查询[query_string]
注意:查询字段分词就将查询条件分词查询 查询字段不分词将查询条件不分词查询
bash
复制代码
# query_string GET /products/_search { "query": { "query_string": { "default_field": "description", "query": "小老鼠的的" } } }
高亮查询[highlight]
highlight关键字:可以让符合条件的文档中的关键词高亮
bash
复制代码
# query_string GET /products/_search { "query": { "query_string": { "default_field": "description", "query": "小老鼠的的" } }, "highlight": { "fields": { "description":{} } } }
默认<em>
标签可以显示高亮,需要前端上面再修改一下,作为特殊的样式自定义
bash
复制代码
# query_string GET /products/_search { "query": { "query_string": { "default_field": "description", "query": "小老鼠的的" } }, "highlight": { "pre_tags": ["<span style='color:red;'>"], "post_tags": ["</span>"], "require_field_match": "false", "fields": { "description":{} } } }
并没有修改文档,而是在输出时加上这些样式
json
复制代码
{ "took": 64, "timed_out": false, "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 1, "relation": "eq" }, "max_score": 2.873343, "hits": [ { "_index": "products", "_id": "1", "_score": 2.873343, "_source": { "id": 1, "title": "我是cfddfc", "price": 1, "created_at": "2012-11-12", "description": "小老鼠真好吃" }, "highlight": { "description": [ "<em>小</em><em>老</em><em>鼠</em>真好吃" ] } } ] } }
指定字段排序[sort]
排序之后,每个文档都没有的得分,因为排序干扰了es默认的计分规则
bash
复制代码
# query_string GET /products/_search { "query": { "query_string": { "default_field": "description", "query": "*" } }, "highlight": { "pre_tags": ["<span style='color:red;'>"], "post_tags": ["</span>"], "require_field_match": "false", "fields": { "description":{} } }, "sort":[ { "price":"desc" } ] }
返回指定字段[_source]
_source
关键字:是一个数组,在数组中用来指定展示那些字段
json
复制代码
"_source": ["id","title","description"]