英文:
Nested aggregation in nested field?
问题
You can use Elasticsearch aggregations to achieve this grouping. Here's how you can get all documents grouped by areas
, which are then grouped by countries
, and retrieve the full documents:
{
"size": 0,
"aggs": {
"areas": {
"nested": {
"path": "areas"
},
"aggs": {
"countries": {
"nested": {
"path": "countries"
},
"aggs": {
"full_document": {
"reverse_nested": {},
"aggs": {
"documents": {
"top_hits": {
"size": 10 // Set the desired size for each group
}
}
}
}
}
}
}
}
}
}
This query first groups documents by areas
, then within each area
, it further groups them by countries
, and finally, it retrieves the full documents using the top_hits
aggregation.
Please adjust the size
parameter in the top_hits
aggregation to control the number of documents returned within each group.
英文:
I am new to elasticsearch and don't know a lot about aggregations but I have this ES6 mapping:
{
"mappings": {
"test": {
"properties": {
"id": {
"type": "integer"
}
"countries": {
"type": "nested",
"properties": {
"global_id": {
"type": "keyword"
},
"name": {
"type": "text",
"fields": {
"raw": {
"type": "keyword"
}
}
}
}
},
"areas": {
"type": "nested",
"properties": {
"global_id": {
"type": "keyword"
},
"name": {
"type": "text",
"fields": {
"raw": {
"type": "keyword"
}
}
},
"parent_global_id": {
"type": "keyword"
}
}
}
}
}
}
}
How can I get all documents grouped by areas
which is then grouped by countries
. Also the document has to be returned in full, not just the nested document. Is this even possible ?
答案1
得分: 1
- 聚合 _search 查询:
首先按区域进行聚合,路径为嵌套结构。然后返回到根文档并在嵌套结构中进行国家聚合。
{
"size": 0,
"aggs": {
"agg_areas": {
"nested": {
"path": "areas"
},
"aggs": {
"areas_name": {
"terms": {
"field": "areas.name"
},
"aggs": {
"agg_reverse": {
"reverse_nested": {},
"aggs": {
"agg_countries": {
"nested": {
"path": "countries"
},
"aggs": {
"countries_name": {
"terms": {
"field": "countries.name"
}
}
}
}
}
}
}
}
}
}
}
}
- 获取文档:
在您的聚合中添加 top_hits:
https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html
top_hits 较慢,所以您需要阅读文档并根据上下文调整大小和排序。
...
"terms": {
"field": "areas.name"
},
"aggregations": {
"hits": {
"top_hits": {
"size": 100
}
}
},
...
英文:
- Aggregation _search query:
first agg by area, with the path as this is nested. Then reverse to the root document and nested agg to country.
{
"size": 0,
"aggs": {
"agg_areas": {
"nested": {
"path": "areas"
},
"aggs": {
"areas_name": {
"terms": {
"field": "areas.name"
},
"aggs": {
"agg_reverse": {
"reverse_nested": {},
"aggs": {
"agg_countries": {
"nested": {
"path": "countries"
},
"aggs": {
"countries_name": {
"terms": {
"field": "countries.name"
}
}
}
}
}
}
}
}
}
}
}
}
- retrieve documents:
add a tophits inside your aggregation:
https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html
top_hits is slow so you will have to read documentation and adjust size and sort to your context.
...
"terms": {
"field": "areas.name"
},
"aggregations": {
"hits": {
"top_hits": { "size": 100}
}
},
...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论