【问题标题】:Filtering out top_hits subaggregation based on total documents根据总文档过滤掉 top_hits 子聚合
【发布时间】:2016-06-26 14:22:42
【问题描述】:

我正在使用 Elasticsearch GeoHash 网格聚合进行地图聚类。 该查询平均返回 100-200 个桶。 每个存储桶都使用 top_hits 聚合,我使用它为每个聚合集群返回 3 个文档。

问题是我想仅在父聚合(GeoHash)聚合不超过 3 个文档时返回 top_hits。

如果一个集群聚合了超过 3 个文档,我不希望 ES 为这个集群返回任何文档(因为我不会使用它们)。

我尝试使用Bucket Selector Aggregation,但未能构建正确的bucket_path。 我在与 top_hits 聚合相同的级别上使用存储桶选择器聚合。 存储桶的总文档数可在 top_hits.hits.total 获得,但我得到的是 reason=path not supported for [top_hits]: [hits.total]

这在弹性搜索中可能吗? 这对我来说很重要,因为在大多数查询中,只有一小部分存储桶的文档少于 3 个。但是即使对于 1000 个文档的集群,top hits 子聚合也总是返回前 3 个文档。 如果查询结果返回 200 个存储桶,其中只有 5 个在聚合

这是我查询的 aggs 部分:

"clusters": {
  "geohash_grid": {
    "field": "coordinates",
    "precision": 3
  },
  "aggs": {
    "top_hits": {
      "top_hits": {
        "size": 3
      }
    },
    "top_hits_filter": {
      "bucket_selector": {
        "buckets_path": {
          "total_hits": "top_hits._count" // tried top_hits.hits.total
        },
        "script": {
          "inline": "total_hits <= 3"
        }
      }
    }
  }
}

【问题讨论】:

    标签: elasticsearch elasticsearch-5 elasticsearch-aggregation


    【解决方案1】:

    试试这个@ilivewithian:

      "aggs": {
        "clusters": {
          "geohash_grid": {
            "field": "coordinates",
            "precision": 3
          },
          "aggs": {
            "top_hits": {
              "top_hits": {
                "size": 3
              }
            },
            "top_hits_filter": {
              "bucket_selector": {
                "buckets_path": {
                  "total_hits": "_count"
                },
                "script": {
                  "inline": "params.total_hits <= 3"
                }
              }
            }
          }
        }
      }
    

    【讨论】:

    • 谢谢,虽然对我来说有点晚了,因为这个问题我们已经停止使用 geohash 聚合。如果可行,将在 1 月份的某个时候看看,然后适当地标记答案。
    • 我刚刚发现这个小宝石可以帮助我解决问题!
    • 这不会返回(排除)总数 >3 的所有其他存储桶。我可以通过一个查询获得两者吗?所有桶和计数
    猜你喜欢
    • 2016-08-29
    • 1970-01-01
    • 1970-01-01
    • 2020-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多