# Querying on a subobject field within a nested object

**URL:** <https://discuss.elastic.co/t/querying-on-a-subobject-field-within-a-nested-object/65533>\
**Category:** Elasticsearch\
**Created:** [November 9, 2016, 5:08pm UTC](https://discuss.elastic.co/t/querying-on-a-subobject-field-within-a-nested-object/65533 "2016-11-09T17:08:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jpblair](https://avatars.discourse-cdn.com/v4/letter/j/5e9695/32.png) [@jpblair](https://discuss.elastic.co/u/jpblair)\
**Post date:** [November 9, 2016, 5:08pm UTC](https://discuss.elastic.co/t/querying-on-a-subobject-field-within-a-nested-object/65533/1 "2016-11-09T17:08:27Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [November 17, 2016, 6:09pm UTC](https://discuss.elastic.co/t/querying-on-a-subobject-field-within-a-nested-object/65533/2 "2016-11-17T18:09:37Z")

</div>

Your query should be

```auto
{
  "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:

```auto
{
  "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.

---

<div class="post-metadata">

**Author:** ![jpblair](https://avatars.discourse-cdn.com/v4/letter/j/5e9695/32.png) [@jpblair](https://discuss.elastic.co/u/jpblair)\
**Post date:** [November 17, 2016, 8:00pm UTC](https://discuss.elastic.co/t/querying-on-a-subobject-field-within-a-nested-object/65533/3 "2016-11-17T20:00:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jpblair](https://avatars.discourse-cdn.com/v4/letter/j/5e9695/32.png) [@jpblair](https://discuss.elastic.co/u/jpblair)\
**Post date:** [December 2, 2016, 4:00pm UTC](https://discuss.elastic.co/t/querying-on-a-subobject-field-within-a-nested-object/65533/4 "2016-12-02T16:00:00Z")

</div>

@jpountz It appears that using the full path (`nestedField.subObject.subObjectField`) is causing an error in 2.x, as explained [here](https://discuss.elastic.co/t/error-using-nested-query-child-query-must-only-match-non-parent-docs/67815). Is using the full path the "correct" way to run this query in 2.x?

---

<div class="post-metadata">

**Author:** ![jpblair](https://avatars.discourse-cdn.com/v4/letter/j/5e9695/32.png) [@jpblair](https://discuss.elastic.co/u/jpblair)\
**Post date:** [December 9, 2016, 7:33pm UTC](https://discuss.elastic.co/t/querying-on-a-subobject-field-within-a-nested-object/65533/5 "2016-12-09T19:33:53Z")

</div>

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

---

<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:** [January 6, 2017, 7:33pm UTC](https://discuss.elastic.co/t/querying-on-a-subobject-field-within-a-nested-object/65533/6 "2017-01-06T19:33:55Z")

</div>

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