# Searching within a single nested object

**URL:** <https://discuss.elastic.co/t/searching-within-a-single-nested-object/4795>\
**Category:** Elasticsearch\
**Created:** [July 6, 2011, 5:30pm UTC](https://discuss.elastic.co/t/searching-within-a-single-nested-object/4795 "2011-07-06T17:30:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ellery\_Crane\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ellery_crane_2/32/3369_2.png) [@Ellery\_Crane\_2](https://discuss.elastic.co/u/Ellery_Crane_2)\
**Post date:** [July 6, 2011, 5:30pm UTC](https://discuss.elastic.co/t/searching-within-a-single-nested-object/4795/1 "2011-07-06T17:30:22Z")

</div>

I'm trying to restrict a query to search on multiple fields within the  
same nested object, but I'm not sure how to do so. Take the following  
example documents:

curl -XDELETE '[http://127.0.0.1:9200/someindex?pretty=1](http://127.0.0.1:9200/someindex?pretty=1)'  
curl -XPOST '[http://127.0.0.1:9200/someindex/user?pretty=1](http://127.0.0.1:9200/someindex/user?pretty=1)' -d '  
{  
"friends" : [  
{  
"name" : {  
"first" : "Jack",  
"last" : "Smith"  
},  
},  
{  
"name" : {  
"first" : "Alice",  
"last" : "Brown"  
},  
}  
]  
}  
'

curl -XPOST '[http://127.0.0.1:9200/someindex/user?pretty=1](http://127.0.0.1:9200/someindex/user?pretty=1)' -d '  
{  
"friends" : [  
{  
"name" : {  
"first" : "Jack",  
"last" : "Brown"  
},  
},  
{  
"name" : {  
"first" : "Alice",  
"last" : "Smith"  
},  
},  
]  
}  
'

I want to search for all users with "Jack Smith" as a friend. If I  
search on friends.name.first = "Jack" and friends.name.last = "Smith",  
however, both documents are returned instead of just the first one. Is  
there some way to restrict fields to be within the same nested object,  
as opposed to _any_ nested object?

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [July 6, 2011, 5:49pm UTC](https://discuss.elastic.co/t/searching-within-a-single-nested-object/4795/2 "2011-07-06T17:49:43Z")

</div>

Hi Ellery

On Wed, 2011-07-06 at 10:30 -0700, Ellery Crane wrote:

> I'm trying to restrict a query to search on multiple fields within the  
> same nested object, but I'm not sure how to do so. Take the following  
> example documents:

Up until today, the answer would have been: create the friends as  
separate docs.

Just landed in master: Nested Docs

> <https://github.com/elastic/elasticsearch/issues/1095>
>
> Nested objects/documents allow to map certain sections in the document indexed a…s nested allowing to query them as if they are separate docs joining with the parent owning doc.
> 
> Note, this feature is experimental and might require reindexing the data if using it.
> 
> One of the problems when indexing inner objects that occur several times in a doc is that "cross object" search match will occur, for example:
> 
> \`\`\`
> {
> "obj1" : \[
> {
> "name" : "blue",
> "count" : 4
> },
> {
> "name" : "green",
> "count" : 6
> }
> \]
> }
> \`\`\`
> 
> Searching for \`name\` set to \`blue\` and \`count\` higher than \`5\` will match the doc, because in the first element the \`name\` matches \`blue\`, and in the second element, \`count\` matches "higher than \`5\`".
> \## Nested Mapping
> 
> Nested mapping allow to map certain inner objects (usually multi instance ones), for example:
> 
> \`\`\`
> {
> "type1" : {
> "properties" : {
> "obj1" : {
> "type" : "nested"
> }
> }
> }
> }
> \`\`\`
> 
> The above will cause all \`obj1\` to be indexed as a nested doc. The mapping is similar in nature to setting \`type\` to \`object\`, except that its \`nested\`.
> 
> The \`nested\` object fields can also be automatically added to the immediate parent by setting \`include\_in\_parent\` to \`true\`, and also included in the root object by setting \`include\_in\_root\` to \`true\`.
> 
> Nested docs will also automatically use the root doc \`\_all\` field. 
> \## Nested Queries
> 
> Nested queries allow to search within nested docs, resulting in the root parent doc (join), for example:
> 
> \`\`\`
> {
> "nested" : {
> "path" : "obj1",
> "score\_mode" : "avg"
> "query" : {
> "bool" : {
> "must" : \[
> "text" : {"obj1.name" : "blue"},
> "range" : {"obj1.count" : {"gt" : 5}}
> \]
> }
> }
> }
> }
> \`\`\`
> 
> The query \`path\` points to the nested object path, and the \`query\` (or \`filter\`) includes the query that will run on the nested docs matching the direct path, and joining with the root parent docs.
> 
> The \`score\_mode\` allows to set how inner children matching affects scoring of parent. It defaults to \`avg\`, but can be \`total\`, \`max\` and \`none\`.
> 
> Multi level nesting is automatically supported, and detected, resulting in an inner nested query to automatically match the relevant nesting level (and not root) if it exists within another nested query.
> \## Internal Implementation
> 
> Internally, nested objects are indexed as additional documents, but, since they can be guaranteed to be indexed within the same "block", it allows for extremely fast joining with parent docs.
> 
> Those internal nested documents are automatically masked away when doing operations against the index (like searching with a \`match\_all\` query), and they bubble out when using the nested query.
> \## Left Overs
> 
> There are many things to still do to have it as a complete. The most important one is have a good story around faceting and nested docs (it will really enhance it as well, especially for key and value based facets).

There are bound to be problems with this still, as it has only just been  
added, but this new feature will solve the issue you describe

clint

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [July 6, 2011, 5:53pm UTC](https://discuss.elastic.co/t/searching-within-a-single-nested-object/4795/3 "2011-07-06T17:53:54Z")

</div>

Whoa !!! That's what many users were waiting for... Thanks for this !

David.

---

<div class="post-metadata">

**Author:** ![Ellery\_Crane\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ellery_crane_2/32/3369_2.png) [@Ellery\_Crane\_2](https://discuss.elastic.co/u/Ellery_Crane_2)\
**Post date:** [July 6, 2011, 6:20pm UTC](https://discuss.elastic.co/t/searching-within-a-single-nested-object/4795/4 "2011-07-06T18:20:49Z")

</div>

Awesome! Man, talk about timely 😉

On Jul 6, 1:49 pm, Clinton Gormley [clin...@iannounce.co.uk](mailto:clin...@iannounce.co.uk) wrote:

> Hi Ellery
> 
> On Wed, 2011-07-06 at 10:30 -0700, Ellery Crane wrote:
> 
> > I'm trying to restrict a query to search on multiple fields within the  
> > same nested object, but I'm not sure how to do so. Take the following  
> > example documents:
> 
> Up until today, the answer would have been: create the friends as  
> separate docs.
> 
> Just landed in master: Nested Docshttps://github.com/elasticsearch/elasticsearch/issues/1095
> 
> There are bound to be problems with this still, as it has only just been  
> added, but this new feature will solve the issue you describe
> 
> clint

---

<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:** [July 6, 2017, 4:01am UTC](https://discuss.elastic.co/t/searching-within-a-single-nested-object/4795/5 "2017-07-06T04:01:33Z")

</div>


