在elasticsearch中嵌套聚合

yhuiod9q  于 7个月前  发布在  ElasticSearch
关注(0)|答案(2)|浏览(75)

如何在elasticsearch的嵌套位置中聚合一个值?我对一个嵌套对象没有问题,但在嵌套对象内部嵌套我感到困惑。

样本数据:

"cat_a": [
      {
        "position": "base",
        "tools": [
          "inside",
          "out"
        ],
        "entry": [
          {
            "tx_a": "inside",
            "rx_a": [
              "soft_1",
              "soft_2",
              "soft_3",
              "soft_4"
            ],
            "number": 0.018
          },
          {
            "tx_a": "out",
            "rx_a": [
              "soft_1",
              "soft_3",
              "soft_5",
              "soft_7"
            ],
            "number": 0.0001
          }
        ],
        "basic": true
      }
    ]

字符串

期望结果:

{
"aggregations": {
        "sample_agg": {
            "count": {
                "buckets": [
                    {
                        "key": "soft_1",
                        "doc_count": 2
                    },
                    {
                        "key": "soft_3",
                        "doc_count": 2
                    },
                    {
                        "key": "soft_2",
                        "doc_count": 1
                    }
                ]
            }
        }
    }
}


我的elasticsearch版本是6.2.3.在索引Map我设置类型的“cat_a”和“entry”字段为“nested”,当我从“tools”字段中查询一个聚合时,在“cat_a”的根(级别1)中没有问题,它正在工作,但在aggregation on catalog_a(级别2)中,我无法检索结果,它或空或显示错误,因为我的错误查询。

1级agg查询:

{
    "aggs" : {
        "sample_agg" : {
            "nested" : {
                "path" : "cat_a"
            },
            "aggs" : {
                "count" : { "terms" : { "field" : "cat_a.rx_a.keyword" ,"size":10} }
            }
        }
    }
}


如何在nested中嵌套?

6yt4nkrj

6yt4nkrj1#

Elasticsearch允许多层嵌套。假设您的Map定义了正确的嵌套,只需将路径更改为cat_a.entry,将字段更改为cat_a.entry. field_a. keyword。

svdrlsy4

svdrlsy42#

我刚刚构造并运行了一些“嵌套中嵌套”的聚合。
这使得语法更加困难。我更喜欢有更扁平的表达式。
恕我直言,如果我们需要知道不同级别的doc_count,或者如果我们有大量的嵌套聚合,它可能会很有用。
下面,
root-nested-1是一个“嵌套中嵌套”的聚合,
root-nested-2是相同的“扁平”聚合。

"root-nested-1": {
      "nested": {
        "path": "rootCollection"
      },
      "aggs": {
        "child-nested": {
          "nested": {
            "path": "rootCollection.childCollection"
          },
          "aggs": {
            "TRIAL": {
              "terms": {
                "field": "rootCollection.childCollection.trial",
                "include": [ "true" ]
              }
            }
          }
        }
      }
    },

个字符
如果我们比较我的真实的聚合的结果,没有太大的差异

"nested#root-nested-1" : {
      "doc_count" : 31,
      "nested#child-nested" : {
        "doc_count" : 35,
        "lterms#TRIAL" : {
          "doc_count_error_upper_bound" : 0,
          "sum_other_doc_count" : 0,
          "buckets" : [
            {
              "key" : 1,
              "key_as_string" : "true",
              "doc_count" : 13
            }
          ]
        }
      }
    }
"nested#root-nested-2" : {
      "doc_count" : 35,
      "lterms#TRIAL" : {
        "doc_count_error_upper_bound" : 0,
        "sum_other_doc_count" : 0,
        "buckets" : [
          {
            "key" : 1,
            "key_as_string" : "true",
            "doc_count" : 13
          }
        ]
      }
    },

的字符串

相关问题