Querying All Text/Keyword Fields, Including Nested Fields

Hello,

I am looking for a way to search across all text and keyword fields, including nested fields, in an index for keyword matches and then retrieve which field it hit on. I don’t necessarily know which field the keyword might hit on because the data is very complex and heavily nested. Because of that, I don’t want to be restricted to a list of fields that I think I might want to search on, but I do need to report back which field the query hit on.

I have tried query_string queries with no field set, but this doesn’t work on nested documents. I tried multi-match query with fields: empty list, but it also didn’t work with the nested fields. I’m wondering if anyone has suggestions on how to accomplish this. Thanks in advance!

Thanks for reaching out, @jalex. Do you have example of the multi-match queries you were working on?

Hi @jessgarson

The query I used was along these lines:

{“query”: {
    “multi_match”: {
        “query”: “something”,
        “fields”: []
    }
}}

It did find matches on regular (non-nested) text fields, but it did not find matches on nested text fields that I would have wanted it to.

1 Like

Thanks for following up, @jalex.

Have you tried anything like this yet:

{
  "query": {
    "bool": {
      "should": [
        {
          "query_string": {
            "query": "your_search_term",
            "fields": ["field1", "field2", "field3"]
          }
        },
        {
          "nested": {
            "path": "nestedField",
            "query": {
              "query_string": {
                "query": "your_search_term",
                "fields": ["nestedField.subfield1", "nestedField.subfield2"]
              }
            },
            "inner_hits": {} 
          }
        }
      ]
    }
  },
  "_source": ["field1", "field2", "field3", "nestedField.subfield1", "nestedField.subfield2"],
  "highlight": {
    "fields": {
      "*": {}
    }
  }
}