Restrict ElasticSearch aggregation by type?

爱⌒轻易说出口 提交于 2019-12-10 15:07:03

问题


How do you perform an ElasticSearch aggregation for a specific type? I realize that you can specify the index and/or type in the request Url, but I want to perform aggregations on two distinct types.

Thank you!


回答1:


You could filter the aggregation by type, and then use sub-aggregations. For example:

{
  "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
          }
        }
      }
    }
  }
}



回答2:


If you can aggregate by an field in all types:

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"
                  }
                }
              }
            }
          }
        }
      }
    }'


来源:https://stackoverflow.com/questions/22622913/restrict-elasticsearch-aggregation-by-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!