惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
L
LangChain Blog
H
Help Net Security
博客园_首页
T
Tailwind CSS Blog
Microsoft Security Blog
Microsoft Security Blog
T
The Blog of Author Tim Ferriss
雷峰网
雷峰网
Recent Announcements
Recent Announcements
D
DataBreaches.Net
U
Unit 42
Vercel News
Vercel News
I
InfoQ
Martin Fowler
Martin Fowler
Microsoft Azure Blog
Microsoft Azure Blog
Apple Machine Learning Research
Apple Machine Learning Research
S
SegmentFault 最新的问题
Jina AI
Jina AI
博客园 - 叶小钗
博客园 - 【当耐特】
罗磊的独立博客
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
月光博客
月光博客
Last Week in AI
Last Week in AI

祈雨的笔记

安全多方计算MPC spark原理解析 kueue执行源码分析 spark on k8s执行源码分析 spark-operator源码解析 系统压测遇到的缓存击穿问题 我的世界PC与安卓联机 蚂蚁金服流量投放平台的AIG改造 G1大对象致Old区占用率高 日志打印导致接口响应率下跌分析 Groovy加载类导致OOM分析 ERROR日志打印导致CPU满载 记OceanBase死锁超时 应用发版期间服务响应超时 Ark Serverless初探 系统优化复盘一二三 The user specified as a definer does not exist Kong网关初探 API网关选型调研 CPU火焰图常用工具 配置中心选型调研 root操作Nginx导致用户组错误 基于Proxifier使用代理 FastJSON字段智能匹配踩坑 Nacos初探 记一次Nginx服务器CPU满荷载故障 基于券系统分库分表的思考 limit不参与SQL成本计算致索引失效 Linux常用性能监控命令 golang低版本http2偶现400
elasticsearch嵌套查询
祈雨的笔记 · 2018-09-20 · via 祈雨的笔记

嵌套查询

样本数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
PUT /testindex/testtype/1
{
"group":"fans",
"users":[
{
"name":"name1",
"age":20
},
{
"name":"name2",
"age":26
}
]
}
PUT /testindex/testtype/2
{
"group":"fans",
"users":[
{
"name":"name2",
"age":15
},
{
"name":"name3",
"age":30
}
]
}

查询

对于上述数据,通过普通查询方式查询users的结果会与预期不同。
例如如下的查询语句,预期只返回第一条数据,实际上两条数据全部返回。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
GET /testindex/testtype/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"users.name": "name2"
}
},
{
"range": {
"users.age": {
"gte": 20
}
}
}
]
}
}
}

修改mapping

使用嵌套查询,需要制定mapping为nested。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
PUT /testindex
{
"mappings": {
"testtype": {
"properties": {
"group": {
"type":"string"
},
"user": {
"type": "nested"
}
}
}
}
}

嵌套查询

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
GET /testindex/testtype/_search
{
"query": {
"nested": {
"path": "users",
"query": {
"bool": {
"must": [
{
"match": {
"users.name": "name2"
}
},
{
"range": {
"users.age": {
"gte": 20
}
}
}
]
}
}
}
}
}