Elasticsearch - combining query_string and bool query in filter
Asked Answered
B

1

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"
      }
    }
  }
}
Brandibrandice answered 29/12, 2014 at 19:38 Comment(0)
E
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"
          }
        }
      ]
    }
  }
}
Elkin answered 29/12, 2014 at 20:45 Comment(2)
How can we target multiple fields?Cardinal
@OliverDixon "query_string": { "query": "field:text", "fields":['field1","field2"] }Skricki

© 2022 - 2024 — McMap. All rights reserved.