The query I would like to replicate in DSL is as below:
GET /_search { "query":{ "bool":{ "must":[ { "term":{ "destination":"singapore" } }, { "terms":{ "tag_ids":[ "tag_luxury" ] } } ] } }, "aggs":{ "max_price":{ "max":{ "field":"price_range_from.SGD" } }, "min_price":{ "min":{ "field":"price_range_from.SGD" } } }, "post_filter":{ "range":{ "price_range_from.SGD":{ "gte":0.0, "lte":100.0 } } } } The above query
- Matches terms -
destinationandtags_ids - Aggregates to result to find the max price from field
price_range_from.SGD - Applies another post_filter to subset the result set within price limits
It works perfectly well in the Elastic/Kibana console.
I replicated the above query in elasticsearch-dsl as below:
es_query = [] es_query.append(Q("term", destination="singapore")) es_query.append(Q("terms", tag_ids=["tag_luxury"])) final_query = Q("bool", must=es_query) es_conn = ElasticSearch.instance().get_client() dsl_client = DSLSearch(using=es_conn, index=index).get_dsl_client() dsl_client.query = final_query dsl_client.aggs.metric("min_price", "min", field="price_range_from.SGD") dsl_client.aggs.metric("max_price", "max", field="price_range_from.SGD") q = Q("range", **{"price_range_from.SGD":{"gte": 0.0, "lte": 100.0}}) dsl_client.post_filter(q) print(dsl_client.to_dict()) response = dsl_client.execute() print(response.to_dict().get("hits", {})) Although the aggregations are correct, products beyond the price range are also being returned. There is no error returned but it seems like the post_filter query is not applied.
I dived in the dsl_client object to see whether my query is being captured correctly. I see only the query and aggs but don't see the post_filter part in the object. The query when converted to a dictionary using dsl_client.to_dict() is as below -
{ "query":{ "bool":{ "must":[ { "term":{ "destination":"singapore" } }, { "terms":{ "tag_ids":[ "tag_luxury" ] } } ] } }, "aggs":{ "min_price":{ "min":{ "field":"price_range_from.SGD" } }, "max_price":{ "max":{ "field":"price_range_from.SGD" } } } } Please help. Thanks!
https://stackoverflow.com/questions/66774108/unable-to-replicate-post-filter-query-in-elasticsearch-dsl March 24, 2021 at 10:57AM
没有评论:
发表评论