# Facets are not returned when nested/complex bool query is used

**URL:** <https://discuss.elastic.co/t/facets-are-not-returned-when-nested-complex-bool-query-is-used/7151>\
**Category:** Elasticsearch\
**Created:** [March 27, 2012, 9:06pm UTC](https://discuss.elastic.co/t/facets-are-not-returned-when-nested-complex-bool-query-is-used/7151 "2012-03-27T21:06:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Woland](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/woland/32/101616_2.png) [@Woland](https://discuss.elastic.co/u/Woland)\
**Post date:** [March 27, 2012, 9:06pm UTC](https://discuss.elastic.co/t/facets-are-not-returned-when-nested-complex-bool-query-is-used/7151/1 "2012-03-27T21:06:38Z")

</div>

I'm trying to use a complex query (where bool is nested) and return some  
facet counts. When that query is used the results are returned but no facet  
info. The query is just an example as I do have a much more complex one  
with a real business value.

Here are the results:

{  
"took" : 3,  
"timed\_out" : false,  
"\_shards" : {  
"total" : 5,  
"successful" : 5,  
"failed" : 0  
},  
"hits" : {  
"total" : 1,  
"max\_score" : 1.0460204,  
"hits" : [ {  
"\_index" : "test",  
"\_type" : "testitem",  
"\_id" : "1",  
"\_score" : 1.0460204, "\_source" : { "user" : "kimchy",  
"post\_date" :  
"2009-11-15T14:12:12", "message" : "trying out Elastic Search",  
"price" :  
123.12, "brand" : "sony"}  
} ]  
}  
}

THIS IS WHAT I EXPECT:  
{  
"took" : 39,  
"timed\_out" : false,  
"\_shards" : {  
"total" : 5,  
"successful" : 5,  
"failed" : 0  
},  
"hits" : {  
"total" : 1,  
"max\_score" : 0.30685282,  
"hits" : [ {  
"\_index" : "test",  
"\_type" : "testitem",  
"\_id" : "1",  
"\_score" : 0.30685282, "\_source" : { "user" : "kimchy",  
"post\_date"  
: "2009-11-15T14:12:12", "message" : "trying out Elastic Search",  
"price"  
: 123.12, "brand" : "sony"}  
} ]  
},  
"facets" : {  
"brand" : {  
"\_type" : "terms",  
"missing" : 0,  
"total" : 1,  
"other" : 0,  
"terms" : [ {  
"term" : "sony",  
"count" : 1  
} ]  
}  
}  
}

I use the latest 0.19.1 version of elasticsearch on windows.  
Here is the test data (i use windows, so that is the reason for files):

## data.json

{  
"user" : "kimchy",  
"post\_date" : "2009-11-15T14:12:12",  
"message" : "trying out Elastic Search",  
"price" : 123.12,  
"brand" : "sony"  
}

## request.json

{  
"from" : 0, "size" : 20,  
"query" :  
{  
"bool" : {  
"must" : {  
"term" : { "brand" : "sony" }  
},  
"must" :  
{  
"bool" :  
{  
"should" :  
{  
"range" : {  
"price" : { "to" : 200 }  
}  
}  
}  
}  
}  
}  
},  
"facets" : {  
"brand" : { "terms" : {"field" : "brand"} }  
}  
}

curl -XPUT [http://localhost:9200/test/testitem/1](http://localhost:9200/test/testitem/1) --data @data.json  
curl -XGET [http://localhost:9200/test/testitem/\_search?pretty=true](http://localhost:9200/test/testitem/_search?pretty=true) --data  
@request.json

Thanks for any help in advance.

---

<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 28, 2012, 10:35am UTC](https://discuss.elastic.co/t/facets-are-not-returned-when-nested-complex-bool-query-is-used/7151/2 "2012-03-28T10:35:24Z")

</div>

Seems like your search request has too many closing } in the json. Run it  
on [jsonlint.com](http://jsonlint.com) and see. Ping back if there is sill a problem...

On Tue, Mar 27, 2012 at 11:06 PM, Woland [sasha.la@gmail.com](mailto:sasha.la@gmail.com) wrote:

> I'm trying to use a complex query (where bool is nested) and return some  
> facet counts. When that query is used the results are returned but no facet  
> info. The query is just an example as I do have a much more complex one  
> with a real business value.
> 
> Here are the results:
> 
> {  
> "took" : 3,  
> "timed\_out" : false,  
> "\_shards" : {  
> "total" : 5,  
> "successful" : 5,  
> "failed" : 0  
> },  
> "hits" : {  
> "total" : 1,  
> "max\_score" : 1.0460204,  
> "hits" : [ {  
> "\_index" : "test",  
> "\_type" : "testitem",  
> "\_id" : "1",  
> "\_score" : 1.0460204, "\_source" : { "user" : "kimchy",  
> "post\_date" :  
> "2009-11-15T14:12:12", "message" : "trying out Elastic Search",  
> "price" :  
> 123.12, "brand" : "sony"}  
> } ]  
> }  
> }
> 
> THIS IS WHAT I EXPECT:  
> {  
> "took" : 39,  
> "timed\_out" : false,  
> "\_shards" : {  
> "total" : 5,  
> "successful" : 5,  
> "failed" : 0  
> },  
> "hits" : {  
> "total" : 1,  
> "max\_score" : 0.30685282,  
> "hits" : [ {  
> "\_index" : "test",  
> "\_type" : "testitem",  
> "\_id" : "1",  
> "\_score" : 0.30685282, "\_source" : { "user" : "kimchy",  
> "post\_date"  
> : "2009-11-15T14:12:12", "message" : "trying out Elastic Search",  
> "price"  
> : 123.12, "brand" : "sony"}  
> } ]  
> },  
> "facets" : {  
> "brand" : {  
> "\_type" : "terms",  
> "missing" : 0,  
> "total" : 1,  
> "other" : 0,  
> "terms" : [ {  
> "term" : "sony",  
> "count" : 1  
> } ]  
> }  
> }  
> }
> 
> I use the latest 0.19.1 version of elasticsearch on windows.  
> Here is the test data (i use windows, so that is the reason for files):
> 
> ## data.json
> 
> {  
> "user" : "kimchy",  
> "post\_date" : "2009-11-15T14:12:12",  
> "message" : "trying out Elastic Search",  
> "price" : 123.12,  
> "brand" : "sony"  
> }
> 
> ## request.json
> 
> {  
> "from" : 0, "size" : 20,  
> "query" :  
> {  
> "bool" : {  
> "must" : {  
> "term" : { "brand" : "sony" }  
> },  
> "must" :  
> {  
> "bool" :  
> {  
> "should" :  
> {  
> "range" : {  
> "price" : { "to" : 200 }  
> }  
> }  
> }  
> }  
> }  
> }  
> },  
> "facets" : {  
> "brand" : { "terms" : {"field" : "brand"} }  
> }  
> }
> 
> curl -XPUT [http://localhost:9200/test/testitem/1](http://localhost:9200/test/testitem/1) --data @data.json  
> curl -XGET [http://localhost:9200/test/testitem/\_search?pretty=true](http://localhost:9200/test/testitem/_search?pretty=true) --data  
> @request.json
> 
> Thanks for any help in advance.

---

<div class="post-metadata">

**Author:** ![Woland](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/woland/32/101616_2.png) [@Woland](https://discuss.elastic.co/u/Woland)\
**Post date:** [March 28, 2012, 5:40pm UTC](https://discuss.elastic.co/t/facets-are-not-returned-when-nested-complex-bool-query-is-used/7151/3 "2012-03-28T17:40:57Z")

</div>

> You are right, there was one extra } in the request.json. I wonder why the  
> query execute and didn't complain that it was not well formed json.

## Here is the correct request.json

{  
"from": 0,  
"size": 20,  
"query": {  
"bool": {  
"must": {  
"bool": {  
"should": {  
"range": {  
"price": {  
"to": 200  
}  
}  
}  
}  
}  
}  
},  
"facets": {  
"brand": {  
"terms": {  
"field": "brand"  
}  
}  
}  
}

---

<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, 3:34am UTC](https://discuss.elastic.co/t/facets-are-not-returned-when-nested-complex-bool-query-is-used/7151/4 "2017-07-06T03:34:17Z")

</div>


