Home Articles

Elasticsearch术语聚合指定键列表

Asked
Viewed 1256 times
-2

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

例如,这是我的查询 . 我使用值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 Answers

  • 0

    尝试添加min_doc_count

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

    除非您专门搜索它,否则Elasticsearch不知道不存在的 term

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

Related