# Nested array doc query

**URL:** <https://discuss.elastic.co/t/nested-array-doc-query/4931>\
**Category:** Elasticsearch\
**Created:** [July 22, 2011, 11:33pm UTC](https://discuss.elastic.co/t/nested-array-doc-query/4931 "2011-07-22T23:33:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tsolakp](https://avatars.discourse-cdn.com/v4/letter/t/7bcc69/32.png) [@tsolakp](https://discuss.elastic.co/u/tsolakp)\
**Post date:** [July 22, 2011, 11:33pm UTC](https://discuss.elastic.co/t/nested-array-doc-query/4931/1 "2011-07-22T23:33:53Z")

</div>

I have the following document in the index:

{property={suitableForModeling=Y, location={lon=-104.930714, lat=39.700083}, bathrooms=1, propStyle:r=R, bedrooms=2, price=135000, lastSalePrice=135000, sales=[{price=135000, saleDate=2009-09-08T06:00:00.000Z}, {price=142900, saleDate=2004-11-23T07:00:00.000Z}], propType=CND, yoc=1982, saleDate=2009-09-08T06:00:00.000Z, acres=0.01, mainSqft=745}}

with the following mapping:

client.admin().indices().preparePutMapping("props").setType("prop").setSource( jsonBuilder()  
.startObject()  
.startObject("type")  
.startObject("properties")  
.startObject("property")  
.startObject("properties")

```
          .startObject("location")
            .field("type", "geo_point")                 
          .endObject() 
          
          .startObject("sales")
            .field("type", "nested")    
          .endObject() 
          
        .endObject()
      .endObject()
      .endObject()
    .endObject()
  .endObject().string()    
 ).execute().actionGet();

```

Why does the following query return 0 hits (using ES 0.17.1)?

SearchResponse response = client.prepareSearch("props")  
.setSearchType(SearchType.DFS\_QUERY\_THEN\_FETCH)  
.setQuery( nestedQuery("property.sales", fieldQuery("property.sales.price", 135000 ) ) )  
.setFrom(0).setSize(60).setExplain(true)  
.execute()  
.actionGet();

---

<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:** [July 24, 2011, 1:30am UTC](https://discuss.elastic.co/t/nested-array-doc-query/4931/2 "2011-07-24T01:30:31Z")

</div>

You need to explicitly wrap it in a nested query:  
[Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/nested-query.html).

On Sat, Jul 23, 2011 at 2:33 AM, tsolakp [tsolakp@gmail.com](mailto:tsolakp@gmail.com) wrote:

> I have the following document in the index:
> 
> {property={suitableForModeling=Y, location={lon=-104.930714,  
> lat=39.700083},  
> bathrooms=1, propStyle:r=R, bedrooms=2, price=135000, lastSalePrice=135000,  
> sales=[{price=135000, saleDate=2009-09-08T06:00:00.000Z}, {price=142900,  
> saleDate=2004-11-23T07:00:00.000Z}], propType=CND, yoc=1982,  
> saleDate=2009-09-08T06:00:00.000Z, acres=0.01, mainSqft=745}}
> 
> with the following mapping:
> 
> client.admin().indices().preparePutMapping("props").setType("prop").setSource(  
> jsonBuilder()  
> .startObject()  
> .startObject("type")  
> .startObject("properties")  
> .startObject("property")  
> .startObject("properties")
> 
> ```
> .startObject("location")
> .field("type", "geo_point")
> .endObject()
> 
> .startObject("sales")
> .field("type", "nested")
> .endObject()
> 
> .endObject()
> .endObject()
> .endObject()
> .endObject()
> .endObject().string()
> ).execute().actionGet();
> 
> ```
> 
> Why does the following query return 0 hits (using ES 0.17.1)?
> 
> SearchResponse response = client.prepareSearch("props")  
> .setSearchType(SearchType.DFS\_QUERY\_THEN\_FETCH)  
> .setQuery( nestedQuery("property.sales",  
> fieldQuery("property.sales.price", 135000 ) ) )  
> .setFrom(0).setSize(60).setExplain(true)  
> .execute()  
> .actionGet();
> 
> --  
> View this message in context:  
> [http://elasticsearch-users.115913.n3.nabble.com/Nested-array-doc-query-tp3192629p3192629.html](http://elasticsearch-users.115913.n3.nabble.com/Nested-array-doc-query-tp3192629p3192629.html)  
> Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![tsolakp](https://avatars.discourse-cdn.com/v4/letter/t/7bcc69/32.png) [@tsolakp](https://discuss.elastic.co/u/tsolakp)\
**Post date:** [July 25, 2011, 3:27pm UTC](https://discuss.elastic.co/t/nested-array-doc-query/4931/3 "2011-07-25T15:27:35Z")

</div>

Hi Shay,

I am not sure what you mean by wrapping it in nested query.

Is not this doing it already?

nestedQuery( "property.sales", fieldQuery("property.sales.price", 135000 ) )

---

<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:** [July 26, 2011, 6:08am UTC](https://discuss.elastic.co/t/nested-array-doc-query/4931/4 "2011-07-26T06:08:48Z")

</div>

I missed that (gave up on understanding why people insist on pasting code  
into mails instead of using gist).

So, it should work, gist a repro (preferable using curl) and I can have a  
look. Are you sure you refresh the index between the indexing of the data  
and searching?

On Mon, Jul 25, 2011 at 6:27 PM, tsolakp [tsolakp@gmail.com](mailto:tsolakp@gmail.com) wrote:

> Hi Shay,
> 
> I am not sure what you mean by wrapping it in nested query.
> 
> Is not this doing it already?
> 
> nestedQuery( "property.sales", fieldQuery("property.sales.price", 135000 )  
> )
> 
> --  
> View this message in context:  
> [http://elasticsearch-users.115913.n3.nabble.com/Nested-array-doc-query-tp3192629p3197785.html](http://elasticsearch-users.115913.n3.nabble.com/Nested-array-doc-query-tp3192629p3197785.html)  
> Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<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:59am UTC](https://discuss.elastic.co/t/nested-array-doc-query/4931/5 "2017-07-06T03:59:26Z")

</div>

Closed on July 6, 2017, 3:59am UTC


