Hi Vietpham,
If you are trying to use the second query to just filter out the search results from the first query, you may want to check post-filter in below link as workaround.
Post filter | Elasticsearch Guide [6.6] | Elastic.
Example:-
curl -X PUT "localhost:9200/shirts" -H 'Content-Type: application/json' -d'
{
"mappings": {
"properties": {
"brand": { "type": "keyword"},
"color": { "type": "keyword"},
"model": { "type": "keyword"}
}
}
}
'
=====
DOCS
====
curl -X PUT "localhost:9200/shirts/_doc/1?refresh" -H 'Content-Type: application/json' -d'
{
"brand": "gucci",
"color": "red",
"model": "slim",
"price": 2
}
'
curl -X PUT "localhost:9200/shirts/_doc/2?refresh" -H 'Content-Type: application/json' -d'
{
"brand": "gucci",
"color": "red",
"model": "slim",
"price": 5
}
'
curl -X PUT "localhost:9200/shirts/_doc/3?refresh" -H 'Content-Type: application/json' -d'
{
"brand": "gucci",
"color": "red",
"model": "slim",
"price": 1
}
'
curl -X PUT "localhost:9200/shirts/_doc/4?refresh" -H 'Content-Type: application/json' -d'
{
"brand": "gucci",
"color": "red",
"model": "slim",
"price": -8
}
'
curl -X PUT "localhost:9200/shirts/_doc/5?refresh" -H 'Content-Type: application/json' -d'
{
"brand": "gucci",
"color": "red",
"model": "slim",
"price": 9
}
'
===========
Post filter on search results for price less than 6
curl -X GET "localhost:9200/shirts/_search" -H 'Content-Type: application/json' -d'
{
"query": {
"bool": {
"filter": {
"term": { "brand": "gucci" }
}
}
},
"post_filter": {
"range" : { "price" : { "lt" : 6 } }
}
}
'
===========
Output
{"took":5,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":4,"relation":"eq"},"max_score":0.0,"hits":[{"_index":"shirts","_type":"_doc","_id":"1","_score":0.0,"_source":
{
"brand": "gucci",
"color": "red",
"model": "slim",
"price": 2
}
},{"_index":"shirts","_type":"_doc","_id":"2","_score":0.0,"_source":
{
"brand": "gucci",
"color": "red",
"model": "slim",
"price": 5
}
},{"_index":"shirts","_type":"_doc","_id":"3","_score":0.0,"_source":
{
"brand": "gucci",
"color": "red",
"model": "slim",
"price": 1
}
},{"_index":"shirts","_type":"_doc","_id":"4","_score":0.0,"_source":
{
"brand": "gucci",
"color": "red",
"model": "slim",
"price": -8
}
}]}}