# A query is not working, and I think it should

**URL:** https://discuss.elastic.co/t/a-query-is-not-working-and-i-think-it-should/2852
**Category:** Elasticsearch
**Created:** [March 18, 2010, 11:56am UTC](https://discuss.elastic.co/t/a-query-is-not-working-and-i-think-it-should/2852 "2010-03-18T11:56:20Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![xavier\_lacot](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/xavier_lacot/32/3354_2.png) [@xavier\_lacot](https://discuss.elastic.co/u/xavier_lacot)
#### Post date: [March 18, 2010, 11:56am UTC](https://discuss.elastic.co/t/a-query-is-not-working-and-i-think-it-should/2852/1 "2010-03-18T11:56:20Z")

</div>

Hello,

I discovered elasticsearch a few weeks ago and have found some time to  
give it a try. I tried yesterday to use it to index some data that I  
have in a database, and query this data using some constraints.

Say I have a database of products :

Products

- id : integer
- name : string
- date : date
- price : float
- items\_available : integer

I created the index "testindex" using the mapping API, here is what I  
find in the logs :

Put mapping [product] with source [{  
"product" : {  
"type" : "object",  
"dynamic" : true,  
"enabled" : true,  
"pathType" : "full",  
"dateFormats" : ["dateOptionalTime"],  
"boostField" : {  
"name" : "\_boost"  
},  
"properties" : {  
"id" : {  
"type" : "integer",  
"indexName" : "id",  
"index" : "not\_analyzed",  
"store" : "yes",  
"termVector" : "no",  
"boost" : 1.0,  
"omitNorms" : true,  
"omitTermFreqAndPositions" : true,  
"precisionStep" : 4  
},  
"price" : {  
"type" : "float",  
"indexName" : "price",  
"index" : "not\_analyzed",  
"store" : "yes",  
"termVector" : "no",  
"boost" : 1.0,  
"omitNorms" : true,  
"omitTermFreqAndPositions" : true,  
"precisionStep" : 4  
},  
"items\_available" : {  
"type" : "integer",  
"indexName" : "items\_available",  
"index" : "not\_analyzed",  
"store" : "yes",  
"termVector" : "no",  
"boost" : 1.0,  
"omitNorms" : true,  
"omitTermFreqAndPositions" : true,  
"precisionStep" : 4  
},  
"name" : {  
"type" : "string",  
"indexName" : "name",  
"index" : "analyzed",  
"store" : "yes",  
"termVector" : "no",  
"boost" : 1.0,  
"omitNorms" : false,  
"omitTermFreqAndPositions" : false  
},  
"date" : {  
"type" : "date",  
"indexName" : "date",  
"index" : "not\_analyzed",  
"store" : "yes",  
"termVector" : "no",  
"boost" : 1.0,  
"omitNorms" : true,  
"omitTermFreqAndPositions" : true,  
"precisionStep" : 4,  
"format" : "YYYY-MM-dd"  
}  
}  
}  
}]

This is what I expect, so until here everything is fine. I index some  
products :

curl -XPUT '[http://localhost:9200/testindex/product/1](http://localhost:9200/testindex/product/1)' -d  
'{"name":"product1","date":"2010-03-01","price":12.7,"items\_available":  
2}'  
curl -XPUT '[http://localhost:9200/testindex/product/2](http://localhost:9200/testindex/product/2)' -d  
'{"name":"product2","date":"2010-03-01","price":30.2,"items\_available":  
7}'  
curl -XPUT '[http://localhost:9200/testindex/product/3](http://localhost:9200/testindex/product/3)' -d  
'{"name":"product3","date":"2010-03-01","price":50,"items\_available":  
6}'  
curl -XPUT '[http://localhost:9200/testindex/product/4](http://localhost:9200/testindex/product/4)' -d  
'{"name":"product4","date":"2010-03-01","price":69.2,"items\_available":  
5}'  
curl -XPUT '[http://localhost:9200/testindex/product/5](http://localhost:9200/testindex/product/5)' -d  
'{"name":"product5","date":"2010-03-01","price":85.4,"items\_available":  
10}'  
curl -XPUT '[http://localhost:9200/testindex/product/6](http://localhost:9200/testindex/product/6)' -d  
'{"name":"product6","date":"2010-03-01","price":100,"items\_available":  
1}'

If I try to retrieve the products in a certain price range, it works  
well :

curl -XGET '[http://localhost:9200/testindex/product/\_search](http://localhost:9200/testindex/product/_search)' -d  
'{"query":{"range":{"price":{"from":30,"to":  
60,"includeLower":true,"includeUpper":true}}}}'

My problem is when I try to retrieve the products in a certain price  
range and with a certain name. Here is what I do :  
curl -XGET '[http://localhost:9200/testindex/product/\_search](http://localhost:9200/testindex/product/_search)' -d  
'{"query":{"term":{"name":"product3"},"range":{"price":{"from":30,"to":  
60,"includeLower":true,"includeUpper":true}}}}'

This returns an error :  
{"\_shards":{"total":5,"successful":0,"failed":5,"failures":  
[{"index":"testindex","shardId":  
3,"reason":"SearchParseException[[testindex][3]:  
query[name:product3],from[-1],size[-1]: Parse Failure [Failed to parse  
[{"query":{"term":{"name":"product3"},"range":{"price":  
{"from":30,"to":60,"includeLower":true,"includeUpper  
":true}}}}]]]; nested: SearchParseException[[testindex][3]:  
query[name:product3],from[-1],size[-1]: Parse Failure [No parser for  
element [price]]]; "},{"index":"testindex","shardId":  
1,"reason":"SearchParseException[[testindex][1]:  
query[name:product3],from[-1],size[-1]: Parse Failure [Failed to parse  
[{"query":{"term":{"name":"product3"},"range":{"price":  
{"from":30,"to":60,"includeLower":true,"includeUpper  
":true}}}}]]]; nested: SearchParseException[[testindex][1]:  
query[name:product3],from[-1],size[-1]: Parse Failure [No parser for  
element [price]]]; "},{"reason":"Unknown"},{"reason":"Unknown"},  
{"reason":"Unknown"}]},"hits":{"total":0,"hits":[]}}

My assumption is that it is not possible to mix term and range  
queries. Therefore, I tried to use a filteredQuery :  
curl -XGET '[http://localhost:9200/testindex/product/\_search](http://localhost:9200/testindex/product/_search)' -d  
'{"filteredQuery":{"query":{"term":{"name":"product3"}},"filter":  
{"range":{"price":{"from":30,"to":60}}}}}'

Here in a more "readable" form :  
{  
"filteredQuery" : {  
"query" : {  
"term" : { "name" : "product3" }  
},  
"filter" : {  
"range" : {  
"price" : { "from" : 30, "to" : 60 }  
}  
}  
}  
}

And this wuary also laucnehs an exception, even if it is quite close  
to the sample query in the documentation at  
[http://www.elasticsearch.com/docs/elasticsearch/rest\_api/query\_dsl/#Filtered\_Query](http://www.elasticsearch.com/docs/elasticsearch/rest_api/query_dsl/#Filtered_Query)

I there something I am doing wrong ? How should I write this (rather  
simple) query ?

Regards and kudos for elasticsearch !

Xavier

---

<div class="post-metadata">

### Author: ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)
#### Post date: [March 18, 2010, 2:11pm UTC](https://discuss.elastic.co/t/a-query-is-not-working-and-i-think-it-should/2852/2 "2010-03-18T14:11:36Z")

</div>

Regarding the first query that did not work, you should wrap the two queries  
in a bool query, with both queries (range and term) under a must clause.  
But, your second query, with the filtered one, is a much better fit since it  
will both perform better, and all you want is to filter based on a range,  
not take it into account when scoring.

The second query should have worked, can you maybe post the result that you  
get back (the exceptions you get) when you run it?

-shay.banon

On Thu, Mar 18, 2010 at 1:56 PM, xavier lacot [xavier@lacot.org](mailto:xavier@lacot.org) wrote:

> Hello,
> 
> I discovered elasticsearch a few weeks ago and have found some time to  
> give it a try. I tried yesterday to use it to index some data that I  
> have in a database, and query this data using some constraints.
> 
> Say I have a database of products :
> 
> Products
> 
> - id : integer
> - name : string
> - date : date
> - price : float
> - items\_available : integer
> 
> I created the index "testindex" using the mapping API, here is what I  
> find in the logs :
> 
> Put mapping [product] with source [{  
> "product" : {  
> "type" : "object",  
> "dynamic" : true,  
> "enabled" : true,  
> "pathType" : "full",  
> "dateFormats" : ["dateOptionalTime"],  
> "boostField" : {  
> "name" : "\_boost"  
> },  
> "properties" : {  
> "id" : {  
> "type" : "integer",  
> "indexName" : "id",  
> "index" : "not\_analyzed",  
> "store" : "yes",  
> "termVector" : "no",  
> "boost" : 1.0,  
> "omitNorms" : true,  
> "omitTermFreqAndPositions" : true,  
> "precisionStep" : 4  
> },  
> "price" : {  
> "type" : "float",  
> "indexName" : "price",  
> "index" : "not\_analyzed",  
> "store" : "yes",  
> "termVector" : "no",  
> "boost" : 1.0,  
> "omitNorms" : true,  
> "omitTermFreqAndPositions" : true,  
> "precisionStep" : 4  
> },  
> "items\_available" : {  
> "type" : "integer",  
> "indexName" : "items\_available",  
> "index" : "not\_analyzed",  
> "store" : "yes",  
> "termVector" : "no",  
> "boost" : 1.0,  
> "omitNorms" : true,  
> "omitTermFreqAndPositions" : true,  
> "precisionStep" : 4  
> },  
> "name" : {  
> "type" : "string",  
> "indexName" : "name",  
> "index" : "analyzed",  
> "store" : "yes",  
> "termVector" : "no",  
> "boost" : 1.0,  
> "omitNorms" : false,  
> "omitTermFreqAndPositions" : false  
> },  
> "date" : {  
> "type" : "date",  
> "indexName" : "date",  
> "index" : "not\_analyzed",  
> "store" : "yes",  
> "termVector" : "no",  
> "boost" : 1.0,  
> "omitNorms" : true,  
> "omitTermFreqAndPositions" : true,  
> "precisionStep" : 4,  
> "format" : "YYYY-MM-dd"  
> }  
> }  
> }  
> }]
> 
> This is what I expect, so until here everything is fine. I index some  
> products :
> 
> curl -XPUT '[http://localhost:9200/testindex/product/1](http://localhost:9200/testindex/product/1)' -d  
> '{"name":"product1","date":"2010-03-01","price":12.7,"items\_available":  
> 2}'  
> curl -XPUT '[http://localhost:9200/testindex/product/2](http://localhost:9200/testindex/product/2)' -d  
> '{"name":"product2","date":"2010-03-01","price":30.2,"items\_available":  
> 7}'  
> curl -XPUT '[http://localhost:9200/testindex/product/3](http://localhost:9200/testindex/product/3)' -d  
> '{"name":"product3","date":"2010-03-01","price":50,"items\_available":  
> 6}'  
> curl -XPUT '[http://localhost:9200/testindex/product/4](http://localhost:9200/testindex/product/4)' -d  
> '{"name":"product4","date":"2010-03-01","price":69.2,"items\_available":  
> 5}'  
> curl -XPUT '[http://localhost:9200/testindex/product/5](http://localhost:9200/testindex/product/5)' -d  
> '{"name":"product5","date":"2010-03-01","price":85.4,"items\_available":  
> 10}'  
> curl -XPUT '[http://localhost:9200/testindex/product/6](http://localhost:9200/testindex/product/6)' -d  
> '{"name":"product6","date":"2010-03-01","price":100,"items\_available":  
> 1}'
> 
> If I try to retrieve the products in a certain price range, it works  
> well :
> 
> curl -XGET '[http://localhost:9200/testindex/product/\_search](http://localhost:9200/testindex/product/_search)' -d  
> '{"query":{"range":{"price":{"from":30,"to":  
> 60,"includeLower":true,"includeUpper":true}}}}'
> 
> My problem is when I try to retrieve the products in a certain price  
> range and with a certain name. Here is what I do :  
> curl -XGET '[http://localhost:9200/testindex/product/\_search](http://localhost:9200/testindex/product/_search)' -d  
> '{"query":{"term":{"name":"product3"},"range":{"price":{"from":30,"to":  
> 60,"includeLower":true,"includeUpper":true}}}}'
> 
> This returns an error :  
> {"\_shards":{"total":5,"successful":0,"failed":5,"failures":  
> [{"index":"testindex","shardId":  
> 3,"reason":"SearchParseException[[testindex][3]:  
> query[name:product3],from[-1],size[-1]: Parse Failure [Failed to parse  
> [{"query":{"term":{"name":"product3"},"range":{"price":  
> {"from":30,"to":60,"includeLower":true,"includeUpper  
> ":true}}}}]]]; nested: SearchParseException[[testindex][3]:  
> query[name:product3],from[-1],size[-1]: Parse Failure [No parser for  
> element [price]]]; "},{"index":"testindex","shardId":  
> 1,"reason":"SearchParseException[[testindex][1]:  
> query[name:product3],from[-1],size[-1]: Parse Failure [Failed to parse  
> [{"query":{"term":{"name":"product3"},"range":{"price":  
> {"from":30,"to":60,"includeLower":true,"includeUpper  
> ":true}}}}]]]; nested: SearchParseException[[testindex][1]:  
> query[name:product3],from[-1],size[-1]: Parse Failure [No parser for  
> element [price]]]; "},{"reason":"Unknown"},{"reason":"Unknown"},  
> {"reason":"Unknown"}]},"hits":{"total":0,"hits":}}
> 
> My assumption is that it is not possible to mix term and range  
> queries. Therefore, I tried to use a filteredQuery :  
> curl -XGET '[http://localhost:9200/testindex/product/\_search](http://localhost:9200/testindex/product/_search)' -d  
> '{"filteredQuery":{"query":{"term":{"name":"product3"}},"filter":  
> {"range":{"price":{"from":30,"to":60}}}}}'
> 
> Here in a more "readable" form :  
> {  
> "filteredQuery" : {  
> "query" : {  
> "term" : { "name" : "product3" }  
> },  
> "filter" : {  
> "range" : {  
> "price" : { "from" : 30, "to" : 60 }  
> }  
> }  
> }  
> }
> 
> And this wuary also laucnehs an exception, even if it is quite close  
> to the sample query in the documentation at
> 
> [http://www.elasticsearch.com/docs/elasticsearch/rest\_api/query\_dsl/#Filtered\_Query](http://www.elasticsearch.com/docs/elasticsearch/rest_api/query_dsl/#Filtered_Query)
> 
> I there something I am doing wrong ? How should I write this (rather  
> simple) query ?
> 
> Regards and kudos for elasticsearch !
> 
> Xavier

---

<div class="post-metadata">

### Author: ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)
#### Post date: [March 18, 2010, 2:16pm UTC](https://discuss.elastic.co/t/a-query-is-not-working-and-i-think-it-should/2852/3 "2010-03-18T14:16:29Z")

</div>

> Here in a more "readable" form :  
> {  
> "filteredQuery" : {  
> "query" : {  
> "term" : { "name" : "product3" }  
> },  
> "filter" : {  
> "range" : {  
> "price" : { "from" : 30, "to" : 60 }  
> }  
> }  
> }  
> }

I think this should be:

{ query:  
{  
"filteredQuery" : {  
"query" : {  
"term" : { "name" : "product3" }  
},  
"filter" : {  
"range" : {  
"price" : { "from" : 30, "to" : 60 }  
}  
}  
}  
}  
}

See my DSL reference here:

> **[ElasticSearch::QueryDSL - metacpan.org](https://metacpan.org/release/DRTECH/ElasticSearch-0.05/view/lib/ElasticSearch/QueryDSL.pod)**
>
> An API for communicating with ElasticSearch

clint

--  
Web Announcements Limited is a company registered in England and Wales,  
with company number 05608868, with registered address at 10 Arvon Road,  
London, N5 1PR.

---

<div class="post-metadata">

### Author: ![xavier\_lacot](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/xavier_lacot/32/3354_2.png) [@xavier\_lacot](https://discuss.elastic.co/u/xavier_lacot)
#### Post date: [March 18, 2010, 2:16pm UTC](https://discuss.elastic.co/t/a-query-is-not-working-and-i-think-it-should/2852/4 "2010-03-18T14:16:50Z")

</div>

Hi Shay,

Thanks for your answer.

here is the query and the result :

curl -XGET '[http://localhost:9200/testindex/product/\_search](http://localhost:9200/testindex/product/_search)' -d  
'{"filteredQuery":{"query":{"term":{"name":"product3"}},"filter":{"range":{"price":{"from":30,"to":60}}}}}'

{"\_shards":{"total":5,"successful":0,"failed":5,"failures":[{"index":"testindex","shardId":3,"reason":"SearchParseException[[testindex][3]:  
query[null],from[-1],size[-1]: Parse Failure [Failed to parse  
[{"filteredQuery":{"query":{"term":{"name":"product3"}},"filter":{"range":{"price":{"from":30,"to":60}}}}}]]];  
nested: SearchParseException[[testindex][3]:  
query[null],from[-1],size[-1]: Parse Failure [No parser for element  
[filteredQuery]]];  
"},{"index":"testindex","shardId":1,"reason":"SearchParseException[[testindex][1]:  
query[null],from[-1],size[-1]: Parse Failure [Failed to parse  
[{"filteredQuery":{"query":{"term":{"name":"product3"}},"filter":{"range":{"price":{"from":30,"to":60}}}}}]]];  
nested: SearchParseException[[testindex][1]:  
query[null],from[-1],size[-1]: Parse Failure [No parser for element  
[filteredQuery]]];  
"},{"reason":"Unknown"},{"reason":"Unknown"},{"reason":"Unknown"}]},"hits":{"total":0,"hits":}}

Best regards,

Xavier

2010/3/18 Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com):

> Regarding the first query that did not work, you should wrap the two queries  
> in a bool query, with both queries (range and term) under a must clause.  
> But, your second query, with the filtered one, is a much better fit since it  
> will both perform better, and all you want is to filter based on a range,  
> not take it into account when scoring.  
> The second query should have worked, can you maybe post the result that you  
> get back (the exceptions you get) when you run it?  
> -shay.banon
> 
> On Thu, Mar 18, 2010 at 1:56 PM, xavier lacot [xavier@lacot.org](mailto:xavier@lacot.org) wrote:
> 
> > Hello,
> > 
> > I discovered elasticsearch a few weeks ago and have found some time to  
> > give it a try. I tried yesterday to use it to index some data that I  
> > have in a database, and query this data using some constraints.
> > 
> > Say I have a database of products :
> > 
> > Products
> > 
> > - id : integer
> > - name : string
> > - date : date
> > - price : float
> > - items\_available : integer
> > 
> > I created the index "testindex" using the mapping API, here is what I  
> > find in the logs :
> > 
> > Put mapping [product] with source [{  
> > "product" : {  
> > "type" : "object",  
> > "dynamic" : true,  
> > "enabled" : true,  
> > "pathType" : "full",  
> > "dateFormats" : ["dateOptionalTime"],  
> > "boostField" : {  
> > "name" : "\_boost"  
> > },  
> > "properties" : {  
> > "id" : {  
> > "type" : "integer",  
> > "indexName" : "id",  
> > "index" : "not\_analyzed",  
> > "store" : "yes",  
> > "termVector" : "no",  
> > "boost" : 1.0,  
> > "omitNorms" : true,  
> > "omitTermFreqAndPositions" : true,  
> > "precisionStep" : 4  
> > },  
> > "price" : {  
> > "type" : "float",  
> > "indexName" : "price",  
> > "index" : "not\_analyzed",  
> > "store" : "yes",  
> > "termVector" : "no",  
> > "boost" : 1.0,  
> > "omitNorms" : true,  
> > "omitTermFreqAndPositions" : true,  
> > "precisionStep" : 4  
> > },  
> > "items\_available" : {  
> > "type" : "integer",  
> > "indexName" : "items\_available",  
> > "index" : "not\_analyzed",  
> > "store" : "yes",  
> > "termVector" : "no",  
> > "boost" : 1.0,  
> > "omitNorms" : true,  
> > "omitTermFreqAndPositions" : true,  
> > "precisionStep" : 4  
> > },  
> > "name" : {  
> > "type" : "string",  
> > "indexName" : "name",  
> > "index" : "analyzed",  
> > "store" : "yes",  
> > "termVector" : "no",  
> > "boost" : 1.0,  
> > "omitNorms" : false,  
> > "omitTermFreqAndPositions" : false  
> > },  
> > "date" : {  
> > "type" : "date",  
> > "indexName" : "date",  
> > "index" : "not\_analyzed",  
> > "store" : "yes",  
> > "termVector" : "no",  
> > "boost" : 1.0,  
> > "omitNorms" : true,  
> > "omitTermFreqAndPositions" : true,  
> > "precisionStep" : 4,  
> > "format" : "YYYY-MM-dd"  
> > }  
> > }  
> > }  
> > }]
> > 
> > This is what I expect, so until here everything is fine. I index some  
> > products :
> > 
> > curl -XPUT '[http://localhost:9200/testindex/product/1](http://localhost:9200/testindex/product/1)' -d  
> > '{"name":"product1","date":"2010-03-01","price":12.7,"items\_available":  
> > 2}'  
> > curl -XPUT '[http://localhost:9200/testindex/product/2](http://localhost:9200/testindex/product/2)' -d  
> > '{"name":"product2","date":"2010-03-01","price":30.2,"items\_available":  
> > 7}'  
> > curl -XPUT '[http://localhost:9200/testindex/product/3](http://localhost:9200/testindex/product/3)' -d  
> > '{"name":"product3","date":"2010-03-01","price":50,"items\_available":  
> > 6}'  
> > curl -XPUT '[http://localhost:9200/testindex/product/4](http://localhost:9200/testindex/product/4)' -d  
> > '{"name":"product4","date":"2010-03-01","price":69.2,"items\_available":  
> > 5}'  
> > curl -XPUT '[http://localhost:9200/testindex/product/5](http://localhost:9200/testindex/product/5)' -d  
> > '{"name":"product5","date":"2010-03-01","price":85.4,"items\_available":  
> > 10}'  
> > curl -XPUT '[http://localhost:9200/testindex/product/6](http://localhost:9200/testindex/product/6)' -d  
> > '{"name":"product6","date":"2010-03-01","price":100,"items\_available":  
> > 1}'
> > 
> > If I try to retrieve the products in a certain price range, it works  
> > well :
> > 
> > curl -XGET '[http://localhost:9200/testindex/product/\_search](http://localhost:9200/testindex/product/_search)' -d  
> > '{"query":{"range":{"price":{"from":30,"to":  
> > 60,"includeLower":true,"includeUpper":true}}}}'
> > 
> > My problem is when I try to retrieve the products in a certain price  
> > range and with a certain name. Here is what I do :  
> > curl -XGET '[http://localhost:9200/testindex/product/\_search](http://localhost:9200/testindex/product/_search)' -d  
> > '{"query":{"term":{"name":"product3"},"range":{"price":{"from":30,"to":  
> > 60,"includeLower":true,"includeUpper":true}}}}'
> > 
> > This returns an error :  
> > {"\_shards":{"total":5,"successful":0,"failed":5,"failures":  
> > [{"index":"testindex","shardId":  
> > 3,"reason":"SearchParseException[[testindex][3]:  
> > query[name:product3],from[-1],size[-1]: Parse Failure [Failed to parse  
> > [{"query":{"term":{"name":"product3"},"range":{"price":  
> > {"from":30,"to":60,"includeLower":true,"includeUpper  
> > ":true}}}}]]]; nested: SearchParseException[[testindex][3]:  
> > query[name:product3],from[-1],size[-1]: Parse Failure [No parser for  
> > element [price]]]; "},{"index":"testindex","shardId":  
> > 1,"reason":"SearchParseException[[testindex][1]:  
> > query[name:product3],from[-1],size[-1]: Parse Failure [Failed to parse  
> > [{"query":{"term":{"name":"product3"},"range":{"price":  
> > {"from":30,"to":60,"includeLower":true,"includeUpper  
> > ":true}}}}]]]; nested: SearchParseException[[testindex][1]:  
> > query[name:product3],from[-1],size[-1]: Parse Failure [No parser for  
> > element [price]]]; "},{"reason":"Unknown"},{"reason":"Unknown"},  
> > {"reason":"Unknown"}]},"hits":{"total":0,"hits":}}
> > 
> > My assumption is that it is not possible to mix term and range  
> > queries. Therefore, I tried to use a filteredQuery :  
> > curl -XGET '[http://localhost:9200/testindex/product/\_search](http://localhost:9200/testindex/product/_search)' -d  
> > '{"filteredQuery":{"query":{"term":{"name":"product3"}},"filter":  
> > {"range":{"price":{"from":30,"to":60}}}}}'
> > 
> > Here in a more "readable" form :  
> > {  
> > "filteredQuery" : {  
> > "query" : {  
> > "term" : { "name" : "product3" }  
> > },  
> > "filter" : {  
> > "range" : {  
> > "price" : { "from" : 30, "to" : 60 }  
> > }  
> > }  
> > }  
> > }
> > 
> > And this wuary also laucnehs an exception, even if it is quite close  
> > to the sample query in the documentation at
> > 
> > [http://www.elasticsearch.com/docs/elasticsearch/rest\_api/query\_dsl/#Filtered\_Query](http://www.elasticsearch.com/docs/elasticsearch/rest_api/query_dsl/#Filtered_Query)
> > 
> > I there something I am doing wrong ? How should I write this (rather  
> > simple) query ?
> > 
> > Regards and kudos for elasticsearch !
> > 
> > Xavier

--  
Xavier Lacot  
[xavier@lacot.org](mailto:xavier@lacot.org)

> **[Chez Xavier - Prenez vos places pour le Forum PHP 2012 !](https://lacot.org/)**
>
> Personal page of Xavier Lacot : articles, (open-source) code, photos, artwork

---

<div class="post-metadata">

### Author: ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)
#### Post date: [March 18, 2010, 2:17pm UTC](https://discuss.elastic.co/t/a-query-is-not-working-and-i-think-it-should/2852/5 "2010-03-18T14:17:14Z")

</div>

Right!, I missed it 🙂

On Thu, Mar 18, 2010 at 4:16 PM, Clinton Gormley [clinton@iannounce.co.uk](mailto:clinton@iannounce.co.uk)wrote:

> > Here in a more "readable" form :  
> > {  
> > "filteredQuery" : {  
> > "query" : {  
> > "term" : { "name" : "product3" }  
> > },  
> > "filter" : {  
> > "range" : {  
> > "price" : { "from" : 30, "to" : 60 }  
> > }  
> > }  
> > }  
> > }
> 
> I think this should be:
> 
> { query:  
> {  
> "filteredQuery" : {  
> "query" : {  
> "term" : { "name" : "product3" }  
> },  
> "filter" : {  
> "range" : {  
> "price" : { "from" : 30, "to" : 60 }  
> }  
> }  
> }  
> }  
> }
> 
> See my DSL reference here:
> 
> [ElasticSearch::QueryDSL - metacpan.org](http://search.cpan.org/~drtech/ElasticSearch-0.05/lib/ElasticSearch/QueryDSL.pod)
> 
> clint
> 
> --  
> Web Announcements Limited is a company registered in England and Wales,  
> with company number 05608868, with registered address at 10 Arvon Road,  
> London, N5 1PR.

---

<div class="post-metadata">

### Author: ![xavier\_lacot](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/xavier_lacot/32/3354_2.png) [@xavier\_lacot](https://discuss.elastic.co/u/xavier_lacot)
#### Post date: [March 18, 2010, 2:25pm UTC](https://discuss.elastic.co/t/a-query-is-not-working-and-i-think-it-should/2852/6 "2010-03-18T14:25:01Z")

</div>

Oh great! 🙂 Thanks both for the help !

Xavier

2010/3/18 Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com):

> Right!, I missed it 🙂
> 
> On Thu, Mar 18, 2010 at 4:16 PM, Clinton Gormley [clinton@iannounce.co.uk](mailto:clinton@iannounce.co.uk)  
> wrote:
> 
> > > Here in a more "readable" form :  
> > > {  
> > > "filteredQuery" : {  
> > > "query" : {  
> > > "term" : { "name" : "product3" }  
> > > },  
> > > "filter" : {  
> > > "range" : {  
> > > "price" : { "from" : 30, "to" : 60 }  
> > > }  
> > > }  
> > > }  
> > > }
> > 
> > I think this should be:
> > 
> > { query:  
> > {  
> > "filteredQuery" : {  
> > "query" : {  
> > "term" : { "name" : "product3" }  
> > },  
> > "filter" : {  
> > "range" : {  
> > "price" : { "from" : 30, "to" : 60 }  
> > }  
> > }  
> > }  
> > }  
> > }
> > 
> > See my DSL reference here:
> > 
> > [ElasticSearch::QueryDSL - metacpan.org](http://search.cpan.org/~drtech/ElasticSearch-0.05/lib/ElasticSearch/QueryDSL.pod)
> > 
> > clint
> > 
> > --  
> > Web Announcements Limited is a company registered in England and Wales,  
> > with company number 05608868, with registered address at 10 Arvon Road,  
> > London, N5 1PR.

--  
Xavier Lacot  
[xavier@lacot.org](mailto:xavier@lacot.org)

> **[Chez Xavier - Prenez vos places pour le Forum PHP 2012 !](https://lacot.org/)**
>
> Personal page of Xavier Lacot : articles, (open-source) code, photos, artwork

---

<div class="post-metadata">

### Author: ![xavier\_lacot](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/xavier_lacot/32/3354_2.png) [@xavier\_lacot](https://discuss.elastic.co/u/xavier_lacot)
#### Post date: [April 1, 2010, 6:31am UTC](https://discuss.elastic.co/t/a-query-is-not-working-and-i-think-it-should/2852/7 "2010-04-01T06:31:55Z")

</div>

Hello,

The last query proposed by Clinton worked well, as expected. But now, I want  
to make more complex queries, with filters on several terms.

For instance, I want to search products based on the product name, the brand  
name, the type of availability ("available" or "not available"), the city  
where the product is (so, a city\_name and eventually a zip\_code), and some  
other similar criteria. I tried for instance the following request, but it  
always returns an empty resultset, event if there should have been valid  
results. Here is what I tried :

{ query:  
{  
"filteredQuery" : {  
"query" : {  
"term" : { "name" : "product3" }  
},  
"filter" : {  
"bool" : {  
"must" : {  
"term" : { "zip\_code" : 12345, "brand" : "testbrand" }  
}  
}  
}  
}  
}  
}

--\> results are only filtered on zip code, not on brand.

{ query:  
{  
"filteredQuery" : {  
"query" : {  
"term" : { "name" : "product3" }  
},  
"filter" : {  
"bool" : {  
"must" : {  
"term" : { "brand" : "testbrand" },  
"term" : { "zip\_code" : 12345 }  
}  
}  
}  
}  
}  
}

-\> QueryParsingException[[myindexname] No json query parser registered for  
[zip\_code]]

How do I express a combination of filters? The example only shows  
restrictions with two filters, but I will have to build queries with 5 or  
more restrictions. Is there a trivial solution I have not seen in the  
documentation? I searched well but could not find examples for complex  
queries like these.

- Is t possible to have boolean filters with several disjoind "should"  
queries ? I want to express in the same query something like : "the city is  
\*\*\* OR the zip\_code is \*\*\*\*\*", and "the type of product is TYPE\_1 or the  
type of product is TYPE\_2 or the type of product is TYPE\_3". How would you  
write these?

Regards + bravos for ES, it's a pleasure to use it!

Xavier

2010/3/18 Xavier Lacot [xavier@lacot.org](mailto:xavier@lacot.org)

> Oh great! 🙂 Thanks both for the help !
> 
> Xavier
> 
> 2010/3/18 Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com):
> 
> > Right!, I missed it 🙂
> > 
> > On Thu, Mar 18, 2010 at 4:16 PM, Clinton Gormley \<  
> > [clinton@iannounce.co.uk](mailto:clinton@iannounce.co.uk)\>  
> > wrote:
> > 
> > > > Here in a more "readable" form :  
> > > > {  
> > > > "filteredQuery" : {  
> > > > "query" : {  
> > > > "term" : { "name" : "product3" }  
> > > > },  
> > > > "filter" : {  
> > > > "range" : {  
> > > > "price" : { "from" : 30, "to" : 60 }  
> > > > }  
> > > > }  
> > > > }  
> > > > }
> > > 
> > > I think this should be:
> > > 
> > > { query:  
> > > {  
> > > "filteredQuery" : {  
> > > "query" : {  
> > > "term" : { "name" : "product3" }  
> > > },  
> > > "filter" : {  
> > > "range" : {  
> > > "price" : { "from" : 30, "to" : 60 }  
> > > }  
> > > }  
> > > }  
> > > }  
> > > }
> > > 
> > > See my DSL reference here:
> 
> [ElasticSearch::QueryDSL - metacpan.org](http://search.cpan.org/~drtech/ElasticSearch-0.05/lib/ElasticSearch/QueryDSL.pod)[http://search.cpan.org/~drtech/ElasticSearch-0.05/lib/ElasticSearch/QueryDSL.pod](http://search.cpan.org/%7Edrtech/ElasticSearch-0.05/lib/ElasticSearch/QueryDSL.pod)
> 
> > > clint
> > > 
> > > --  
> > > Web Announcements Limited is a company registered in England and Wales,  
> > > with company number 05608868, with registered address at 10 Arvon Road,  
> > > London, N5 1PR.
> 
> --  
> Xavier Lacot  
> [xavier@lacot.org](mailto:xavier@lacot.org)  
> [http://lacot.org/](http://lacot.org/)

--  
Xavier Lacot  
[xavier@lacot.org](mailto:xavier@lacot.org)

> **[Chez Xavier - Prenez vos places pour le Forum PHP 2012 !](https://lacot.org/)**
>
> Personal page of Xavier Lacot : articles, (open-source) code, photos, artwork

---

<div class="post-metadata">

### Author: ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)
#### Post date: [April 1, 2010, 9:12am UTC](https://discuss.elastic.co/t/a-query-is-not-working-and-i-think-it-should/2852/8 "2010-04-01T09:12:49Z")

</div>

Hi,

Each clase in the bool filter can accept a (json) array of filters. Have  
a look here:  
[http://www.elasticsearch.com/docs/elasticsearch/rest\_api/query\_dsl/bool\_filter/](http://www.elasticsearch.com/docs/elasticsearch/rest_api/query_dsl/bool_filter/)  
in  
which the should accepts an array of two filters. You can use the same  
syntax on the must/mustNot clauses as well.

-shay.banon

On Thu, Apr 1, 2010 at 9:31 AM, Xavier Lacot [xavier@lacot.org](mailto:xavier@lacot.org) wrote:

> Hello,
> 
> The last query proposed by Clinton worked well, as expected. But now, I  
> want to make more complex queries, with filters on several terms.
> 
> For instance, I want to search products based on the product name, the  
> brand name, the type of availability ("available" or "not available"), the  
> city where the product is (so, a city\_name and eventually a zip\_code), and  
> some other similar criteria. I tried for instance the following request, but  
> it always returns an empty resultset, event if there should have been valid  
> results. Here is what I tried :
> 
> { query:  
> {  
> "filteredQuery" : {  
> "query" : {  
> "term" : { "name" : "product3" }  
> },  
> "filter" : {  
> "bool" : {  
> "must" : {  
> "term" : { "zip\_code" : 12345, "brand" : "testbrand" }  
> }  
> }  
> }  
> }  
> }  
> }
> 
> --\> results are only filtered on zip code, not on brand.
> 
> { query:  
> {  
> "filteredQuery" : {  
> "query" : {  
> "term" : { "name" : "product3" }  
> },  
> "filter" : {  
> "bool" : {  
> "must" : {  
> "term" : { "brand" : "testbrand" },  
> "term" : { "zip\_code" : 12345 }  
> }  
> }  
> }  
> }  
> }  
> }
> 
> -\> QueryParsingException[[myindexname] No json query parser registered for  
> [zip\_code]]
> 
> How do I express a combination of filters? The example only shows  
> restrictions with two filters, but I will have to build queries with 5 or  
> more restrictions. Is there a trivial solution I have not seen in the  
> documentation? I searched well but could not find examples for complex  
> queries like these.
> 
> - Is t possible to have boolean filters with several disjoind "should"  
> queries ? I want to express in the same query something like : "the city is  
> \*\*\* OR the zip\_code is \*\*\*\*\*", and "the type of product is TYPE\_1 or the  
> type of product is TYPE\_2 or the type of product is TYPE\_3". How would you  
> write these?
> 
> Regards + bravos for ES, it's a pleasure to use it!
> 
> Xavier
> 
> 2010/3/18 Xavier Lacot [xavier@lacot.org](mailto:xavier@lacot.org)
> 
> Oh great! 🙂 Thanks both for the help !
> 
> > Xavier
> > 
> > 2010/3/18 Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com):
> > 
> > > Right!, I missed it 🙂
> > > 
> > > On Thu, Mar 18, 2010 at 4:16 PM, Clinton Gormley \<  
> > > [clinton@iannounce.co.uk](mailto:clinton@iannounce.co.uk)\>  
> > > wrote:
> > > 
> > > > > Here in a more "readable" form :  
> > > > > {  
> > > > > "filteredQuery" : {  
> > > > > "query" : {  
> > > > > "term" : { "name" : "product3" }  
> > > > > },  
> > > > > "filter" : {  
> > > > > "range" : {  
> > > > > "price" : { "from" : 30, "to" : 60 }  
> > > > > }  
> > > > > }  
> > > > > }  
> > > > > }
> > > > 
> > > > I think this should be:
> > > > 
> > > > { query:  
> > > > {  
> > > > "filteredQuery" : {  
> > > > "query" : {  
> > > > "term" : { "name" : "product3" }  
> > > > },  
> > > > "filter" : {  
> > > > "range" : {  
> > > > "price" : { "from" : 30, "to" : 60 }  
> > > > }  
> > > > }  
> > > > }  
> > > > }  
> > > > }
> > > > 
> > > > See my DSL reference here:
> > 
> > [ElasticSearch::QueryDSL - metacpan.org](http://search.cpan.org/~drtech/ElasticSearch-0.05/lib/ElasticSearch/QueryDSL.pod)[http://search.cpan.org/~drtech/ElasticSearch-0.05/lib/ElasticSearch/QueryDSL.pod](http://search.cpan.org/%7Edrtech/ElasticSearch-0.05/lib/ElasticSearch/QueryDSL.pod)
> > 
> > > > clint
> > > > 
> > > > --  
> > > > Web Announcements Limited is a company registered in England and Wales,  
> > > > with company number 05608868, with registered address at 10 Arvon Road,  
> > > > London, N5 1PR.
> > 
> > --  
> > Xavier Lacot  
> > [xavier@lacot.org](mailto:xavier@lacot.org)  
> > [http://lacot.org/](http://lacot.org/)
> 
> --  
> Xavier Lacot  
> [xavier@lacot.org](mailto:xavier@lacot.org)  
> [http://lacot.org/](http://lacot.org/)

---

<div class="post-metadata">

### Author: ![xavier\_lacot](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/xavier_lacot/32/3354_2.png) [@xavier\_lacot](https://discuss.elastic.co/u/xavier_lacot)
#### Post date: [April 1, 2010, 9:54pm UTC](https://discuss.elastic.co/t/a-query-is-not-working-and-i-think-it-should/2852/9 "2010-04-01T21:54:38Z")

</div>

Hi Shay,

Thanks for the answer, I did not notice the array in the sample. Maybe  
should it be worth a sentence underlining this point?

There is however still a point that I am missing : is it possible to have  
boolean filters with several disjoint "should" queries ? I want to express  
in the same query something like: "the city is \*\*\* OR the zip\_code is \*\*\*\*\*"  
AND "the type of product is TYPE\_1 OR the type of product is TYPE\_2". How  
would you write this?

I tried the following, but it did not work:

"filter" : {  
"bool" : {  
"should" : [  
{  
"term" : { "city" : "city1" }  
},  
{  
"term" : { "zip\_code" : "012345" }  
}  
],  
"should" : [  
{  
"term" : { "product" : "product1" }  
},  
{  
"term" : { "product" : "product2" }  
}  
]  
}  
}

Then, I tried to write "should" boolean queries nested into a "must" one. It  
works as expected, but I am not sure if it is the most concise and efficient  
syntax?

"filter" : {  
"bool" : {  
"must" : [  
{  
"bool" : {  
"should" : [  
{  
"term" : { "city" : "city1" }  
},  
{  
"term" : { "zip\_code" : "012345" }  
}  
]  
}  
},  
{  
"bool" : {  
"should" : [  
{  
"term" : { "product" : "product1" }  
},  
{  
"term" : { "product" : "product2" }  
}  
]  
}  
}  
]  
}  
}

kudos + cheers,

Xavier

2010/4/1 Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com)

> Hi,
> 
> Each clase in the bool filter can accept a (json) array of filters. Have  
> a look here:  
> [http://www.elasticsearch.com/docs/elasticsearch/rest\_api/query\_dsl/bool\_filter/](http://www.elasticsearch.com/docs/elasticsearch/rest_api/query_dsl/bool_filter/) in  
> which the should accepts an array of two filters. You can use the same  
> syntax on the must/mustNot clauses as well.
> 
> -shay.banon
> 
> On Thu, Apr 1, 2010 at 9:31 AM, Xavier Lacot [xavier@lacot.org](mailto:xavier@lacot.org) wrote:
> 
> > Hello,
> > 
> > The last query proposed by Clinton worked well, as expected. But now, I  
> > want to make more complex queries, with filters on several terms.
> > 
> > For instance, I want to search products based on the product name, the  
> > brand name, the type of availability ("available" or "not available"), the  
> > city where the product is (so, a city\_name and eventually a zip\_code), and  
> > some other similar criteria. I tried for instance the following request, but  
> > it always returns an empty resultset, event if there should have been valid  
> > results. Here is what I tried :
> > 
> > { query:  
> > {  
> > "filteredQuery" : {  
> > "query" : {  
> > "term" : { "name" : "product3" }  
> > },  
> > "filter" : {  
> > "bool" : {  
> > "must" : {  
> > "term" : { "zip\_code" : 12345, "brand" : "testbrand" }  
> > }  
> > }  
> > }  
> > }  
> > }  
> > }
> > 
> > --\> results are only filtered on zip code, not on brand.
> > 
> > { query:  
> > {  
> > "filteredQuery" : {  
> > "query" : {  
> > "term" : { "name" : "product3" }  
> > },  
> > "filter" : {  
> > "bool" : {  
> > "must" : {  
> > "term" : { "brand" : "testbrand" },  
> > "term" : { "zip\_code" : 12345 }  
> > }  
> > }  
> > }  
> > }  
> > }  
> > }
> > 
> > -\> QueryParsingException[[myindexname] No json query parser registered for  
> > [zip\_code]]
> > 
> > How do I express a combination of filters? The example only shows  
> > restrictions with two filters, but I will have to build queries with 5 or  
> > more restrictions. Is there a trivial solution I have not seen in the  
> > documentation? I searched well but could not find examples for complex  
> > queries like these.
> > 
> > - Is t possible to have boolean filters with several disjoind "should"  
> > queries ? I want to express in the same query something like : "the city is  
> > \*\*\* OR the zip\_code is \*\*\*\*\*", and "the type of product is TYPE\_1 or the  
> > type of product is TYPE\_2 or the type of product is TYPE\_3". How would you  
> > write these?
> > 
> > Regards + bravos for ES, it's a pleasure to use it!
> > 
> > Xavier
> > 
> > 2010/3/18 Xavier Lacot [xavier@lacot.org](mailto:xavier@lacot.org)
> > 
> > Oh great! 🙂 Thanks both for the help !
> > 
> > > Xavier
> > > 
> > > 2010/3/18 Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com):
> > > 
> > > > Right!, I missed it 🙂
> > > > 
> > > > On Thu, Mar 18, 2010 at 4:16 PM, Clinton Gormley \<  
> > > > [clinton@iannounce.co.uk](mailto:clinton@iannounce.co.uk)\>  
> > > > wrote:
> > > > 
> > > > > > Here in a more "readable" form :  
> > > > > > {  
> > > > > > "filteredQuery" : {  
> > > > > > "query" : {  
> > > > > > "term" : { "name" : "product3" }  
> > > > > > },  
> > > > > > "filter" : {  
> > > > > > "range" : {  
> > > > > > "price" : { "from" : 30, "to" : 60 }  
> > > > > > }  
> > > > > > }  
> > > > > > }  
> > > > > > }
> > > > > 
> > > > > I think this should be:
> > > > > 
> > > > > { query:  
> > > > > {  
> > > > > "filteredQuery" : {  
> > > > > "query" : {  
> > > > > "term" : { "name" : "product3" }  
> > > > > },  
> > > > > "filter" : {  
> > > > > "range" : {  
> > > > > "price" : { "from" : 30, "to" : 60 }  
> > > > > }  
> > > > > }  
> > > > > }  
> > > > > }  
> > > > > }
> > > > > 
> > > > > See my DSL reference here:
> > > 
> > > [ElasticSearch::QueryDSL - metacpan.org](http://search.cpan.org/~drtech/ElasticSearch-0.05/lib/ElasticSearch/QueryDSL.pod)[http://search.cpan.org/~drtech/ElasticSearch-0.05/lib/ElasticSearch/QueryDSL.pod](http://search.cpan.org/%7Edrtech/ElasticSearch-0.05/lib/ElasticSearch/QueryDSL.pod)
> > > 
> > > > > clint
> > > > > 
> > > > > --  
> > > > > Web Announcements Limited is a company registered in England and  
> > > > > Wales,  
> > > > > with company number 05608868, with registered address at 10 Arvon  
> > > > > Road,  
> > > > > London, N5 1PR.
> > > 
> > > --  
> > > Xavier Lacot  
> > > [xavier@lacot.org](mailto:xavier@lacot.org)  
> > > [http://lacot.org/](http://lacot.org/)
> > 
> > --  
> > Xavier Lacot  
> > [xavier@lacot.org](mailto:xavier@lacot.org)  
> > [http://lacot.org/](http://lacot.org/)

--  
Xavier Lacot  
[xavier@lacot.org](mailto:xavier@lacot.org)

> **[Chez Xavier - Prenez vos places pour le Forum PHP 2012 !](https://lacot.org/)**
>
> Personal page of Xavier Lacot : articles, (open-source) code, photos, artwork

---

<div class="post-metadata">

### Author: ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)
#### Post date: [April 1, 2010, 10:03pm UTC](https://discuss.elastic.co/t/a-query-is-not-working-and-i-think-it-should/2852/10 "2010-04-01T22:03:12Z")

</div>

The should/must/mustNot elements in the bool filter accepts _any_ other  
filter, which can be another bool filter. So, you can construct a boolfilter  
of two must clauses, each containing another bool filter. Same thing, by the  
way, applied to the bool query.

-shay.banon

On Fri, Apr 2, 2010 at 12:54 AM, Xavier Lacot [xavier@lacot.org](mailto:xavier@lacot.org) wrote:

> Hi Shay,
> 
> Thanks for the answer, I did not notice the array in the sample. Maybe  
> should it be worth a sentence underlining this point?
> 
> There is however still a point that I am missing : is it possible to have  
> boolean filters with several disjoint "should" queries ? I want to express  
> in the same query something like: "the city is \*\*\* OR the zip\_code is \*\*\*\*\*"  
> AND "the type of product is TYPE\_1 OR the type of product is TYPE\_2". How  
> would you write this?
> 
> I tried the following, but it did not work:
> 
> "filter" : {  
> "bool" : {  
> "should" : [  
> {  
> "term" : { "city" : "city1" }  
> },  
> {  
> "term" : { "zip\_code" : "012345" }  
> }  
> ],  
> "should" : [  
> {  
> "term" : { "product" : "product1" }  
> },  
> {  
> "term" : { "product" : "product2" }  
> }  
> ]  
> }  
> }
> 
> Then, I tried to write "should" boolean queries nested into a "must" one.  
> It works as expected, but I am not sure if it is the most concise and  
> efficient syntax?
> 
> "filter" : {  
> "bool" : {  
> "must" : [  
> {  
> "bool" : {  
> "should" : [  
> {  
> "term" : { "city" : "city1" }  
> },  
> {  
> "term" : { "zip\_code" : "012345" }  
> }  
> ]  
> }  
> },  
> {  
> "bool" : {  
> "should" : [  
> {  
> "term" : { "product" : "product1" }  
> },  
> {  
> "term" : { "product" : "product2" }  
> }  
> ]  
> }  
> }  
> ]  
> }  
> }
> 
> kudos + cheers,
> 
> Xavier
> 
> 2010/4/1 Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com)
> 
> Hi,
> 
> > Each clase in the bool filter can accept a (json) array of filters.  
> > Have a look here:  
> > [http://www.elasticsearch.com/docs/elasticsearch/rest\_api/query\_dsl/bool\_filter/](http://www.elasticsearch.com/docs/elasticsearch/rest_api/query_dsl/bool_filter/) in  
> > which the should accepts an array of two filters. You can use the same  
> > syntax on the must/mustNot clauses as well.
> > 
> > -shay.banon
> > 
> > On Thu, Apr 1, 2010 at 9:31 AM, Xavier Lacot [xavier@lacot.org](mailto:xavier@lacot.org) wrote:
> > 
> > > Hello,
> > > 
> > > The last query proposed by Clinton worked well, as expected. But now, I  
> > > want to make more complex queries, with filters on several terms.
> > > 
> > > For instance, I want to search products based on the product name, the  
> > > brand name, the type of availability ("available" or "not available"), the  
> > > city where the product is (so, a city\_name and eventually a zip\_code), and  
> > > some other similar criteria. I tried for instance the following request, but  
> > > it always returns an empty resultset, event if there should have been valid  
> > > results. Here is what I tried :
> > > 
> > > { query:  
> > > {  
> > > "filteredQuery" : {  
> > > "query" : {  
> > > "term" : { "name" : "product3" }  
> > > },  
> > > "filter" : {  
> > > "bool" : {  
> > > "must" : {  
> > > "term" : { "zip\_code" : 12345, "brand" : "testbrand" }  
> > > }  
> > > }  
> > > }  
> > > }  
> > > }  
> > > }
> > > 
> > > --\> results are only filtered on zip code, not on brand.
> > > 
> > > { query:  
> > > {  
> > > "filteredQuery" : {  
> > > "query" : {  
> > > "term" : { "name" : "product3" }  
> > > },  
> > > "filter" : {  
> > > "bool" : {  
> > > "must" : {  
> > > "term" : { "brand" : "testbrand" },  
> > > "term" : { "zip\_code" : 12345 }  
> > > }  
> > > }  
> > > }  
> > > }  
> > > }  
> > > }
> > > 
> > > -\> QueryParsingException[[myindexname] No json query parser registered  
> > > for [zip\_code]]
> > > 
> > > How do I express a combination of filters? The example only shows  
> > > restrictions with two filters, but I will have to build queries with 5 or  
> > > more restrictions. Is there a trivial solution I have not seen in the  
> > > documentation? I searched well but could not find examples for complex  
> > > queries like these.
> > > 
> > > - Is t possible to have boolean filters with several disjoind "should"  
> > > queries ? I want to express in the same query something like : "the city is  
> > > \*\*\* OR the zip\_code is \*\*\*\*\*", and "the type of product is TYPE\_1 or the  
> > > type of product is TYPE\_2 or the type of product is TYPE\_3". How would you  
> > > write these?
> > > 
> > > Regards + bravos for ES, it's a pleasure to use it!
> > > 
> > > Xavier
> > > 
> > > 2010/3/18 Xavier Lacot [xavier@lacot.org](mailto:xavier@lacot.org)
> > > 
> > > Oh great! 🙂 Thanks both for the help !
> > > 
> > > > Xavier
> > > > 
> > > > 2010/3/18 Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com):
> > > > 
> > > > > Right!, I missed it 🙂
> > > > > 
> > > > > On Thu, Mar 18, 2010 at 4:16 PM, Clinton Gormley \<  
> > > > > [clinton@iannounce.co.uk](mailto:clinton@iannounce.co.uk)\>  
> > > > > wrote:
> > > > > 
> > > > > > > Here in a more "readable" form :  
> > > > > > > {  
> > > > > > > "filteredQuery" : {  
> > > > > > > "query" : {  
> > > > > > > "term" : { "name" : "product3" }  
> > > > > > > },  
> > > > > > > "filter" : {  
> > > > > > > "range" : {  
> > > > > > > "price" : { "from" : 30, "to" : 60 }  
> > > > > > > }  
> > > > > > > }  
> > > > > > > }  
> > > > > > > }
> > > > > > 
> > > > > > I think this should be:
> > > > > > 
> > > > > > { query:  
> > > > > > {  
> > > > > > "filteredQuery" : {  
> > > > > > "query" : {  
> > > > > > "term" : { "name" : "product3" }  
> > > > > > },  
> > > > > > "filter" : {  
> > > > > > "range" : {  
> > > > > > "price" : { "from" : 30, "to" : 60 }  
> > > > > > }  
> > > > > > }  
> > > > > > }  
> > > > > > }  
> > > > > > }
> > > > > > 
> > > > > > See my DSL reference here:
> > > > 
> > > > [ElasticSearch::QueryDSL - metacpan.org](http://search.cpan.org/~drtech/ElasticSearch-0.05/lib/ElasticSearch/QueryDSL.pod)[http://search.cpan.org/~drtech/ElasticSearch-0.05/lib/ElasticSearch/QueryDSL.pod](http://search.cpan.org/%7Edrtech/ElasticSearch-0.05/lib/ElasticSearch/QueryDSL.pod)
> > > > 
> > > > > > clint
> > > > > > 
> > > > > > --  
> > > > > > Web Announcements Limited is a company registered in England and  
> > > > > > Wales,  
> > > > > > with company number 05608868, with registered address at 10 Arvon  
> > > > > > Road,  
> > > > > > London, N5 1PR.
> > > > 
> > > > --  
> > > > Xavier Lacot  
> > > > [xavier@lacot.org](mailto:xavier@lacot.org)  
> > > > [http://lacot.org/](http://lacot.org/)
> > > 
> > > --  
> > > Xavier Lacot  
> > > [xavier@lacot.org](mailto:xavier@lacot.org)  
> > > [http://lacot.org/](http://lacot.org/)
> 
> --  
> Xavier Lacot  
> [xavier@lacot.org](mailto:xavier@lacot.org)  
> [http://lacot.org/](http://lacot.org/)

---

<div class="post-metadata">

### Author: ![xavier\_lacot](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/xavier_lacot/32/3354_2.png) [@xavier\_lacot](https://discuss.elastic.co/u/xavier_lacot)
#### Post date: [April 1, 2010, 10:05pm UTC](https://discuss.elastic.co/t/a-query-is-not-working-and-i-think-it-should/2852/11 "2010-04-01T22:05:30Z")

</div>

Great! Just wanted to be sure. Thanks a lot.

Xavier

2010/4/2 Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com)

> The should/must/mustNot elements in the bool filter accepts _any_ other  
> filter, which can be another bool filter. So, you can construct a boolfilter  
> of two must clauses, each containing another bool filter. Same thing, by the  
> way, applied to the bool query.
> 
> -shay.banon
> 
> On Fri, Apr 2, 2010 at 12:54 AM, Xavier Lacot [xavier@lacot.org](mailto:xavier@lacot.org) wrote:
> 
> > Hi Shay,
> > 
> > Thanks for the answer, I did not notice the array in the sample. Maybe  
> > should it be worth a sentence underlining this point?
> > 
> > There is however still a point that I am missing : is it possible to have  
> > boolean filters with several disjoint "should" queries ? I want to express  
> > in the same query something like: "the city is \*\*\* OR the zip\_code is \*\*\*\*\*"  
> > AND "the type of product is TYPE\_1 OR the type of product is TYPE\_2". How  
> > would you write this?
> > 
> > I tried the following, but it did not work:
> > 
> > "filter" : {  
> > "bool" : {  
> > "should" : [  
> > {  
> > "term" : { "city" : "city1" }  
> > },  
> > {  
> > "term" : { "zip\_code" : "012345" }  
> > }  
> > ],  
> > "should" : [  
> > {  
> > "term" : { "product" : "product1" }  
> > },  
> > {  
> > "term" : { "product" : "product2" }  
> > }  
> > ]  
> > }  
> > }
> > 
> > Then, I tried to write "should" boolean queries nested into a "must" one.  
> > It works as expected, but I am not sure if it is the most concise and  
> > efficient syntax?
> > 
> > "filter" : {  
> > "bool" : {  
> > "must" : [  
> > {  
> > "bool" : {  
> > "should" : [  
> > {  
> > "term" : { "city" : "city1" }  
> > },  
> > {  
> > "term" : { "zip\_code" : "012345" }  
> > }  
> > ]  
> > }  
> > },  
> > {  
> > "bool" : {  
> > "should" : [  
> > {  
> > "term" : { "product" : "product1" }  
> > },  
> > {  
> > "term" : { "product" : "product2" }  
> > }  
> > ]  
> > }  
> > }  
> > ]  
> > }  
> > }
> > 
> > kudos + cheers,
> > 
> > Xavier
> > 
> > 2010/4/1 Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com)
> > 
> > Hi,
> > 
> > > Each clase in the bool filter can accept a (json) array of filters.  
> > > Have a look here:  
> > > [http://www.elasticsearch.com/docs/elasticsearch/rest\_api/query\_dsl/bool\_filter/](http://www.elasticsearch.com/docs/elasticsearch/rest_api/query_dsl/bool_filter/) in  
> > > which the should accepts an array of two filters. You can use the same  
> > > syntax on the must/mustNot clauses as well.
> > > 
> > > -shay.banon
> > > 
> > > On Thu, Apr 1, 2010 at 9:31 AM, Xavier Lacot [xavier@lacot.org](mailto:xavier@lacot.org) wrote:
> > > 
> > > > Hello,
> > > > 
> > > > The last query proposed by Clinton worked well, as expected. But now, I  
> > > > want to make more complex queries, with filters on several terms.
> > > > 
> > > > For instance, I want to search products based on the product name, the  
> > > > brand name, the type of availability ("available" or "not available"), the  
> > > > city where the product is (so, a city\_name and eventually a zip\_code), and  
> > > > some other similar criteria. I tried for instance the following request, but  
> > > > it always returns an empty resultset, event if there should have been valid  
> > > > results. Here is what I tried :
> > > > 
> > > > { query:  
> > > > {  
> > > > "filteredQuery" : {  
> > > > "query" : {  
> > > > "term" : { "name" : "product3" }  
> > > > },  
> > > > "filter" : {  
> > > > "bool" : {  
> > > > "must" : {  
> > > > "term" : { "zip\_code" : 12345, "brand" : "testbrand" }  
> > > > }  
> > > > }  
> > > > }  
> > > > }  
> > > > }  
> > > > }
> > > > 
> > > > --\> results are only filtered on zip code, not on brand.
> > > > 
> > > > { query:  
> > > > {  
> > > > "filteredQuery" : {  
> > > > "query" : {  
> > > > "term" : { "name" : "product3" }  
> > > > },  
> > > > "filter" : {  
> > > > "bool" : {  
> > > > "must" : {  
> > > > "term" : { "brand" : "testbrand" },  
> > > > "term" : { "zip\_code" : 12345 }  
> > > > }  
> > > > }  
> > > > }  
> > > > }  
> > > > }  
> > > > }
> > > > 
> > > > -\> QueryParsingException[[myindexname] No json query parser registered  
> > > > for [zip\_code]]
> > > > 
> > > > How do I express a combination of filters? The example only shows  
> > > > restrictions with two filters, but I will have to build queries with 5 or  
> > > > more restrictions. Is there a trivial solution I have not seen in the  
> > > > documentation? I searched well but could not find examples for complex  
> > > > queries like these.
> > > > 
> > > > - Is t possible to have boolean filters with several disjoind "should"  
> > > > queries ? I want to express in the same query something like : "the city is  
> > > > \*\*\* OR the zip\_code is \*\*\*\*\*", and "the type of product is TYPE\_1 or the  
> > > > type of product is TYPE\_2 or the type of product is TYPE\_3". How would you  
> > > > write these?
> > > > 
> > > > Regards + bravos for ES, it's a pleasure to use it!
> > > > 
> > > > Xavier
> > > > 
> > > > 2010/3/18 Xavier Lacot [xavier@lacot.org](mailto:xavier@lacot.org)
> > > > 
> > > > Oh great! 🙂 Thanks both for the help !
> > > > 
> > > > > Xavier
> > > > > 
> > > > > 2010/3/18 Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com):
> > > > > 
> > > > > > Right!, I missed it 🙂
> > > > > > 
> > > > > > On Thu, Mar 18, 2010 at 4:16 PM, Clinton Gormley \<  
> > > > > > [clinton@iannounce.co.uk](mailto:clinton@iannounce.co.uk)\>  
> > > > > > wrote:
> > > > > > 
> > > > > > > > Here in a more "readable" form :  
> > > > > > > > {  
> > > > > > > > "filteredQuery" : {  
> > > > > > > > "query" : {  
> > > > > > > > "term" : { "name" : "product3" }  
> > > > > > > > },  
> > > > > > > > "filter" : {  
> > > > > > > > "range" : {  
> > > > > > > > "price" : { "from" : 30, "to" : 60 }  
> > > > > > > > }  
> > > > > > > > }  
> > > > > > > > }  
> > > > > > > > }
> > > > > > > 
> > > > > > > I think this should be:
> > > > > > > 
> > > > > > > { query:  
> > > > > > > {  
> > > > > > > "filteredQuery" : {  
> > > > > > > "query" : {  
> > > > > > > "term" : { "name" : "product3" }  
> > > > > > > },  
> > > > > > > "filter" : {  
> > > > > > > "range" : {  
> > > > > > > "price" : { "from" : 30, "to" : 60 }  
> > > > > > > }  
> > > > > > > }  
> > > > > > > }  
> > > > > > > }  
> > > > > > > }
> > > > > > > 
> > > > > > > See my DSL reference here:
> > > > > 
> > > > > [ElasticSearch::QueryDSL - metacpan.org](http://search.cpan.org/~drtech/ElasticSearch-0.05/lib/ElasticSearch/QueryDSL.pod)[http://search.cpan.org/~drtech/ElasticSearch-0.05/lib/ElasticSearch/QueryDSL.pod](http://search.cpan.org/%7Edrtech/ElasticSearch-0.05/lib/ElasticSearch/QueryDSL.pod)
> > > > > 
> > > > > > > clint
> > > > > > > 
> > > > > > > --  
> > > > > > > Web Announcements Limited is a company registered in England and  
> > > > > > > Wales,  
> > > > > > > with company number 05608868, with registered address at 10 Arvon  
> > > > > > > Road,  
> > > > > > > London, N5 1PR.
> > > > > 
> > > > > --  
> > > > > Xavier Lacot  
> > > > > [xavier@lacot.org](mailto:xavier@lacot.org)  
> > > > > [http://lacot.org/](http://lacot.org/)
> > > > 
> > > > --  
> > > > Xavier Lacot  
> > > > [xavier@lacot.org](mailto:xavier@lacot.org)  
> > > > [http://lacot.org/](http://lacot.org/)
> > 
> > --  
> > Xavier Lacot  
> > [xavier@lacot.org](mailto:xavier@lacot.org)  
> > [http://lacot.org/](http://lacot.org/)

--  
Xavier Lacot  
[xavier@lacot.org](mailto:xavier@lacot.org)

> **[Chez Xavier - Prenez vos places pour le Forum PHP 2012 !](https://lacot.org/)**
>
> Personal page of Xavier Lacot : articles, (open-source) code, photos, artwork

---

<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 6, 2017, 4:24am UTC](https://discuss.elastic.co/t/a-query-is-not-working-and-i-think-it-should/2852/12 "2017-07-06T04:24:57Z")

</div>


