Nested array doc query


(tsolakp) #1

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();


(Shay Banon) #2

You need to explicitly wrap it in a nested query:
http://www.elasticsearch.org/guide/reference/query-dsl/nested-query.html.

On Sat, Jul 23, 2011 at 2:33 AM, tsolakp 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
Sent from the ElasticSearch Users mailing list archive at Nabble.com.


(tsolakp) #3

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 ) )


(Shay Banon) #4

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 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
Sent from the ElasticSearch Users mailing list archive at Nabble.com.


(system) #5