# Getting a Parse Failure when trying to filter a terms aggregation

**URL:** <https://discuss.elastic.co/t/getting-a-parse-failure-when-trying-to-filter-a-terms-aggregation/28257>\
**Category:** Elasticsearch\
**Created:** [August 28, 2015, 4:22pm UTC](https://discuss.elastic.co/t/getting-a-parse-failure-when-trying-to-filter-a-terms-aggregation/28257 "2015-08-28T16:22:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![doug\_livesey](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/doug_livesey/32/2114_2.png) [@doug\_livesey](https://discuss.elastic.co/u/doug_livesey)\
**Post date:** [August 28, 2015, 4:22pm UTC](https://discuss.elastic.co/t/getting-a-parse-failure-when-trying-to-filter-a-terms-aggregation/28257/1 "2015-08-28T16:22:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![clement\_tourriere](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/clement_tourriere/32/479_2.png) [@clement\_tourriere](https://discuss.elastic.co/u/clement_tourriere)\
**Post date:** [August 28, 2015, 4:52pm UTC](https://discuss.elastic.co/t/getting-a-parse-failure-when-trying-to-filter-a-terms-aggregation/28257/2 "2015-08-28T16:52:12Z")

</div>

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](https://www.elastic.co/downloads/marvel)) to build your queries in development ;).

---

<div class="post-metadata">

**Author:** ![doug\_livesey](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/doug_livesey/32/2114_2.png) [@doug\_livesey](https://discuss.elastic.co/u/doug_livesey)\
**Post date:** [August 29, 2015, 8:17am UTC](https://discuss.elastic.co/t/getting-a-parse-failure-when-trying-to-filter-a-terms-aggregation/28257/3 "2015-08-29T08:17:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![doug\_livesey](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/doug_livesey/32/2114_2.png) [@doug\_livesey](https://discuss.elastic.co/u/doug_livesey)\
**Post date:** [August 29, 2015, 10:50am UTC](https://discuss.elastic.co/t/getting-a-parse-failure-when-trying-to-filter-a-terms-aggregation/28257/4 "2015-08-29T10:50:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 5, 2017, 11:53pm UTC](https://discuss.elastic.co/t/getting-a-parse-failure-when-trying-to-filter-a-terms-aggregation/28257/5 "2017-07-05T23:53:12Z")

</div>


