Getting a Parse Failure when trying to filter a terms aggregation

Hi -- I hope this is the correct place to ask this. I have a whole bunch of items indexed, and amongst other fields, they have a section_id and a price.
I'm trying to return an aggregation on a query of a count of all section_ids, but only where the price is below 50.00
To do this, my query looks like this:

{
  "query": { "match_all": {} },
  "aggs": {
    "section_ids": {
      "filter": {
        "range": {
          "price": { "to": 50.00 }
        }
      },
      "aggs": {
        "terms": { "field": "section_id" }
      }
    }
  }
}

However, this gives me this error:

{
  "error" : "SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {[xxxxxxxxxxxxxxxxxxxxxx][production_items][0]: RemoteTransportException[[balin-1][inet[/xx.xx.xx.xx:9300]][indices:data/read/search[phase/query]]]; nested: SearchParseException[[production_items][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\"aggs\":{\"section_ids\":{\"filter\":{\"range\":{\"price\":{\"to\":50.0}}},\"aggs\":{\"terms\":{\"field\":\"section_id\"}}}},\"query\":{\"match_all\":{}}}]]]; nested: SearchParseException[[production_items][0]: from[-1],size[-1]: Parse Failure [Expected [START_OBJECT] under [field], but got a [VALUE_STRING] in [terms]]]; }{[xxxxxxxxxxxxxxxxxxxxxx][production_items][1]: RemoteTransportException[[balin-3][inet[/xx.xx.xx.xx:9300]][indices:data/read/search[phase/query]]]; nested: SearchParseException[[production_items][1]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\"aggs\":{\"section_ids\":{\"filter\":{\"range\":{\"price\":{\"to\":50.0}}},\"aggs\":{\"terms\":{\"field\":\"section_id\"}}}},\"query\":{\"match_all\":{}}}]]]; nested: SearchParseException[[production_items][1]: from[-1],size[-1]: Parse Failure [Expected [START_OBJECT] under [field], but got a [VALUE_STRING] in [terms]]]; }]",
  "status" : 400
}

I've been trying to get this to work all day, but have been drawing a blank. As far as I can tell from the documentation on filtering an aggregation, this should work. But it obviously isn't.
If anyone could offer me any advice on how to fix this, I'd be really grateful.
Thanks in advance for any & all help,
Doug.

1 Like

Hi doug,

A name is missing for your terms aggregation.

A correct query :

{
  "aggs": {
    "section_ids": {
      "filter": {
        "range": {
          "price": { "to": 50.00 }
        }
      },
      "aggs": {
        "filtered_section_ids": {
           "terms": { "field": "section_id" }
        }
      }
    }
  }
}

You can use sense (marvel plugin : https://www.elastic.co/downloads/marvel) to build your queries in development ;).

3 Likes

@clement_tourriere -- thankyou so much for that! I shall give it a try as soon as I'm back with my dev machine!

Yes, that did it -- again, thankyou so much!