elasticsearch根据doc count过滤aggs

时间:2016-10-13 15:45:28

标签: elasticsearch

我有一个查询计算每个用户的图像数量:

GET images/_search
{
 "query": {
    "bool": {
      "must": [
      {
        "term": {
           "appID.raw": "myApp"
      }
    }
  ]
 }
},
"size": 0,
"aggs": {
  "perDeviceAggregation": {
    "terms": {
       "field": "deviceID"    
    }
  }
 }
}

它基本上工作正常,但我想排除少于200个图像的用户的所有聚合结果。我如何调整上面的查询来实现这一目标?

感谢。

2 个答案:

答案 0 :(得分:1)

您可以使用Minimum Document Count选项来实现此目的。

"aggs": {
      "perDeviceAggregation": {
         "terms": {
            "field": "deviceID",
            "min_doc_count": 200
         }
      }
   }

答案 1 :(得分:0)

使用query子句向您的术语聚合添加过滤器聚合。 Filter Aggregations

您可以将上述查询修改为如下所示。

{
    "query": {
        "bool": {
            "must": [
                {
                    "term": {
                        "appID.raw": "myApp"
                    }
                }
            ]
        }
    },
    "size": 0,
    "aggs": {
        "filtered_users_with_images_count": {
            "filter": {
                "term": {
                    "count": 200
                }
            },
            "aggs": {
                "perDeviceAggregation": {
                    "terms": {
                        "field": "deviceID"
                    }
                }
            }
        }
    }
}

您可以修改filtered_users_with_images_count中的过滤器,以匹配图片大于200的文档。

请考虑发布您的数据映射以及查询以支持您的问题。

相关问题