【问题标题】:Restrict ElasticSearch aggregation by type?按类型限制 ElasticSearch 聚合?
【发布时间】:2014-05-02 14:34:24
【问题描述】:

如何为特定类型执行 ElasticSearch 聚合?我意识到您可以在请求 URL 中指定索引和/或类型,但我想对两种不同的类型执行聚合。

谢谢!

【问题讨论】:

    标签: types elasticsearch aggregation


    【解决方案1】:

    您可以按类型过滤聚合,然后使用子聚合。例如:

    {
      "aggs": {
        "Test 1": {
          "filter": {
            "type": {
              "value": "type1"
            }
          },
          "aggs": {
            "agg_name": {
              "terms": {
                "field": "field1",
                "size": 10
              }
            }
          }
        },
        "Test 2": {
          "filter": {
            "type": {
              "value": "type2"
            }
          },
          "aggs": {
            "agg_name": {
              "terms": {
                "field": "field2",
                "size": 10
              }
            }
          }
        }
      }
    }
    

    【讨论】:

    • 这应该发布到your_server_url/index/_search(网址中没有类型)
    【解决方案2】:

    如果你可以按一个字段聚合所有类型:

    curl -XGET 'localhost:9200/index/type1,type2,type3,typeN/_search/?pretty=true' -d '{
          "query": {
            "filtered": {
              "query": {
                "match_all": {}
              }
            }
          },
          "size": 0,
          "aggs": {
            "field_aggs": {
              "terms": {
                "size": 0,
                "field": "field_common_on_all_types"
              },
              "aggs": {
                "testing": {
                  "terms": {
                    "field": "_type"
                  },
                  "aggs": {
                    "testing": {
                      "date_histogram": {
                        "field": "date_start",
                        "interval": "month",
                        "format": "yyyy-MM-dd HH:mm:ss"
                      }
                    }
                  }
                }
              }
            }
          }
        }'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-07
      • 1970-01-01
      • 2022-12-11
      • 1970-01-01
      相关资源
      最近更新 更多