Elasticsearch术语聚合指定键列表

时间:2016-08-01 11:38:27

标签: elasticsearch

无论如何都要指定术语聚合的键列表。

例如,这是我的查询。我使用值72,16,92

过滤成员字段
{
  "from": 0,
  "size": 0,
  "query": {
    "filtered": {
      "filter": {
        "and": [{
          "or": [{
            "term": {
              "member": 72
            }
          }, {
            "term": {
              "member": 16
            }
          }, {
            "term": {
              "member": 92
            }
          }]
        }]
      },
      "query": {
        "match_all": {}
      }
    }
  },
  "aggs": {
    "member": {
      "terms": {
        "field": "member",
        "order": {
          "_term": "asc"
        },
        "size": 100
      }
    }
  }
}

聚合结果仅返回16和72,因为member = 92与任何文档都不匹配。

{
  "took": 6,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 2360,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "member": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [{
        "key": 16,
        "doc_count": 2357
      }, {
        "key": 72,
        "doc_count": 3
      }]
    }
  }
}

我希望将聚合结果中的密钥作为doc_count 0获取,这在结果集

中不存在

2 个答案:

答案 0 :(得分:0)

尝试添加min_doc_count

"aggs": {
"member": {
  "terms": {
    "field": "member",
    "min_doc_count": 0,
    "order": {
      "_term": "asc"
    },
    "size": 100
  }
}

答案 1 :(得分:0)

Elasticsearch不了解不存在的term,除非您专门搜索它:

  "aggs": {
    "member72": {
      "filter": {
        "term": {
          "member": 72
        }
      }
    },
    "member16": {
      "filter": {
        "term": {
          "member": 16
        }
      }
    },
    "member92": {
      "filter": {
        "term": {
          "member": 92
        }
      }
    }
  }