当前位置: 首页 > 面试题库 >

深度嵌套类型的Elasticsearch聚合

尉迟鑫鹏
2023-03-14
问题内容

示例文档中有一个简化的文档。这对我理解非嵌套类型与嵌套类型的聚合差异很有帮助。但是,这种简化掩盖了进一步的复杂性,因此我不得不在这里扩展这个问题。

所以我的实际文件更接近以下内容:

"_source": {
    "keyword": "my keyword",
    "response": [
        {
            "results": [
                {
                    "items": [
                        {
                            "prop": [
                                {
                                    "item_property_1": ["A"],
                                }
                            ]
                            ( ... other properties )
                        },
                        {
                            "prop": [
                                {
                                    "item_property_1": ["B"],
                                }
                            ]
                            ( ... other properties )
                        },
                        ( ... other items )
                    ]
                }
            ],
            ( ... other properties )
        }
    ]
}

因此,我保留了,和的关键属性keyword,但隐藏了许多其他使情况复杂化的内容。首先,请注意,与引用的问题相比,有很多额外的嵌套:在根和“项目”之间,以及在“项目”和“
item_property_1”之间。此外,还请注意,属性和都是具有单个元素的数组。这很奇怪,但是事实就是这样:-)items``item_property_1``response``results

现在,这个问题与上面引用的问题不同的原因是,我尝试了接受的答案(确实适用于此处的示例),并且在这里不起作用。也就是说,如果我使用以下映射:

"items": {
    "type":"nested",
    "properties": {
        "prop": {
            "properties": {
                "item_property_1": {
                    "type": "string",
                    "index": "not_analyzed"
                },
            }
        }
    }
}

那么聚合将不起作用。它返回零命中。

稍后,我将进行编辑并提供可立即使用的批量插入示例。

编辑:好的,下面我显示三个查询,分别是:映射,批量插入和聚合(零命中)

映射("type":"nested"如先前回答的问题所示)

PUT /test2/_mapping/test3
{
    "test3": {
        "properties": {
            "keyword": {
                "type": "string",
                "index": "not_analyzed"
            },
            "response": {
                "properties": {
                    "results": {
                        "properties": {
                            "items": {
                                "type": "nested",
                                "properties": {
                                    "prop": {
                                        "properties": {
                                            "item_property_1": {
                                                "type": "string",
                                                "index": "not_analyzed"
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

批量投放:

PUT /test2/test3/_bulk
{ "index": {}}
{    "keyword": "my keyword",    "response": [        {            "results": [                {                    "items": [                        {                            "prop": [                                {"item_property_1": ["A"]}                            ]                        },                        {                            "prop": [                                {"item_property_1": ["B"]}                            ]                        },                        {                            "prop": [                                {"item_property_1": ["A"]}                            ]                        }                    ]                }            ]        }    ]}
{ "index": {}}
{    "keyword": "different keyword",    "response": [        {            "results": [                {                    "items": [                        {                            "prop": [                                {"item_property_1": ["A"]}                            ]                        },                        {                            "prop": [                                {"item_property_1": ["C"]}                            ]                        }                    ]                }            ]        }    ]}

汇总(零点击):

POST /test2/test3/_search
{
    "size":0,
    "aggregations": {
        "item_property_1_count": {
            "terms":{
                "field":"item_property_1"
            }
        }
    }
}

问题答案:

它与先前的答案并没有真正的不同。您需要做的就是稍微修改字段名称,以考虑其他嵌套。除此之外,映射中无需进行任何更改。请注意,此查询无需映射更改就可以工作,因为responseresults都是带有单个元素的数组,如果不是这种情况,它将涉及更多问题,并且需要映射更改和其他查询。

现在查询如下所示:

{
  "size": 0,
  "aggregations": {
    "by_keyword": {
      "terms": {
        "field": "keyword"
      },
      "aggs": {
        "prop_1_count": {
          "nested": {
            "path": "response.results.items"
          },
          "aggs": {
            "prop_1": {
              "terms": {
                "field": "response.results.items.prop.item_property_1"
              }
            }
          }
        }
      }
    }
  }
}

结果:

{
  ...
  "aggregations" : {
    "by_keyword" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [ {
        "key" : "different keyword",
        "doc_count" : 1,
        "prop_1_count" : {
          "doc_count" : 2,
          "prop_1" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [ {
              "key" : "A",
              "doc_count" : 1
            }, {
              "key" : "C",
              "doc_count" : 1
            } ]
          }
        }
      }, {
        "key" : "my keyword",
        "doc_count" : 1,
        "prop_1_count" : {
          "doc_count" : 3,
          "prop_1" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [ {
              "key" : "A",
              "doc_count" : 2
            }, {
              "key" : "B",
              "doc_count" : 1
            } ]
          }
        }
      } ]
    }
  }
}


 类似资料:
  • 如何聚合一个值在嵌套在Elasticsearch嵌套位置?我对一个嵌套对象没有问题,但在嵌套对象内的嵌套我感到困惑... 样本数据: 欲望结果: 在索引映射中,我将cat_a和条目字段的类型设置为嵌套,当我从工具字段查询聚合时,在cat_a的根(级别1)中没有问题,并且可以工作,但是在聚合中在rx_a(这是在第2级)我不能检索结果,它或空或显示错误,因为我的错误查询。 查询级别1 agg: 如何处

  • 我无法使elasticsearch聚合+筛选器处理嵌套字段。数据模式(相关部分)如下所示: 本质上,“RB”对象包含一个名为“project”的嵌套字段,该字段包含另外两个字段--“name”和“age”。我正在运行的查询: 该查询应该生成与日期筛选器匹配的前10个项目(project.name字段),按其年龄中值排序,忽略数据库中提及次数少于5次的项目。中位数应仅用于匹配筛选器(日期范围)的项目

  • 我试图在c#中运行聚合查询(使用nest 5),但我不知道我得到了多少聚合作为输入以及聚合类型是什么。 例如,一个查询是:{"aggs":{"type_count":{"术语":{"field":"type"}}}} 其他查询将是:{“aggs”:{“type\u count”:{“terms”:{“field”:“type”}},“salary\u count”:{“field”:“salary

  • 我的索引中有以下类型的文档,但由于深度嵌套方面,我找不到正确排序的方法。 文档示例: 我希望排序或提升在匹配时间,以便我可以得到排序的文档(asc/desc)与约束和内嵌套文档和内嵌套文档

  • 在这里,我得到了错误: “无效的术语聚合顺序路径[price>price>price.max]。术语桶只能在子聚合器路径上排序,该路径由路径中的零个或多个单桶聚合和路径末尾的最终单桶或度量聚合组成。子路径[price]指向非单桶聚合” 如果我按持续时间聚合排序,查询可以正常工作,如 那么,有什么方法可以通过嵌套字段上的嵌套聚合来排序聚合吗?

  • 问题内容: 我想获得一个请求数据来构建这样的东西: 首先,我在Elasticsearch上进行映射,如下所示: 然后我添加对象,如下所示。在将许多属性。如果笔记本电脑有许多端口,则每个端口都是中的另一个阵列。 现在我想要这样的结果: 我接近解决问题(我下面的查询),但在第二级聚集我所有的值(例如,在“决议”我有,和)。我想有只,并具有其他关键值,对只,以及其他价值具有关键。 问题答案: 你需要改变