curious1
curious1

Reputation: 14737

Elasticsearch: how to do filtered search and aggregation at the same time

I need to do a filtered search plus aggregation the following way, conceptually.

{
    "filtered" : {
        "query": { 
                "match_all" : {
                }
        },
        "aggregations": {
            "facets": {
                "terms": {
                    "field": "subject"
                }
            }
        },
        "filter" : {
            ...
        }
    }
}

The above query is not working because I got the following error message:

[filtered] query does not support [aggregations]]

I was trying to solve this problem. I found Filter Aggregation or Filters Aggregation online, but they do not seem to address my need.

Could someone show me the structure of the correct query that can achieve my goal?

Thanks and regards.

Upvotes: 0

Views: 249

Answers (1)

Vineeth Mohan
Vineeth Mohan

Reputation: 19283

The scope of aggregation is the query and all the filters in it. Which means if you give the aggregation along with the query in normal fashion , it should work.

{
  "query": {
    "filtered": {
      "query": {
        "match_all": {}
      },
      "filter": {}
    }
  },
  "aggregations": {
    "facets": {
      "terms": {
        "field": "subject"
      }
    }
  }
}

Upvotes: 1

Related Questions