Searching within a single nested object


(Ellery Crane-2) #1

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'
curl -XPOST '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' -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?


(Clinton Gormley) #2

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

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


(David Pilato) #3

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

David.


(Ellery Crane-2) #4

Awesome! Man, talk about timely :wink:

On Jul 6, 1:49 pm, Clinton Gormley 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


(system) #5