php - Elasticsearch combine two different queries types in one request
I'm wondering if there is a possibility to combine two query types, in my case I need a queryString and filtered query, each has to operate on a different field.
the query filtered get all record who's in$postids
$postids:multiple
value of post_id
and the query string is to search a words in text
Currently I need two requests to archive this:
Query filtered:
GET /myindex1/tweecoms/_search
{
"query" : {
"filtered" : {
"filter" : {
"terms" : {
"post_id" : ['.$postids.']
}
}
}
}
query String:
GET /myindex1/tweecoms/_search{
"query": {
"query_string": {
"query": "*' . $sujet . '*",
"lenient": true
}
}
}
and I tried to combine it
GET /myindex1/tweecoms/_search
{
"query": {
"bool": {
"should": [
{
"query_string": {
"query": "hhhh",
"lenient": true
}
},
{
"filtered" : {
"filter" : {
"terms" : {
"post_id" : [0,157]
}
}
}
}
]
}
}
}`
but what i try run just the query string
Answer
Solution:
I think you need to write query inside
filtered
clause. This will help you understand. Try thisThe problem with your query is you are using should clause which will select either documents with given query string or those with given post_ids. You can replace
should
withmust
and will get expected results.You could also use minimum_should_match parameter inside your query
I hope this helps