29

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"
      }
    }
  }
}

1 Answer 1

66

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"
          }
        }
      ]
    }
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

How can we target multiple fields?
@OliverDixon "query_string": { "query": "field:text", "fields":['field1","field2"] }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.