Combine normal and nested fields with query_string

I have a document which contains both normal and nested fields. I'm trying to find all the documents that contain all the words from a regex query in any of the fields of interest. Since I'm using a regex query and trying to match on multiple fields, the query_string seems like the best option for my case, but however it isn't working as expected.

Let's say I have the following documents (where only the tags field is nested):

[
	{
		"name": "doc1",
		"type": "document",
		"color": "blue",
		"tags": [
			{
				"name": "tag1",
				"description": "test tag"
			},
			{
				"name": "tag2",
				"description": "test tag"
			}
		]
	},
	{
		"name": "doc2",
		"type": "document",
		"color": "lightblue",
		"tags": [
			{
				"name": "tag2",
				"description": "test tag"
			},
			{
				"name": "tag3",
				"description": "test tag"
			}
		]
	},
	{
		"name": "doc3",
		"type": "document",
		"color": "green",
		"tags": [
			{
				"name": "tag1",
				"description": "test tag"
			},
			{
				"name": "tag3",
				"description": "test tag"
			}
		]
	}
]

Now, I want to run something like:

{
	"query": {
		"bool": {
			"must": [
				{
					"query_string": {
						"query": "*doc* AND *blue* AND *tag2*",
						"fields": [
							"name^2",
							"color^1.5",
							"tags.name"
						]
					}
				}
			]
		}
	}
}

But this won't work, because the query_string is not nested.
Splitting the query into 2 separate query_string queries (one for the normal and the other for the nested fields) won't work, because it will try to find all 3 words in the mentioned fields.
Putting everything in a nested query_string also won't work because it can't access the normal fields.

Is there any possible solution to this? A possible solution (not the best) is to change the mapping of the tags field to be a normal object field instead of nested, but that could lead to other problems with different queries.

1 Like

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