elasticsearch 复杂查询小记
以下接口调用都基于5.5版本
JSON 文档格式
{
"_index": "zipkin-2017-09-06",
"_type": "span",
"_id": "AV5WSb1lKwYfgxikh_Fp",
"_score": null,
"_source": {
"timestamp_millis": 1504686226897,
"traceId": "58d858be36d2493e",
"id": "eb5e8ee2ff39eaa7",
"name": "close",
"parentId": "47622e0c4229a48b",
"timestamp": 1504686226897000,
"duration": 2,
"binaryAnnotations": [
{
"key": "ip",
"value": "127.0.0.1",
"endpoint": {
"serviceName": "redis",
"ipv4": "127.0.0.1",
"port": 20880
}
},
{
"key": "lc",
"value": "unknown",
"endpoint": {
"serviceName": "redis",
"ipv4": "127.0.0.1",
"port": 20880
}
},
{
"key": "service",
"value": "redis",
"endpoint": {
"serviceName": "redis",
"ipv4": "127.0.0.1",
"port": 20880
}
}
]
},
"fields": {
"timestamp_millis": [
1504686226897
]
},
"sort": [
1504686226897
]
}
1.OR条件查询格式
{"query":{"bool":{"should":[{},{},{}...}]}},"size":400,"from":0,"sort":[{"timestamp":{"order":"desc","unmapped_type":"boolean"}}]}
should条件的意思就只要匹配到里面其中一个条件就可以命中, 如
{"query":{"bool":{"should":[{"match":{"traceId":"6edb691b4bc775b1"}},{"match":{"traceId":"7e5b391r4bc775b1"}}]}},"size":400,"from":0,"sort":[{"timestamp":{"order":"desc","unmapped_type":"boolean"}}]}
只要traceId等于其中一个值就可以命中
2.AND 条件查询格式
{"query":{"bool":{"must":[{},{},{}...}]}},"size":400,"from":0,"sort":[{"timestamp":{"order":"desc","unmapped_type":"boolean"}}]}
must条件的意思就是必须匹配里面的所有条件才可以命中,如
{"query":{"bool":{"must":[{"range":{"timestamp":{"gte":1504581280866000,"lte":1504581280878000,"format":"date_time_no_millis"}}}, {"match":{"traceId":"6edb691b4bc775b1"}}],"must_not": {"exists": { "field": "parentId" } }}},"size":400,"from":0,"sort":[{"timestamp":{"order":"desc","unmapped_type":"boolean"}}]}
必须匹配traceId=6edb691b4bc775b1, 并且时间范围在1504581280866000,1504581280878000
3.是否含有某key
"must_not": {"exists": { "field": "parentId" } }
意思是查询必须没有parenId这个key的数据
{"query":{ "bool":{"must":[{"range":{"timestamp":{"gte":1504581280866000,"lte":1504581280878000,"format":"date_time_no_millis"}}}, {"match":{"traceId":"6edb691b4bc775b1"}}],"must_not": {"exists": { "field": "parentId" } }}}, "size":400,"from":0,"sort":[{"timestamp":{"order":"desc","unmapped_type":"boolean"}}]}
PS: 不管是must,should,must_not都是平级的,包含在bool里面
4.嵌套查询
{"query":{ "bool":{"must":[{"range":{"timestamp":{"gte":1504581280866000,"lte":1504581280878000,"format":"date_time_no_millis"}}}, {"match":{"traceId":"6edb691b4bc775b1"}},{"nested": {"path": "binaryAnnotations" ,"query": { "bool": {"must": [{ "match": { "binaryAnnotations.key": "service" }},{ "match": { "binaryAnnotations.value": "WebRequest" }}] } }}}],"must_not": {"exists": { "field": "parentId" } }}}, "size":400,"from":0,"sort":[{"timestamp":{"order":"desc","unmapped_type":"boolean"}}]}
nested嵌套查询和其他match,range条件一样,是包含在must,should这些条件里面
{"nested": {"path": "binaryAnnotations" ,"query": { "bool": {"must": [{ "match": { "binaryAnnotations.key": "service" }},{ "match": { "binaryAnnotations.value": "WebRequest" }}] } }}}
我们的JSON文档里有binaryAnnotations这个key, 而value是一个数组, 嵌套查询必须指定path,在我们这里就是binaryAnnotations,然后里面再使用query查询,query里面的语法和外层的一样
5.复合条件嵌套查询
假设我们要查询binaryAnnotations 里面两个并行的条件
{"query":{ "bool":{"must":[{"range":{"timestamp":{"gte":1504581280866000,"lte":1504581280878000,"format":"date_time_no_millis"}}}, {"match":{"traceId":"6edb691b4bc775b1"}},{"nested": {"path": "binaryAnnotations" ,"query": { "bool": {"must": [{ "match": { "binaryAnnotations.key": "service" }},{ "match": { "binaryAnnotations.value": "WebRequest" }}] } }}},{"nested": {"path": "binaryAnnotations" ,"query": { "bool": {"must": [{ "match": { "binaryAnnotations.key": "ip" }},{ "match": { "binaryAnnotations.value": "127.0.0.1" }}] } }}}],"must_not": {"exists": { "field": "parentId" } }}}, "size":400,"from":0,"sort":[{"timestamp":{"order":"desc","unmapped_type":"boolean"}}]}
6.去重查询
{"query":{"bool":{"must":[ {"match":{"name":"query"}} ]}}, "aggs": {"traceId": {"terms": {"field": "traceId","size": 10 }}}, "size":10,"from":0,"sort":[{"timestamp":{"order":"desc","unmapped_type":"boolean"}}]}
去重要使用aggs 语句,和query查询平级,这里的意思是获取name=query 的记录并且用traceId去重
elasticsearch 复杂查询小记的更多相关文章
- 【转】elasticsearch的查询器query与过滤器filter的区别
很多刚学elasticsearch的人对于查询方面很是苦恼,说实话es的查询语法真心不简单- 当然你如果入门之后,会发现elasticsearch的rest api设计是多么有意思. 说正题,ela ...
- 基于百度地图SDK和Elasticsearch GEO查询的地理围栏分析系统(1)
本文描述了一个系统,功能是评价和抽象地理围栏(Geo-fencing),以及监控和分析核心地理围栏中业务的表现. 技术栈:Spring-JQuery-百度地图WEB SDK 存储:Hive-Elast ...
- Elasticsearch Kibana查询语法
Elasticsearch Kibana查询语法 2018年06月03日 23:52:30 wangpei1949 阅读数:3992 Elasticsearch Kibana Discover的搜 ...
- ElasticSearch—分页查询
ElasticSearch查询—分页查询详解 Elasticsearch中数据都存储在分片中,当执行搜索时每个分片独立搜索后,数据再经过整合返回.那么,如何实现分页查询呢? 按照一般的查询流程来说,如 ...
- Elasticsearch 邻近查询示例
Elasticsearch 邻近查询示例(全切分分词) JAVA API方式: SpanNearQueryBuilder span = QueryBuilders.spanNearQuery(); s ...
- elasticsearch简单查询
elasticsearch简单查询示例: { "from": "0", //分页,从第一页开始 "size": "10" ...
- ElasticSearch高级查询
ElasticSearch高级查询 https://www.imooc.com/video/15759/0 ElasticSearch查询 1,子条件查询:特定字段查询所指特定值 1.1query c ...
- elasticsearch 分页查询实现方案——Top K+归并排序
elasticsearch 分页查询实现方案 1. from+size 实现分页 from表示从第几行开始,size表示查询多少条文档.from默认为0,size默认为10,注意:size的大小不能超 ...
- ELK 学习笔记之 elasticsearch 基本查询
elasticsearch 基本查询: 基本查询: term查询: terms查询: from和size查询: match查询: match_all查询: match_phrase查询: multi_ ...
随机推荐
- <NET CLR via c# 第4版>笔记 第12章 泛型
泛型优势: 源代码保护 使用泛型算法的开发人员不需要访问算法的源代码.(使用c++模板的泛型技术,算法的源代码必须提供给使用算法的用户) 类型安全 向List<DateTime>实例添加一 ...
- Giter Link
Giter Link Anton Chen https://github.com/antonchen Bear Zheng https://github.com/bearzlh Mac Directo ...
- 程序运行时间c++/matlab
前言 一般在调试程序的过程中,需要查看代码运行速度的快慢,此时则需要计算代码的运行时间. 实验过程: c++: #include<iostream> #include<time.h& ...
- 广播中receiver配置需要注意data的配置
1.sd卡的转载和卸载 这个需要配置好android:scheme=“file” 要不然是检测不到的 2.在安装应用或者卸载应用都要有一个约束,要不然是不会执行的.而这个约束条件为package. 但 ...
- iOS7,iOS8和iOS9的区别
iOS7,iOS8和iOS9的区别:iOS7.0 1.iOS 7是iOS面世以来在用户界面上做出改变最大的一个操作系统.iOS 7抛弃了以往的拟物化设计,而采用了扁平化设计. 苹果在重新思考 iOS ...
- Quartz(一):Cron表达式
正文 1. 表达式介绍 Cron:cron表达式用于配置cronTrigger的实例.cron表达式实际上是由七个子表达式组成.这些表达式之间用空格分隔. Cron格式:[秒] [分] [小时] [日 ...
- 压力测试命令行工具SuperBenchmarker
压力测试命令行工具SuperBenchmarker SuperBenchmarker 是ㄧ个开源的类似于Apache ab的压力测试命令行工具.可以在 .NET 4.52+ 或者 .NET Core ...
- .NET Core Generic Host Windows服务部署使用Topshelf
此文源于前公司在迁移项目到.NET Core的过程中,希望使用Generic Host来管理定时任务程序时,没法部署到Windows服务的问题,而且官方也没给出解决方案,只能关注一下官方issue # ...
- Python & 机器学习入门指导
Getting started with Python & Machine Learning(阅者注:这是一篇关于机器学习的指导入门,作者大致描述了用Python来开始机器学习的优劣,以及如果 ...
- test20181004 排列
题意 分析 容斥公式的意义 选了原图中\(x(x \geq i)\)条边的方案,重复了\(\binom{x}{i}\)次. 有多加多减,所以就是那个式子. 具体而言,对选了x条原图中的边的方案,总共加 ...