【问题标题】:Elasticsearch 1.7 Document type aggregationElasticsearch 1.7 文档类型聚合
【发布时间】:2016-01-11 12:19:36
【问题描述】:

我正在尝试在我们的日期存储中获取文档类型的聚合。查看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 的单一聚合中将它们全部取回吗?

【问题讨论】:

    标签: elasticsearch elasticsearch-aggregation


    【解决方案1】:

    您需要通过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"
          }
        }
      }
    }'
    

    【讨论】:

    • 第二个查询正是我要找的! FWIW,第一个查询返回错误。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2020-11-14
    • 1970-01-01
    • 2014-05-02
    • 2017-03-18
    • 2018-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多