# Accessing nested document fields in nested queries

**URL:** <https://discuss.elastic.co/t/accessing-nested-document-fields-in-nested-queries/5166>\
**Category:** Elasticsearch\
**Created:** [August 15, 2011, 8:16pm UTC](https://discuss.elastic.co/t/accessing-nested-document-fields-in-nested-queries/5166 "2011-08-15T20:16:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris\_K\_Wensel](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chris_k_wensel/32/2486_2.png) [@Chris\_K\_Wensel](https://discuss.elastic.co/u/Chris_K_Wensel)\
**Post date:** [August 15, 2011, 8:16pm UTC](https://discuss.elastic.co/t/accessing-nested-document-fields-in-nested-queries/5166/1 "2011-08-15T20:16:25Z")

</div>

Hi all

I've have a doc with a nested document. in practice the nested field is an array.

first, is there a simple way to find all of a set of nested documents if I don't care about values in the root document?

if not, it seems I am unable to access the fields in the nested document when executing a nested query. but i can access fields in the root document. is this behavior expected?

below is the query i'm executing. it returns no fields in the result.

{  
"query" : {  
"nested" : {  
"query" : {  
"bool" : {  
"must" : [ {  
"term" : {  
"parents.type" : "app"  
}  
}, {  
"term" : {  
"parents.id" : "C494277873EA9ADB1903CEA95F0C77C2"  
}  
} ]  
}  
},  
"path" : "parents"  
}  
},  
"fields" : ["parents.type", "parents.id", "parents.name", "parents.version"]  
}

FWIW, the relevant portion of the mapping

"parents" : {  
"properties" : {  
"tags" : {  
"analyzer" : "csv",  
"type" : "string"  
},  
"id" : {  
"index" : "not\_analyzed",  
"type" : "string"  
},  
"name" : {  
"index" : "not\_analyzed",  
"type" : "string"  
},  
"type" : {  
"index" : "not\_analyzed",  
"type" : "string"  
}  
},  
"type" : "nested"  
},

--  
Chris K Wensel  
[chris@concurrentinc.com](mailto:chris@concurrentinc.com)  
[http://www.concurrentinc.com](http://www.concurrentinc.com)

-- Concurrent, Inc. offers mentoring, support for Cascading

---

<div class="post-metadata">

**Author:** ![Chris\_K\_Wensel](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chris_k_wensel/32/2486_2.png) [@Chris\_K\_Wensel](https://discuss.elastic.co/u/Chris_K_Wensel)\
**Post date:** [August 15, 2011, 8:39pm UTC](https://discuss.elastic.co/t/accessing-nested-document-fields-in-nested-queries/5166/2 "2011-08-15T20:39:47Z")

</div>

fwiw, looks like if the nested type isn't an array, I can access the nested values.

On Aug 15, 2011, at 1:16 PM, Chris K Wensel wrote:

> Hi all
> 
> I've have a doc with a nested document. in practice the nested field is an array.
> 
> first, is there a simple way to find all of a set of nested documents if I don't care about values in the root document?
> 
> if not, it seems I am unable to access the fields in the nested document when executing a nested query. but i can access fields in the root document. is this behavior expected?
> 
> below is the query i'm executing. it returns no fields in the result.
> 
> {  
> "query" : {  
> "nested" : {  
> "query" : {  
> "bool" : {  
> "must" : [ {  
> "term" : {  
> "parents.type" : "app"  
> }  
> }, {  
> "term" : {  
> "parents.id" : "C494277873EA9ADB1903CEA95F0C77C2"  
> }  
> } ]  
> }  
> },  
> "path" : "parents"  
> }  
> },  
> "fields" : ["parents.type", "parents.id", "parents.name", "parents.version"]  
> }
> 
> FWIW, the relevant portion of the mapping
> 
> "parents" : {  
> "properties" : {  
> "tags" : {  
> "analyzer" : "csv",  
> "type" : "string"  
> },  
> "id" : {  
> "index" : "not\_analyzed",  
> "type" : "string"  
> },  
> "name" : {  
> "index" : "not\_analyzed",  
> "type" : "string"  
> },  
> "type" : {  
> "index" : "not\_analyzed",  
> "type" : "string"  
> }  
> },  
> "type" : "nested"  
> },
> 
> --  
> Chris K Wensel  
> [chris@concurrentinc.com](mailto:chris@concurrentinc.com)  
> [http://www.concurrentinc.com](http://www.concurrentinc.com)
> 
> -- Concurrent, Inc. offers mentoring, support for Cascading

--  
Chris K Wensel  
[chris@concurrentinc.com](mailto:chris@concurrentinc.com)

> **[Integrate.io - One Platform To Support Your Entire Data Journey](https://www.integrate.io/)**
>
> Integrate.io - Unify your data while building & managing clean, secure pipelines for better decision making. Power your data warehouse with ETL, ELT, CDC, Reverse ETL, and API Management.

-- Concurrent, Inc. offers mentoring, support for Cascading

---

<div class="post-metadata">

**Author:** ![Chris\_K\_Wensel](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chris_k_wensel/32/2486_2.png) [@Chris\_K\_Wensel](https://discuss.elastic.co/u/Chris_K_Wensel)\
**Post date:** [August 15, 2011, 9:58pm UTC](https://discuss.elastic.co/t/accessing-nested-document-fields-in-nested-queries/5166/3 "2011-08-15T21:58:39Z")

</div>

well, it turns out what I really want is a way to query and return only matching nested documents (, sometimes using criteria from the root document).

any pointers would be helpful. but I suspect this isn't an option (yet).

cheers,  
chris

On Aug 15, 2011, at 1:39 PM, Chris K Wensel wrote:

> fwiw, looks like if the nested type isn't an array, I can access the nested values.
> 
> On Aug 15, 2011, at 1:16 PM, Chris K Wensel wrote:
> 
> > Hi all
> > 
> > I've have a doc with a nested document. in practice the nested field is an array.
> > 
> > first, is there a simple way to find all of a set of nested documents if I don't care about values in the root document?
> > 
> > if not, it seems I am unable to access the fields in the nested document when executing a nested query. but i can access fields in the root document. is this behavior expected?
> > 
> > below is the query i'm executing. it returns no fields in the result.
> > 
> > {  
> > "query" : {  
> > "nested" : {  
> > "query" : {  
> > "bool" : {  
> > "must" : [ {  
> > "term" : {  
> > "parents.type" : "app"  
> > }  
> > }, {  
> > "term" : {  
> > "parents.id" : "C494277873EA9ADB1903CEA95F0C77C2"  
> > }  
> > } ]  
> > }  
> > },  
> > "path" : "parents"  
> > }  
> > },  
> > "fields" : ["parents.type", "parents.id", "parents.name", "parents.version"]  
> > }
> > 
> > FWIW, the relevant portion of the mapping
> > 
> > "parents" : {  
> > "properties" : {  
> > "tags" : {  
> > "analyzer" : "csv",  
> > "type" : "string"  
> > },  
> > "id" : {  
> > "index" : "not\_analyzed",  
> > "type" : "string"  
> > },  
> > "name" : {  
> > "index" : "not\_analyzed",  
> > "type" : "string"  
> > },  
> > "type" : {  
> > "index" : "not\_analyzed",  
> > "type" : "string"  
> > }  
> > },  
> > "type" : "nested"  
> > },
> > 
> > --  
> > Chris K Wensel  
> > [chris@concurrentinc.com](mailto:chris@concurrentinc.com)  
> > [http://www.concurrentinc.com](http://www.concurrentinc.com)
> > 
> > -- Concurrent, Inc. offers mentoring, support for Cascading
> 
> --  
> Chris K Wensel  
> [chris@concurrentinc.com](mailto:chris@concurrentinc.com)  
> [http://www.concurrentinc.com](http://www.concurrentinc.com)
> 
> -- Concurrent, Inc. offers mentoring, support for Cascading

--  
Chris K Wensel  
[chris@concurrentinc.com](mailto:chris@concurrentinc.com)

> **[Integrate.io - One Platform To Support Your Entire Data Journey](https://www.integrate.io/)**
>
> Integrate.io - Unify your data while building & managing clean, secure pipelines for better decision making. Power your data warehouse with ETL, ELT, CDC, Reverse ETL, and API Management.

-- Concurrent, Inc. offers mentoring, support for Cascading

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [August 15, 2011, 11:22pm UTC](https://discuss.elastic.co/t/accessing-nested-document-fields-in-nested-queries/5166/4 "2011-08-15T23:22:08Z")

</div>

Suspect validated :), its not an option (yet)...

On Tue, Aug 16, 2011 at 12:58 AM, Chris K Wensel [chris@wensel.net](mailto:chris@wensel.net) wrote:

> well, it turns out what I really want is a way to query and return only  
> matching nested documents (, sometimes using criteria from the root  
> document).
> 
> any pointers would be helpful. but I suspect this isn't an option (yet).
> 
> cheers,  
> chris
> 
> On Aug 15, 2011, at 1:39 PM, Chris K Wensel wrote:
> 
> > fwiw, looks like if the nested type isn't an array, I can access the  
> > nested values.
> > 
> > On Aug 15, 2011, at 1:16 PM, Chris K Wensel wrote:
> > 
> > > Hi all
> > > 
> > > I've have a doc with a nested document. in practice the nested field is  
> > > an array.
> > > 
> > > first, is there a simple way to find all of a set of nested documents if  
> > > I don't care about values in the root document?
> > > 
> > > if not, it seems I am unable to access the fields in the nested document  
> > > when executing a nested query. but i can access fields in the root document.  
> > > is this behavior expected?
> > > 
> > > below is the query i'm executing. it returns no fields in the result.
> > > 
> > > {  
> > > "query" : {  
> > > "nested" : {  
> > > "query" : {  
> > > "bool" : {  
> > > "must" : [ {  
> > > "term" : {  
> > > "parents.type" : "app"  
> > > }  
> > > }, {  
> > > "term" : {  
> > > "parents.id" : "C494277873EA9ADB1903CEA95F0C77C2"  
> > > }  
> > > } ]  
> > > }  
> > > },  
> > > "path" : "parents"  
> > > }  
> > > },  
> > > "fields" : [ "parents.type", "parents.id", "parents.name",  
> > > "parents.version" ]  
> > > }
> > > 
> > > FWIW, the relevant portion of the mapping
> > > 
> > > "parents" : {  
> > > "properties" : {  
> > > "tags" : {  
> > > "analyzer" : "csv",  
> > > "type" : "string"  
> > > },  
> > > "id" : {  
> > > "index" : "not\_analyzed",  
> > > "type" : "string"  
> > > },  
> > > "name" : {  
> > > "index" : "not\_analyzed",  
> > > "type" : "string"  
> > > },  
> > > "type" : {  
> > > "index" : "not\_analyzed",  
> > > "type" : "string"  
> > > }  
> > > },  
> > > "type" : "nested"  
> > > },
> > > 
> > > --  
> > > Chris K Wensel  
> > > [chris@concurrentinc.com](mailto:chris@concurrentinc.com)  
> > > [http://www.concurrentinc.com](http://www.concurrentinc.com)
> > > 
> > > -- Concurrent, Inc. offers mentoring, support for Cascading
> > 
> > --  
> > Chris K Wensel  
> > [chris@concurrentinc.com](mailto:chris@concurrentinc.com)  
> > [http://www.concurrentinc.com](http://www.concurrentinc.com)
> > 
> > -- Concurrent, Inc. offers mentoring, support for Cascading
> 
> --  
> Chris K Wensel  
> [chris@concurrentinc.com](mailto:chris@concurrentinc.com)  
> [http://www.concurrentinc.com](http://www.concurrentinc.com)
> 
> -- Concurrent, Inc. offers mentoring, support for Cascading

---

<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, 3:56am UTC](https://discuss.elastic.co/t/accessing-nested-document-fields-in-nested-queries/5166/5 "2017-07-06T03:56:53Z")

</div>


