Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch - combining query_string and bool query in filter

Tags:

Is it possible to combine query_string and bool query in filter query?

For Example -

{   "filter": {     "query_string": {       "query": "field:text"     }   },   "bool": {     "should": {       "match": {         "field": "text"       }     }   } } 
like image 577
Deepak Avatar asked Dec 29 '14 19:12

Deepak


People also ask

How do I merge two queries in Elasticsearch?

You need to use the bool query to combine different queries together. You can then choose whether each single query must match, should match (optional), or must not match.

How do I write multiple queries in Elasticsearch?

Add multiple criteria by using the bool data type. Use the bool data type to combine two or more criteria. This way, you'll be certain the results match what you need. The boolean data type, hence bool , is the same logical AND (&&) operator in the computer programming language.

When should I use bool query Elasticsearch?

Boolean queries in Elasticsearch are a popular query type because of their versatility and ease of use. Boolean queries, or bool queries, find or match documents by using boolean clauses. For the vast majority of cases, the filtering clause will be used because it can be cached for faster search times.

What is bool in Elasticsearch?

Boolean, or a bool query in Elasticsearch, is a type of search that allows you to combine conditions using Boolean conditions. Elasticsearch will search the document in the specified index and return all the records matching the combination of Boolean clauses.


1 Answers

bool is meant to be used to club various queries together into a single bool query. You can use bool to combine multiple queries in this manner -

{   "query": {     "bool": {       "must": [         {           "query_string": {             "query": "field:text"           }         },         {           "match": {             "field": "text"           }         }       ]     }   } } 

The must clause will make sure all the conditions are matched. You can also use should which will make sure either one of the query is matched in case of only should is used.

As bool is just another query type , you can also club bool queries inside bool queries as follows -

{   "query": {     "bool": {       "must": [         {           "bool": {             "must": [               {                 "query_string": {                   "query": "field:text"                 }               },               {                 "match": {                   "field": "value"                 }               }             ]           }         },         {           "match": {             "field": "text"           }         }       ]     }   } } 
like image 124
Vineeth Mohan Avatar answered Oct 01 '22 03:10

Vineeth Mohan