Multiple filters and an aggregate in elasticsearch

前端 未结 4 655
广开言路
广开言路 2020-12-14 01:26

How can I use a filter in connection with an aggregate in elasticsearch?

The official documentation gives only trivial examples for filter and for aggregations and n

相关标签:
4条回答
  • 2020-12-14 01:45

    Put your filter in a filtered-query.

    The top-level filter is for filtering search hits only, and not facets/aggregations. It was renamed to post_filter in 1.0 due to this quite common confusion.

    Also, you might want to look into this post on why you often want to use bool and not and/or: http://www.elasticsearch.org/blog/all-about-elasticsearch-filter-bitsets/

    0 讨论(0)
  • 2020-12-14 01:49

    I ended up using a filter aggregation - not filtered query. So now I have 3 nested aggs elements.

    I also use bool filter instead of and as recommended by @alex-brasetvik because of http://www.elasticsearch.org/blog/all-about-elasticsearch-filter-bitsets/

    My final implementation:

    {
      "aggs": {
        "filtered": {
          "filter": {
            "bool": {
              "must": [
                {
                  "term": {
                    "_type": "logs"
                  }
                },
                {
                  "term": {
                    "dc": "eu-west-12"
                  }
                },
                {
                  "term": {
                    "status": "204"
                  }
                },
                {
                  "range": {
                    "@timestamp": {
                      "from": 1398176502000,
                      "to": 1400768502000
                    }
                  }
                }
              ]
            }
          },
          "aggs": {
            "time_histo": {
              "date_histogram": {
                "field": "@timestamp",
                "interval": "1h"
              },
              "aggs": {
                "name": {
                  "percentiles": {
                    "field": "upstream_response_time",
                    "percents": [
                      98.0
                    ]
                  }
                }
              }
            }
          }
        }
      },
      "size": 0
    }
    
    0 讨论(0)
  • 2020-12-14 01:49

    more on @geekQ 's answer: to support filter string with space char,for multipal term search,use below:

    {   "aggs": {
        "aggresults": {
          "filter": {
            "bool": {
              "must": [
                {
                  "match_phrase": {
                    "term_1": "some text with space 1"
                  }
                },
                {
                  "match_phrase": {
                    "term_2": "some text with also space 2"
                  }
                }
              ]
            }
          },
          "aggs" : {
                "all_term_3s" : {
                    "terms" : {
                        "field":"term_3.keyword",
                        "size" : 10000,
                        "order" : {
                            "_term" : "asc" 
                        }
                    }
               }
            }
        }   },   "size": 0 }
    
    0 讨论(0)
  • 2020-12-14 01:57

    Just for reference, as for the version 7.2, I tried with something as follows to achieve multiple filters for aggregation:

    • filter aggregation to filter for aggregation
    • use bool to set up the compound query
    POST movies/_search?size=0
    {
      "size": 0,
      "aggs": {
        "test": {
          "filter": {
            "bool": {
              "must": {
                "term": {
                  "genre": "action"
                }
              },
              "filter": {
                "range": {
                  "year": {
                    "gte": 1800,
                    "lte": 3000
                  }
                }
              }
            }
          },
          "aggs": {
            "year_hist": {
              "histogram": {
                "field": "year",
                "interval": 50
              }
            }
          }
        }
      }
    }
    
    0 讨论(0)
提交回复
热议问题