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

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 --data @data.json
curl -XGET http://localhost:9200/test/testitem/_search?pretty=true --data
@request.json

Thanks for any help in advance.

Seems like your search request has too many closing } in the json. Run it
on 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 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 --data @data.json
curl -XGET http://localhost:9200/test/testitem/_search?pretty=true --data
@request.json

Thanks for any help in advance.

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"
}
}
}
}