How to search for a value in any nested field?

776 views Asked by At

Basically I have to find a matching value in all the fields in my document.

I tried using query string but unfortunately, it only searches in first-level fields. The nested field values are not fetched using query string or may be the way I am using it is wrong.

I tried

GET events/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "default_field": "*",
            "query": "match"
          }
        }
      ]
    }
  }
}

how will I query such a way that it checks all the fields including the nested fields.

Thanks in advance

1

There are 1 answers

2
Val On BEST ANSWER

You can always have multiple clauses, one for the first level fields, and another for nested fields combined in an OR condition:

GET events/_search
{
  "query": {
    "bool": {
      "minimum_should_match": 1,
      "should": [
        {
          "query_string": {
            "default_field": "*",
            "query": "match",
            "lenient": true
          }
        },
        {
          "nested": {
            "path": "nested_field",
            "query": {
              "query_string": {
                "default_field": "nested_field.*",
                "query": "match",
                "lenient": true
              }
            }
          }
        }
      ]
    }
  }
}