ElasticSearch adjacent words for nested queries

I'm using ES 7.14/Kibana 7.10, I have to search for adjacent words (any order), hence I'm using this query:

{
 "query":{
    "bool":{
        "must":[
            {
                "query_string":{
                    "query":"*antonio* *banderas*",
                    "fields":[
                        "text"
                    ],
                    "default_operator":"and",
                }
            }]
      }
  }
}

This works ok for a text plain field. Now, I have a nested field metadata, let's say the mapping is

{
	"mappings:": {
		"properties": {
			"text": {
				"type": "text"
			},
			"metadata": {
				"type": "nested",
				"properties": {
					"text": {
						"type": "text"
					}
				}
			}
		}
	}
}

and I would like to search that nested field in the same way (adjacent words search), so assumed that it's is possibile to write a nested query for query_string in this way

{
  "query": {
    "query_string": {
      "query": "metadata.text:*antonio* *banderas*"
    }
  }
}

How to adapt this approach to the previous one with default_operator=and etc.? If I do

   {
      "query": {
        "query_string": {
          "query": "metadata.text:*antonio* *banderas*",
          "default_operator": "and"
        }
      }
    }

I don't get any result (but any error too).

Original SF question.

Hey,

First, in order to search for adjacent words, do not use asterisks (which you should never do), but a so called phrase query. See Query string query | Elasticsearch Guide [7.14] | Elastic

Second, in order to properly support nested queries, you need to use the nested query. I am, however, not a hundred percent that you even need a nested query. Can you explain why you went with that for the metadata?

Hope that helps as a start.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.