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_ ...
随机推荐
- windowsphone开发页面跳转到另一个dll中的页面
WP的页面跳转一般是只能跳转到本DLL的页面, 如果要跳转到其他DLL的页面则需要这样写 (Application.Current.RootVisual as PhoneApplicationFram ...
- L1-003 个位数统计
给定一个 k 位整数 N=d(k−1)*10^(k−1)+⋯+d(1)*10^1+d(0) (0≤d(i)≤9, i=0,⋯,k−1, d(k−1)>0 ...
- 对Repository模式误用的反思和纠正
一直以来想自己做一套开发框架,在其基础上进行快速开发,自从接触微软的MVC框架和Entityframework以来,阅读了大量园子里的相关的技术文章,也进行了不少摸索和尝试,中间经历了多次大刀阔斧的重 ...
- Beta 冲刺(7/7)
前言 队名:拖鞋旅游队 组长博客:https://www.cnblogs.com/Sulumer/p/10129067.html 作业博客:https://edu.cnblogs.com/campus ...
- spring事务管理及相关知识
最近在项目中遇到了spring事务的注解及相关知识,突然间感觉自己对于这部分知识只停留在表面的理解层次上,于是乎花些时间上网搜索了一些文章,以及对于源码的解读,整理如下: 一.既然谈到事务,那就先搞清 ...
- WebGL编程指南理论分析之物体层次模型(局部运动)
书中340页,开始讲到层次模型(关节模型),也就是整个物体,可以自由控制其各部位单独运动,就像关节一样,互不干扰或者有一定关联. 就像图中,左右键控制整个物体(arm1和arm2)的Y轴旋转,上下键控 ...
- SWIFT Scan QRCode
SWIFT中扫描QRCode代码如下,照着敲一次再看下API的注释应该就没问题了. import UIKit import Foundation import AVFoundation class V ...
- NVIDIA GeForce GTX 960 设备是不可移动的,无法弹出
系统环境 系统:win7_x64; matlab版本:matlab2017b试用版: GPU:NVIDIA GeForce GTX 960: 问题描述: 第一次使用深度学习实现代码,运行的是matla ...
- Codeforces 1096G. Lucky Tickets【生成函数】
LINK 题目大意 很简单自己看 思路 考虑生成函数(为啥tags里面有一个dp啊) 显然,每一个指数上是否有系数是由数集中是否有这个数决定的 有的话就是1没有就是0 然后求出这个生成函数的\(\fr ...
- Hibernate4.3配置
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hi ...