【问题标题】:how to bucket empty and non empty fields in nested aggregation in elasticsearch?如何在elasticsearch的嵌套聚合中存储空字段和非空字段?
【发布时间】:2016-02-11 22:27:14
【问题描述】:

我在 elasticsearch 中有以下一组嵌套子聚合(field2 是 field1 的子聚合,field3 是 field2 的子聚合)。 然而事实证明,field3 的术语聚合不会存储没有 field3 的文档。

我的理解是,除了 field3 的术语查询之外,我还必须使用 Missing 子聚合查询来存储这些查询。

但我不确定如何将其添加到下面的查询中以存储两者。

{
  "size": 0,
  "aggregations": {
    "f1": {
      "terms": {
        "field": "field1",
        "size": 0,
        "order": {
          "_count": "asc"
        },
        "include": [
          "123"
        ]
      },
      "aggregations": {
        "field2": {
          "terms": {
            "field": "f2",
            "size": 0,
            "order": {
              "_count": "asc"
            },
            "include": [
              "tr"
            ]
          },
          "aggregations": {
            "field3": {
              "terms": {
                "field": "f3",
                "order": {
                  "_count": "asc"
                },
                "size": 0
              },
              "aggregations": {
                "aggTopHits": {
                  "top_hits": {
                    "size": 1
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

【问题讨论】:

    标签: elasticsearch elasticsearch-aggregation


    【解决方案1】:

    在 2.1.2 及更高版本中,您可以使用missing parameter of the terms aggregation,它允许您为缺少该字段的文档指定默认值。 (仅供参考,missing 参数从 2.0 开始可用,但 there was a bug 阻止它处理子聚合,这就是您在此处使用它的方式。)

         ...
         "aggregations": {
            "field3": {
              "terms": {
                "field": "f3",
                "order": {
                  "_count": "asc"
                },
                "size": 0,
                "missing": "n/a"     <----- provide a default here
              },
              "aggregations": {
                "aggTopHits": {
                  "top_hits": {
                    "size": 1
                  }
                }
              }
            }
          }
    

    但是,如果您使用的是 2.x 之前的 ES 集群,则可以使用与 field3 聚合相同深度的 missing aggregation 来存储缺少“f3”的文档,如下所示:

         ...
         "aggregations": {
            "field3": {
              "terms": {
                "field": "f3",
                "order": {
                  "_count": "asc"
                },
                "size": 0
              },
              "aggregations": {
                "aggTopHits": {
                  "top_hits": {
                    "size": 1
                  }
                }
              }
            },
            "missing_field3": {
              "missing" : {
                "field": "f3"
              },
              "aggregations": {
                "aggTopMissingHit": {
                  "top_hits": {
                    "size": 1
                  }
                }
              }
            }
          }
    

    【讨论】:

    • 我使用的是 2.1.1 版本,但不幸的是,较新的方法在内部级别上不起作用,请参阅此问题 github.com/elastic/elasticsearch/issues/14882
    • 好电话。我在 2.2 集群上进行测试,它按预期工作。我不知道 2.1.1 及之前版本中的错误。我已经更新了我的答案,以澄清 missing 选项在哪个版本中有效,并包含您提供的错误详细信息的链接。
    猜你喜欢
    • 2015-07-30
    • 2018-10-05
    • 2023-04-03
    • 2021-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-01
    • 2023-03-03
    相关资源
    最近更新 更多