什么是DSL?
Elasticsearch提供丰富且灵活的查询语言叫做DSL查询(Query DSL),它允许你构建更加复杂、强大的查询。 DSL(Domain Specific Language特定领域语言)以JSON请求体的形式出现。目前常用的框架查询方法什么的底层都是构建DSL语句实现的,所以你必须掌握DSL。
例如:
GET book/_search
{"query": {"match": {"name": "活着"}}
}
准备测试数据
请参考我的另一篇文章:https://blog.csdn.net/IndexMan/article/details/140611813
#精确查询
也就是term查询不会分析查询条件,只有当词条和查询字符串完全匹配时才匹配,也就是精确查找,比如数字,日期,布尔值或 not_analyzed 的字符串(未经分析的文本数据类型):
GET /article_doc/_doc/1815982664109314052
- 请求
{"query": {"term": {"category": "技术"}}
}
- 响应
{"_index": "article_doc","_type": "_doc","_id": "1815982664109314052","_version": 1,"_seq_no": 4,"_primary_term": 1,"found": true,"_source": {"author": "华为","category": "技术","content": "就在昨天,华为正式官宣自研编程语言 「仓颉」 正式诞生!!!\r\n\r\n华为,作为全球领先的信息与通信技术(ICT)解决方案提供商,以“仓颉”编程语言的诞生,再次引领了技术革新的潮流,为智能时代的软件开发定义了新的艺术。","createTime": "2024-07-24 13:29:41","id": "1815982664109314052","readCount": 1,"title": "华为自研编程语言“仓颉”正式发布"}
}
terms查询:terms 跟 term 有点类似,但 terms 允许指定多个匹配条件。 如果某个字段指定了多个值,那么文档需要一起去 做匹配:
{"query": {"terms": {"category": [ "技术","管理"]}}
}
全文查询
顾名思义,全文查询会分析查询条件,先将查询条件进行分词,然后查询,求并集。
区别于term,match的区别是:match是经过analyer分词器分词的,也就是说,文档首先被分析器给处理了。根据不同的分析器,分析的结果也稍显不同,然后再根据分词结果进行匹配。term则不经过分词,它是直接去倒排索引中查找了精确的值了。
match 查询的主要用法:
- match_all:查询所有,无条件;太简单不再演示!
- match:返回所有匹配到的数据
- match_phrase:短语查询,在match的基础上进一步查询词组,可以指定slop分词间隔。
{"query": {"match_phrase": {"content": {"query": "中国"}}}
}
- match_phrase_prefix:前缀查询,根据短语中最后一个词组做前缀匹配,可以应用于搜索提示,但注意和max_expanions搭配。
- multi_match:多字段查询,使用相当的灵活,可以完成match_phrase和match_phrase_prefix的工作。
post /article_doc/_search
- 请求
{"query": {"match": {"content": "技术"}}
}
- 响应
{"took": 11,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 1,"relation": "eq"},"max_score": 3.3864474,"hits": [{"_index": "article_doc","_type": "_doc","_id": "1815982664109314052","_score": 3.3864474,"_source": {"author": "华为","category": "技术","content": "就在昨天,华为正式官宣自研编程语言 「仓颉」 正式诞生!!!\r\n\r\n华为,作为全球领先的信息与通信技术(ICT)解决方案提供商,以“仓颉”编程语言的诞生,再次引领了技术革新的潮流,为智能时代的软件开发定义了新的艺术。","createTime": "2024-07-24 13:29:41","id": "1815982664109314052","readCount": 1,"title": "华为自研编程语言“仓颉”正式发布"}}]}
}
- 多字段查询
{"query": {"multi_match": {"query": "中国","fields": ["title","content"]}}
}
排序查询
例如,按照文章阅读数降序排序:
{"query": {"match_all": {}},"sort": [{"readCount": {"order": "desc"}}]
}
范围查询
range 过滤允许我们按照指定范围查找一批数据,范围操作符包含:
- gt:大于,相当于关系型数据库中的 >
- gte:大于等于,相当于关系型数据库中的 >=
- lt:小于,相当于关系型数据库中的 <
- lte:小于等于,相当于关系型数据库中的 <=
例如,查询文章阅读数大于5的文章列表:
{"query": {"range": {"readCount": {"gt": 5}}}
}
分页查询
{"query": {"match_all": {}},"from": 0,"size": 5
}
高亮查询
{"query": {"match": {"content": "智能"}},"highlight": {"fields": {"content": {"pre_tags": "<b class='key' style='color:red'>","post_tags": "</b>"}}}
}
{"took": 32,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 3,"relation": "eq"},"max_score": 1.2924489,"hits": [{"_index": "article_doc","_type": "_doc","_id": "1815982664109314052","_score": 1.2924489,"_source": {"author": "华为","category": "技术","content": "就在昨天,华为正式官宣自研编程语言 「仓颉」 正式诞生!!!\r\n\r\n华为,作为全球领先的信息与通信技术(ICT)解决方案提供商,以“仓颉”编程语言的诞生,再次引领了技术革新的潮流,为智能时代的软件开发定义了新的艺术。","createTime": "2024-07-24 13:29:41","id": "1815982664109314052","readCount": 1,"title": "华为自研编程语言“仓颉”正式发布"},"highlight": {"content": ["华为,作为全球领先的信息与通信技术(ICT)解决方案提供商,以“仓颉”编程语言的诞生,再次引领了技术革新的潮流,为<b class='key' style='color:red'>智能</b>时代的软件开发定义了新的艺术。"]}},{"_index": "article_doc","_type": "_doc","_id": "1815982664180617221","_score": 0.9935417,"_source": {"author": "狸花猫","category": "人工智能","content": "在信息时代,Web 页面成为我们与世界交互的重要窗口。如今,AI 程序的出现,为 Web 页面带来了新的变革。通过在 Web 页面上实现图片识别,我们即将迈入一个更加智能与便捷的时代。它将赋予网页全新的能力,使其能够理解图片的内容,为用户带来前所未有的体验。让我们一同踏上这充满无限可能的探索之旅。\n\n作者:睡着学\n链接:https://juejin.cn/post/7359084330121789452\n来源:稀土掘金\n著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。","createTime": "2024-07-24 13:29:41","id": "1815982664180617221","readCount": 3,"title": "AI 赋能007 jack Web 页面,图像识别超越想象"},"highlight": {"content": ["通过在 Web 页面上实现图片识别,我们即将迈入一个更加<b class='key' style='color:red'>智能</b>与便捷的时代。它将赋予网页全新的能力,使其能够理解图片的内容,为用户带来前所未有的体验。让我们一同踏上这充满无限可能的探索之旅。"]}},{"_index": "article_doc","_type": "_doc","_id": "1815982664180617220","_score": 0.85687244,"_source": {"author": "kimi","category": "人工智能","content": "今年,随着各大GLM模型的开源和算力的提升,尤其是最近比较火热的月之暗面的Kimi 模型,AI应用场景中的各种智能体如雨后春笋般涌现。许多同学们纷纷表达了加入AI应用的学习和测试的愿望,然而各大模型提供商所提供的API的免费tokens在数量和其他方面都存在着不同的限制,这给我们这些白嫖党带来了一定的不便。今天,在GitHub上,我发现了这个名为kimi-free-api的项目,它为我们学习和测试使用GLM模型提供了极大的便利。\n\n作者:修己xj\n链接:https://juejin.cn/post/7357546247848427558\n来源:稀土掘金\n著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。","createTime": "2024-07-22 13:29:41","id": "1815982664180617220","readCount": 10,"title": "免费使用Kimi的API接口,kimi-free-api真香"},"highlight": {"content": ["今年,随着各大GLM模型的开源和算力的提升,尤其是最近比较火热的月之暗面的Kimi 模型,AI应用场景中的各种<b class='key' style='color:red'>智能</b>体如雨后春笋般涌现。"]}}]}
}
布尔查询
bool 查询可以用来合并多个条件查询结果的布尔逻辑,它包含以下操作符:
- must:多个查询条件必须完全匹配,相当于关系型数据库中的 且 and
- should:至少有一个查询条件匹配,相当于关系型数据库中的 或 or
- must_not: 多个查询条件的相反匹配,相当于关系型数据库中的 非 not
- filter:过滤满足条件的数据。
range:条件筛选范围,上面讲过了
{"query": {"bool": {"should": [{"match": {"title": "智能"}},{"match": {"content": "智能"}}]}}
}
聚合查询
我们平时在使用Elasticsearch时,更多会用到聚合操作,它类似SQL中的group by操作。ES的聚合查询一定是先查出结果,然后对结果使用聚合函数做处理,常用的操作有:avg:求平均、max:最大值、min:最小值、sum:求和等。
在ES中聚合分为指标聚合和分桶聚合:
- 指标聚合:指标聚合对一个数据集求最大、最小、和、平均值等
{"query": {"match_all": {}},"aggs": {"avg_read": {"avg": {"field": "readCount"}}},"_source": ["id","readCount"]
}
- 响应
{"took": 5,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 10,"relation": "eq"},"max_score": 1,"hits": [{"_index": "article_doc","_type": "_doc","_id": "1815982662364483586","_score": 1,"_source": {"id": "1815982662364483586","readCount": 0}},{"_index": "article_doc","_type": "_doc","_id": "1815982664109314049","_score": 1,"_source": {"id": "1815982664109314049","readCount": 1}},{"_index": "article_doc","_type": "_doc","_id": "1815982664109314050","_score": 1,"_source": {"id": "1815982664109314050","readCount": 2}},{"_index": "article_doc","_type": "_doc","_id": "1815982664109314052","_score": 1,"_source": {"id": "1815982664109314052","readCount": 1}},{"_index": "article_doc","_type": "_doc","_id": "1815982664109314053","_score": 1,"_source": {"id": "1815982664109314053","readCount": 5}},{"_index": "article_doc","_type": "_doc","_id": "1815982664180617218","_score": 1,"_source": {"id": "1815982664180617218","readCount": 6}},{"_index": "article_doc","_type": "_doc","_id": "1815982664180617219","_score": 1,"_source": {"id": "1815982664180617219","readCount": 0}},{"_index": "article_doc","_type": "_doc","_id": "1815982664180617220","_score": 1,"_source": {"id": "1815982664180617220","readCount": 10}},{"_index": "article_doc","_type": "_doc","_id": "1815982664109314051","_score": 1,"_source": {"id": "1815982664109314051","readCount": 3}},{"_index": "article_doc","_type": "_doc","_id": "1815982664180617221","_score": 1,"_source": {"id": "1815982664180617221","readCount": 3}}]},"aggregations": {"avg_read": {"value": 3.1}}
}
- 分桶聚合:除了有聚合函数外,还可以对查询出的数据进行分组group by,再在组上进行游标聚合。
{"size": 0,"query": {"match_all": {}},"aggs": {"age_group": {"terms": {"field": "category"}}}
}
- 响应:
{"took": 208,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 10,"relation": "eq"},"max_score": null,"hits": []},"aggregations": {"age_group": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "人工智能","doc_count": 3},{"key": "技术","doc_count": 2},{"key": "经济","doc_count": 2},{"key": "生活","doc_count": 1},{"key": "管理","doc_count": 1},{"key": "面试","doc_count": 1}]}}
}
总结
以上就是DSL的核心用法,用到了再看也不迟,单纯记忆很快就会忘记没啥用。