我的数据看起来像是:
{
"id": 321,
"name": "Parent 1",
"childs":[
{
"id": 3211,
"name": "Child 1",
"data": "Some data"
},
{
"id": 3212,
"name": "Child 2"
},
{
"id": 3213,
"name": "Child 3"
}
]
}
现在我想查询所有没有"数据的所有孩子的ElasticSearch"得到这样的结果:
[
{
"id":321,
"childs":[
3212,
3213
]
}
]
我在文档中阅读了有关嵌套对象和父子关系的内容。我认为我需要像父子关系,子女的_id和查询_ids而不是查询来源。
有人可以帮我吗?
谢谢
答案 0 :(得分:2)
你肯定需要亲子。这是映射:
POST /my_index
{
"mappings": {
"parents": {
"properties": {
"name": {
"type": "string"
}
}
},
"children": {
"_parent": {
"type": "parents"
},
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "string"
},
"data": {
"type": "string"
}
}
}
}
}
示例数据:
POST /my_index/parents/_bulk
{"index":{"_id": 1}}
{"name":"Parent 1"}
{"index":{"_id": 2}}
{"name":"Parent 2"}
{"index":{"_id": 3}}
{"name":"Parent 3"}
POST /my_index/children/_bulk
{"index":{"_id": 1, "parent": 1}}
{"name":"Child 1","data":"Some data"}
{"index":{"_id": 2, "parent": 1}}
{"name":"Child 2"}
{"index":{"_id": 3, "parent": 1}}
{"name":"Child 3"}
{"index":{"_id": 4, "parent": 2}}
{"name":"Child 4","data":"Some data 4"}
{"index":{"_id": 5, "parent": 2}}
{"name":"Child 5","data":"Some data 5"}
{"index":{"_id": 6, "parent": 2}}
{"name":"Child 6"}
{"index":{"_id": 7, "parent": 3}}
{"name":"Child 7","data":"Some data 7"}
{"index":{"_id": 8, "parent": 3}}
{"name":"Child 8","data":"Some data 8"}
{"index":{"_id": 9, "parent": 3}}
{"name":"Child 9","data":"Some data 9"}
查询本身:
GET my_index/children/_search
{
"size": 0,
"query": {
"filtered": {
"filter": {
"missing": {
"field": "data"
}
}
}
},
"aggs": {
"missing_data": {
"terms": {
"field": "_parent"
},
"aggs": {
"top_children": {
"top_hits": {
"_source": "_parent"
}
}
}
}
}
}
您无法使用您想要的格式,但上述查询将返回父级的ID,并为每位父级返回按父级分组的子级ID。