在弹性搜索中过滤存储桶键和doc_count

时间:2019-09-11 09:42:56

标签: elasticsearch

我有一个包含多个文档的索引。现在,我想在弹性搜索中编写一个查询,这将使我能够过滤存储桶键和doc_count

{
  "aggs": {
    "genres": {
      "terms": {
        "field": "event.keyword"
      }
    }
  }
}

"aggregations": {
"genres": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 33,
"buckets": [
{
"key": "eone",
"doc_count": 5
}
,
{
"key": "etwo",
"doc_count": 2
}

]
}
}

我想编写查询,通过该查询可以对键名和dpc计数应用过滤器。假设我想得到键为eone并且文档数为5的结果,那么我应该只得到匹配此critera的结果

1 个答案:

答案 0 :(得分:1)

您可以尝试使用min_doc_count,如下所示,

{
    "aggs": {
      "genres": {
        "terms": {
          "field": "event.keyword",
          "min_doc_count": 5
        }
      }
    }
  }

通过使用filtermin_doc_count

GET index_name/_search
  {
    "size": 0,
    "query": {
      "constant_score": {
        "filter": {
          "bool": {
            "must": [
              {
                "match": {
                  "event.keyword": "eone"
                }
              }
            ]
          }
        }
      }
    },
    "aggs": {
      "genres": {
        "terms": {
           "field": "event.keyword",
            "min_doc_count": 5
        }

      }
    }
  } 

OR ,同时使用includemin_doc_count,如下所示,

GET index_name/_search
  {
    "size": 0,
    "aggs": {
      "genres": {
        "terms": {
          "field": "event.keyword",
          "min_doc_count": 5,
          "include" : "eone"
        }
      }
    }
  }

查看更多信息: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#_minimum_document_count_4