I have a query like below,
query = {
"query": {"query_string": {"query": "%s" % q}},
"filter":{"ids":{"values":list(ids)}},
"facets": {"destination": {
"terms": {"field": "destination.en"}},
"hotel_class":{
"terms":{"field":"hotel_class"}},
"hotel_type":{
"terms":{"field": "hotel_type"}},
}}
But my facets are not filtered due to my ids filter. I get all the facets, but I want them filtered by my ids filter above. Do you have any ideas ?
Although what you do works, a cleaner solution would be to have a filtered query. http://www.elasticsearch.org/guide/reference/query-dsl/filtered-query/
Which allows for your original query + some arbitrary filter (which in turn can be a complex boolean/ nested filter, etc.)
{
query: {
"filtered" : {
"query": {"query_string": {"query": "%s" % q}},
"filter":{"ids":{"values":list(ids)}},
}
},
"facets": {
"destination": {
"terms": {"field": "destination.en"}
},
"hotel_class": {
"terms": {"field": "hotel_class"}
},
"hotel_type": {
"terms": {"field": "hotel_type"}
}
}
}
The rationale is the following:
- any query is applied BEFORE faceting.
- any filter is applied AFTER faceting.
So if you want your facets to be filtered by some filter, you have to include said filter in the QUERY.
tunaktunak
facet_filter
fixed my problem,
like below,
{
"query": {
"query_string": {
"query": "%s" %q
}
},
"filter": {
"ids": {
"values": list(ids)
}
},
"facets": {
"destination": {
"terms": {
"field": "destination.en"
},
"facet_filter": {
"ids": {
"values": list(ids)
}
}
},
"hotel_class": {
"terms": {
"field": "hotel_class"
},
"facet_filter": {
"ids": {
"values": list(ids)
}
}
},
"hotel_type": {
"terms": {
"field": "hotel_type"
},
"facet_filter": {
"ids": {
"values": list(ids)
}
}
},
}
}
来源:https://stackoverflow.com/questions/16231727/filtering-facets-in-elasticsearch