# ElasticSearch adjacent words for nested queries

**URL:** <https://discuss.elastic.co/t/elasticsearch-adjacent-words-for-nested-queries/282242>\
**Category:** Elasticsearch\
**Created:** [August 23, 2021, 2:22pm UTC](https://discuss.elastic.co/t/elasticsearch-adjacent-words-for-nested-queries/282242 "2021-08-23T14:22:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Loreto\_Parisi1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/loreto_parisi1/32/93581_2.png) [@Loreto\_Parisi1](https://discuss.elastic.co/u/Loreto_Parisi1)\
**Post date:** [August 23, 2021, 2:22pm UTC](https://discuss.elastic.co/t/elasticsearch-adjacent-words-for-nested-queries/282242/1 "2021-08-23T14:22:11Z")

</div>

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](https://stackoverflow.com/questions/28415456/elasticsearch-query-string-nested-query) 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.](https://stackoverflow.com/questions/68894012/elasticsearch-adjacent-words-for-nested-queries)

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [August 25, 2021, 9:46am UTC](https://discuss.elastic.co/t/elasticsearch-adjacent-words-for-nested-queries/282242/2 "2021-08-25T09:46:17Z")

</div>

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](https://www.elastic.co/guide/en/elasticsearch/reference/7.14/query-dsl-query-string-query.html#_proximity_searches)

Second, in order to properly support nested queries, you need to use the [nested query](https://www.elastic.co/guide/en/elasticsearch/reference/7.14/query-dsl-nested-query.html). 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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [September 22, 2021, 9:46am UTC](https://discuss.elastic.co/t/elasticsearch-adjacent-words-for-nested-queries/282242/3 "2021-09-22T09:46:59Z")

</div>

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