Sofia
Sofia

Reputation: 25

Elasticsearch - searching through document keys

Is there any way to have a full_text search through document keys? I have dynamic template and some of the documents doesn't have all the keys. So I want to search for "X" and the results will be all the documents that "contains" X in its keys or values.

Your help is highly appreciated!

Upvotes: 0

Views: 140

Answers (1)

glenacota
glenacota

Reputation: 2547

(tested on Elasticsearch v7.x)

You can combine a multi_match query to search in values with an exists query to search in keys. For example, if you're looking for keys or values that contain the substring X, you can try

{
  "query": {
    "bool": {
      "should": [
        {
          "multi_match": {
            "query": "X"
          }
        },
        {
          "exists": {
            "field": "*X*"
          }
        }
      ]
    }
  }
}

The query performance will increase if you can get rid of the wildcards in the exists clause, notably, the one on the left.

Upvotes: 1

Related Questions