elasticsearch - 术语聚合上 doc_count 的范围过滤器

标签 elasticsearch

{
    "size": 0,
    "aggs": {
        "categories_agg": {
            "terms": {
                "field": "categories",
                "order": {
                    "_count": "desc"
                }
            }
        }
    }
}

为了获取特定字段的聚合,我使用了上面给出的查询。它工作正常并给出如下结果:

{
  "took": 10,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 77445,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "categories_agg": {
      "doc_count_error_upper_bound": 794,
      "sum_other_doc_count": 148316,
      "buckets": [
        {
          "key": "Restaurants",
          "doc_count": 25071
        },
        {
          "key": "Shopping",
          "doc_count": 11233
        },
        {
          "key": "Food",
          "doc_count": 9250
        },
        {
          "key": "Beauty & Spas",
          "doc_count": 6583
        },
        {
          "key": "Health & Medical",
          "doc_count": 5121
        },
        {
          "key": "Nightlife",
          "doc_count": 5088
        },
        {
          "key": "Home Services",
          "doc_count": 4785
        },
        {
          "key": "Bars",
          "doc_count": 4328
        },
        {
          "key": "Automotive",
          "doc_count": 4208
        },
        {
          "key": "Local Services",
          "doc_count": 3468
        }
      ]
    }
  }
}

有没有一种方法可以过滤聚合,以便在每个桶的 doc_count 上获取特定范围内的桶?

例如对 doc_count 使用范围过滤器,其中最大值是 25000,最小值是 5000 应该给我

{
  "took": 10,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 77445,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "categories_agg": {
      "doc_count_error_upper_bound": 794,
      "sum_other_doc_count": 148316,
      "buckets": [
        {
          "key": "Shopping",
          "doc_count": 11233
        },
        {
          "key": "Food",
          "doc_count": 9250
        },
        {
          "key": "Beauty & Spas",
          "doc_count": 6583
        },
        {
          "key": "Health & Medical",
          "doc_count": 5121
        },
        {
          "key": "Nightlife",
          "doc_count": 5088
        }
      ]
    }
  }
}

最佳答案

我通过 buckets_selector 解决了这个问题。 我们可以在脚本中过滤计数。

```
"aggs": {
    "categories_agg": {
      "terms": {
        "field": "cel_num",
        "size": 5000,
        "min_doc_count":1
      },
      "aggs": {
        "count_bucket_selector": {
          "bucket_selector": {
            "buckets_path": {
              "count": "_count"
            },
            "script": {
              "lang":"expression",
              "inline": "count>5000 && count <10000"
            }
          }
        }
      }
    }
  }
```

关于elasticsearch - 术语聚合上 doc_count 的范围过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37504179/

相关文章:

elasticsearch - 如何停止登录 Elasticsearch 中的节点?

python - 在python的Elasticsearch中批量插入时出现连接错误

ruby-on-rails - 如何使用 RSpec 测试 Elasticsearch 和 Searchkick

elasticsearch - 与Elasticsearch的比率

elasticsearch - 搜索具有不同字段的多个索引

ruby-on-rails - 带有Elasticsearch和Tire的灵活模型

Elasticsearch document_missing_exception

json - Elasticsearch-Sense-索引JSON文件?

elasticsearch - Elasticsearch-术语条件中的OR

.net - 如何使用 NEST 客户端将 Elasticsearch 配置为使用 AutoMap 类型作为动态映射对象的默认模板?