Querying on a subobject field within a nested object


#1

I can't seem to get the expected results when running a terms query for a field within a subobject of a nested object. Assume an example like this (where nestedField has type nested in the mapping):

"nestedField": [
	{
		"topLevel": "value",
		"subObject": {
			"subObjectField": "value2"
		}
	},
	{
		"topLevel": "value3",
		"subObject": {
			"subObjectField": "value4"
		}
	}	
]

A filter like so:

"nested" : {
	"filter" : {
		"term" : {
			"subObject.subObjectField" : "value2"
		}
	},
	"path" : "nestedField"
}

Does not appear to return any results. Am I doing something wrong? Is this expected to work? Querying on topLevel does seem to work. Obviously this is just a fake example of the type of thing I'm trying to do, not the real one.


Error using nested query: "child query must only match non-parent docs"
(Adrien Grand) #2

Your query should be

{
  "nested" : {
	"filter" : {
		"term" : {
			"nestedField.subObject.subObjectField" : "value2"
		}
	},
	"path" : "nestedField"
  }
}

The only reason it worked with the toplevel object is that if you search on field "a", it actually searches on all fields that match *.a. By the way, the below query would work with your nested object:

{
  "nested" : {
	"filter" : {
		"term" : {
			"subObjectField" : "value2"
		}
	},
	"path" : "nestedField"
  }
}

But we found this behaviour trappy and so it got removed in 2.0. Now queries always require full paths.


#3

Looks like that did the trick. Thanks for the help.


#4

@jpountz It appears that using the full path (nestedField.subObject.subObjectField) is causing an error in 2.x, as explained here. Is using the full path the "correct" way to run this query in 2.x?


#5

Nevermind, turns out I accidentally wrapped the nested filter in another nested filter.


(system) #6

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