【问题标题】:ElasticSearch Aggregation with Group By on Nested Property基于嵌套属性的 ElasticSearch 聚合
【发布时间】:2018-05-25 03:33:45
【问题描述】:

我正在尝试开发一个 ElasticSearch 聚合查询,该查询基于嵌套对象中的属性执行结果分箱。

product :
{
    productName : String,
    manufacturerInfo : {
        manufacturerName : String
    }
}

为索引设置了相应的映射。

我需要的查询应该是这样的:

{
    "size" : 0,
    "aggs" : {
        "manufacturers" : {
            "terms" : {
                "field" : "manufacturerInfo.manufacturerName.keyword"
            }
        },
        "aggs" : {
            "productNames" : {
                "terms" : {
                    "field" : "productName.keyword"
                }
            }
        }
    }
}

查询需要按嵌套属性的名称分组,但根对象的 bin 属性,这似乎在 ElasticSearch 中造成了一些麻烦。

当以下尝试前置时:

"aggs" : {
    "root" : {
        "nested" : {
            "path" : "manufacturerInfo"
         }
     },
     ...
 }

似乎此上下文不允许根据产品对象的根对项目进行分箱。

将与嵌套的manufacturerInfo 对象中指定的manufacturerNames 对应的productNames 分箱的正确语法是什么?

【问题讨论】:

    标签: elasticsearch nested aggregate


    【解决方案1】:

    嵌套上下文:

    "aggs" : {
        "root" : {
            "nested" : {
                "path" : "manufacturerInfo"
             }
         },
    

    必须与 reverse_nested {} 结合使用 - 嵌套 aggs 语法,在引用被分箱的父对象的属性之前插入:

        "aggs" : {
            "products" : {
                "reverse_nested" : {}, "aggs" : { "productNames" : { 
                "terms" : {
                    "field" : "productName.keyword"
                } } }
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-14
      • 2015-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-30
      • 2015-07-30
      相关资源
      最近更新 更多