我正在使用ElasticSearch 6.3,我正在处理一个包含多个子聚合的聚合,其中我需要根据较低级别reverse_nested聚合的doc_count来排序顶级聚合桶。
我的索引是这样创建的:
PUT /myindex
{
"mappings": {
"default": {
"properties": {
"items": {
"type": "nested",
"properties": {
"subitems": {
"type": "nested",
"properties": {
"id": {
"type": "long"
},
"name": {
"type": "keyword"
}
}
}
}
},
"name": {
"type": "keyword"
}
}
}
}
}
这些是我索引的示例文档:
{
"name": "Document #1",
"items": [
{
"subitems": [
{
"id": 1,
"name": "Subitem #1"
},
{
"id": 2,
"name": "Subitem #2"
}
]
},
{
"subitems": [
{
"id": 2,
"name": "Subitem #2"
},
{
"id": 3,
"name": "Subitem #3"
}
]
}
]
}
{
"name": "Document #2",
"items": [
{
"subitems": [
{
"id": 2,
"name": "Subitem #2"
}
]
}
]
}
{
"name": "Document #3",
"items": [
{
"subitems": [
{
"id": 3,
"name": "Subitem #3"
}
]
},
{
"subitems": [
{
"id": 2,
"name": "Subitem #2"
}
]
}
]
}
{
"name": "Document #4",
"items": [
{
"subitems": [
{
"id": 2,
"name": "Subitem #2"
},
{
"id": 5,
"name": "Subitem #5"
}
]
}
]
}
{
"name": "Document #5",
"items": [
{
"subitems": [
{
"id": 2,
"name": "Subitem #2"
}
]
},
{
"subitems": [
{
"id": 2,
"name": "Subitem #2"
}
]
},
{
"subitems": [
{
"id": 2,
"name": "Subitem #2"
}
]
},
{
"subitems": [
{
"id": 2,
"name": "Subitem #2"
}
]
},
{
"subitems": [
{
"id": 2,
"name": "Subitem #2"
}
]
},
{
"subitems": [
{
"id": 2,
"name": "Subitem #2"
}
]
}
]
}
{
"name": "Document #6",
"items": [
{
"subitems": [
{
"id": 3,
"name": "Subitem #3"
}
]
}
]
}
{
"name": "Document #7",
"items": [
{
"subitems": [
{
"id": 3,
"name": "Subitem #3"
}
]
}
]
}
{
"name": "Document #8",
"items": [
{
"subitems": [
{
"id": 3,
"name": "Subitem #3"
}
]
}
]
}
{
"name": "Document #9",
"items": [
{
"subitems": [
{
"id": 3,
"name": "Subitem #3"
}
]
}
]
}
我需要我的聚合能够提取包含每个子项id/名称对的文档数。(考虑子项ID始终对应于相同的子项名称)。即:
id | name | count
---+------------+------
2 | Subitem #2 | 5
3 | Subitem #3 | 6
1 | Subitem #1 | 1
5 | Subitem #5 | 1
这是原始聚合查询:
GET /myindex/default/_search
{
"size": 0,
"aggregations": {
"my_nested_agg": {
"nested": {
"path": "items.subitems"
},
"aggregations": {
"subitem_id": {
"terms": {
"field": "items.subitems.id"
},
"aggregations": {
"subitem_name": {
"terms": {
"field": "items.subitems.name"
},
"aggregations": {
"my_rev_agg": {
"reverse_nested": {}
}
}
}
}
}
}
}
}
}
聚合似乎返回了我需要的所有数据:
{
"took": 0,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 9,
"max_score": 0.0,
"hits": []
},
"aggregations": {
"my_nested_agg": {
"doc_count": 19,
"subitem_id": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 2,
"doc_count": 11,
"subitem_name": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "Subitem #2",
"doc_count": 11,
"my_rev_agg": {
"doc_count": 5
}
}
]
}
},
{
"key": 3,
"doc_count": 6,
"subitem_name": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "Subitem #3",
"doc_count": 6,
"my_rev_agg": {
"doc_count": 6
}
}
]
}
},
{
"key": 1,
"doc_count": 1,
"subitem_name": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "Subitem #1",
"doc_count": 1,
"my_rev_agg": {
"doc_count": 1
}
}
]
}
},
{
"key": 5,
"doc_count": 1,
"subitem_name": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "Subitem #5",
"doc_count": 1,
"my_rev_agg": {
"doc_count": 1
}
}
]
}
}
]
}
}
}
}
但是,存储桶是根据“subitem\u id”子聚合的doc\u计数按降序排列的。
相反,我需要根据reverse\u嵌套子聚合的doc\u计数,按降序对桶进行排序。这样地:
id | name | count
---+------------+------
3 | Subitem #3 | 6
2 | Subitem #2 | 5
1 | Subitem #1 | 1
5 | Subitem #5 | 1
我试图通过以下查询实现这一点:
GET /myindex/default/_search
{
"size": 0,
"aggregations": {
"my_nested_agg": {
"nested": {
"path": "items.subitems"
},
"aggregations": {
"subitem_id": {
"terms": {
"field": "items.subitems.id",
"order": [
{
"subitem_name>my_rev_agg._count": "desc"
}
]
},
"aggregations": {
"subitem_name": {
"terms": {
"field": "items.subitems.name"
},
"aggregations": {
"my_rev_agg": {
"reverse_nested": {}
}
}
}
}
}
}
}
}
}
但我得到了一个错误:
无效的聚合顺序路径[subitem_name
能请你指点一下吗?非常感谢。
我找到了一个我想要的解决方案。关键点是将reverse_nested聚合移到用于检索名称的术语子聚合之外:
GET /myindex/default/_search
{
"size": 0,
"aggregations": {
"my_nested_agg": {
"nested": {
"path": "items.subitems"
},
"aggregations": {
"subitem_id": {
"terms": {
"field": "items.subitems.id",
"order": [
{
"my_rev_agg": "desc"
}
]
},
"aggregations": {
"subitem_name": {
"terms": {
"field": "items.subitems.name"
}
},
"my_rev_agg": {
"reverse_nested": {}
}
}
}
}
}
}
}
这将返回正确排序的子项桶:
{
"took": 0,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 9,
"max_score": 0.0,
"hits": []
},
"aggregations": {
"my_nested_agg": {
"doc_count": 19,
"subitem_id": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 3,
"doc_count": 6,
"my_rev_agg": {
"doc_count": 6
},
"subitem_name": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "Subitem #3",
"doc_count": 6
}
]
}
},
{
"key": 2,
"doc_count": 11,
"my_rev_agg": {
"doc_count": 5
},
"subitem_name": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "Subitem #2",
"doc_count": 11
}
]
}
},
{
"key": 1,
"doc_count": 1,
"my_rev_agg": {
"doc_count": 1
},
"subitem_name": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "Subitem #1",
"doc_count": 1
}
]
}
},
{
"key": 5,
"doc_count": 1,
"my_rev_agg": {
"doc_count": 1
},
"subitem_name": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "Subitem #5",
"doc_count": 1
}
]
}
}
]
}
}
}
}
我想要实现的是通过唯一对(城市、州)进行聚合。根据Elasticsearch文档,术语聚合不支持从同一文档中的多个字段收集术语。因此,我创建了一个嵌套的agg,如下所示: 作为这种聚合的结果,我得到了这样的响应: 问题是如何获得按最深doc\u计数排序的结果? 预期的有序列表应如下所示: 内华达州拉斯维加斯(5840) 杰克逊维尔,佛罗里达州(2986) 北卡罗来纳州杰克逊维尔(1962) 亚利桑
如何聚合一个值在嵌套在Elasticsearch嵌套位置?我对一个嵌套对象没有问题,但在嵌套对象内的嵌套我感到困惑... 样本数据: 欲望结果: 在索引映射中,我将cat_a和条目字段的类型设置为嵌套,当我从工具字段查询聚合时,在cat_a的根(级别1)中没有问题,并且可以工作,但是在聚合中在rx_a(这是在第2级)我不能检索结果,它或空或显示错误,因为我的错误查询。 查询级别1 agg: 如何处
我试图在c#中运行聚合查询(使用nest 5),但我不知道我得到了多少聚合作为输入以及聚合类型是什么。 例如,一个查询是:{"aggs":{"type_count":{"术语":{"field":"type"}}}} 其他查询将是:{“aggs”:{“type\u count”:{“terms”:{“field”:“type”}},“salary\u count”:{“field”:“salary
我是ElasticSearch的新手,我目前正在尝试编写一个涉及聚合的ElasticSearch查询,它将为我获取涉及某些字段的前5个桶(查询中涉及的字段数量是动态的,它可以在2个字段和5个字段之间的任何位置)。 我面临的问题是,团队当前的ElasticSearch版本不支持多术语,因此我恢复了复合聚合,将top_hits作为子聚合器,这样我就可以获得前5个桶。然而,我没有根据doc_count/
在这里,我得到了错误: “无效的术语聚合顺序路径[price>price>price.max]。术语桶只能在子聚合器路径上排序,该路径由路径中的零个或多个单桶聚合和路径末尾的最终单桶或度量聚合组成。子路径[price]指向非单桶聚合” 如果我按持续时间聚合排序,查询可以正常工作,如 那么,有什么方法可以通过嵌套字段上的嵌套聚合来排序聚合吗?
问题内容: 我想获得一个请求数据来构建这样的东西: 首先,我在Elasticsearch上进行映射,如下所示: 然后我添加对象,如下所示。在将许多属性。如果笔记本电脑有许多端口,则每个端口都是中的另一个阵列。 现在我想要这样的结果: 我接近解决问题(我下面的查询),但在第二级聚集我所有的值(例如,在“决议”我有,和)。我想有只,并具有其他关键值,对只,以及其他价值具有关键。 问题答案: 你需要改变