Elasticsearch 1.7 文档类型聚合

Elasticsearch 1.7 Document type aggregation

我正在尝试获取日期存储中文档类型的聚合。查看 1.7 Type Filter documentation,使用类型筛选器很简单。但是,我 运行 尝试提交该查询时遇到以下问题:

curl -XGET localhost:9200/my_index/_search?pretty -d '
{ "type":
  { "value" : "my_type" }
}'

结果:

"error" : "SearchPhaseExecutionException[Failed to execute phase [query], all shards failed;...

我成功了运行以下:

curl -XGET localhost:9200/my_index/_search?pretty -d '
{ 
  "aggs": {
    "type_a_total": {
      "filter": {
        "type": {
          "value": "type_a"
        }
      }
    }
  }
}'

curl -XGET localhost:9200/my_index/_search?pretty -d '
{ 
  "aggs": {
    "type_b_total": {
      "filter": {
        "type": {
          "value": "type_b"
        }
      }
    }
  }
}'

结果:

...
"aggregations" : {
  "type_a" : {
    "doc_count" : 123456789
  }
}
...
"aggregations" : {
  "type_b" : {
    "doc_count" : 987654321
  }
}
...

知道如何在基于 _type 的单个聚合中将它们全部恢复吗?

您需要通过 constant_score 查询或 filtered 查询(我的选择)将 type 过滤器包装在 query 元素中。

curl -XGET localhost:9200/my_index/_search?pretty -d '{
  "query": {
    "filtered": {
      "filter": {
        "type": {
          "value": "my_type"
        }
      }
    }
  }
}'

但是,如果您只是想获取每种类型的文档数量,您可以简单地在 _type 字段上使用 terms 聚合

curl -XGET localhost:9200/my_index/_search?pretty -d '{
  "size": 0,
  "aggs": {
    "all_types": {
      "terms": {
        "field": "_type"
      }
    }
  }
}'