Filter documents by nested objects


(Harry Walter) #1

Based on this data i've been testing
with: https://gist.github.com/a0d78e7c32ec76f03681 I've been trying to work
out how to get a set of documents back based on the nested objects.

The filter needs to be an AND however this seems to match nothing when I
try.

Does anyone know how to filter (or query) documents based on it's nested
objects and using an AND type operation to recover documents that have
matching nested objects?

--


(Кирилл Шнуров) #2

Try to omit 'match_all' and 'and' (you don't need 'and' for 1 query) in
nested filter:

'{
"fields": ["title", "slug", "tags"],
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"nested": {
"_scope": "tag_search",
"path": "tags",
"query": {
"term": { "tags.slug": [ "cars", "news" ] }
}
}
}
}
}
}
}'

вторник, 30 октября 2012 г., 21:35:32 UTC+4 пользователь Harry Walter
написал:

Based on this data i've been testing with:
https://gist.github.com/a0d78e7c32ec76f03681 I've been trying to work out
how to get a set of documents back based on the nested objects.

The filter needs to be an AND however this seems to match nothing when I
try.

Does anyone know how to filter (or query) documents based on it's nested
objects and using an AND type operation to recover documents that have
matching nested objects?

--


(Кирилл Шнуров) #3

Also there's a typo: "term": { "tags.slug": [ "cars", "news" ] }
Should be "terms": { "tags.slug": [ "cars", "news" ] }

вторник, 30 октября 2012 г., 23:25:28 UTC+4 пользователь Кирилл Шнуров
написал:

Try to omit 'match_all' and 'and' (you don't need 'and' for 1 query) in
nested filter:

'{
"fields": ["title", "slug", "tags"],
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"nested": {
"_scope": "tag_search",
"path": "tags",
"query": {
"term": { "tags.slug": [ "cars", "news" ] }
}
}
}
}
}
}
}'

вторник, 30 октября 2012 г., 21:35:32 UTC+4 пользователь Harry Walter
написал:

Based on this data i've been testing with:
https://gist.github.com/a0d78e7c32ec76f03681 I've been trying to work
out how to get a set of documents back based on the nested objects.

The filter needs to be an AND however this seems to match nothing when I
try.

Does anyone know how to filter (or query) documents based on it's nested
objects and using an AND type operation to recover documents that have
matching nested objects?

--


(Harry Walter) #4

Thanks for the reply.

Testing with your query returns me all results as it is doing an OR on the
tags.slug field. Say I have two documents, both have a nested tag with slug
"cars", each also has a secondary nested tag, first document with "news"
seconds with "reviews". With your query I get back all documents, however
I'd only want to one that has BOTH "cars" and "news" for the tags.slug
field.

On Tuesday, October 30, 2012 7:27:02 PM UTC, Кирилл Шнуров wrote:

Also there's a typo: "term": { "tags.slug": [ "cars", "news" ] }
Should be "terms": { "tags.slug": [ "cars", "news" ] }

вторник, 30 октября 2012 г., 23:25:28 UTC+4 пользователь Кирилл Шнуров
написал:

Try to omit 'match_all' and 'and' (you don't need 'and' for 1 query) in
nested filter:

'{
"fields": ["title", "slug", "tags"],
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"nested": {
"_scope": "tag_search",
"path": "tags",
"query": {
"term": { "tags.slug": [ "cars", "news" ] }
}
}
}
}
}
}
}'

вторник, 30 октября 2012 г., 21:35:32 UTC+4 пользователь Harry Walter
написал:

Based on this data i've been testing with:
https://gist.github.com/a0d78e7c32ec76f03681 I've been trying to work
out how to get a set of documents back based on the nested objects.

The filter needs to be an AND however this seems to match nothing when I
try.

Does anyone know how to filter (or query) documents based on it's nested
objects and using an AND type operation to recover documents that have
matching nested objects?

--


(Igor Motov) #5

If I understood you correctly, you are trying to find one term in one
nested document and another term in another nested document and both terms
have to be present for the parent to match. If this is the case, you need
to move "AND" operation to the parent document level because nested query
can only match a single nested document at a
time: https://gist.github.com/3e9a2daf3e6f2b12412b#file_search_documents.sh

BTW, if this is a frequently used query, it might make sense to pull the
tags to the parent by setting include_in_parent=true on the nested object
mapping.

On Wednesday, October 31, 2012 7:06:28 AM UTC-4, Harry Walter wrote:

Thanks for the reply.

Testing with your query returns me all results as it is doing an OR on the
tags.slug field. Say I have two documents, both have a nested tag with slug
"cars", each also has a secondary nested tag, first document with "news"
seconds with "reviews". With your query I get back all documents, however
I'd only want to one that has BOTH "cars" and "news" for the tags.slug
field.

On Tuesday, October 30, 2012 7:27:02 PM UTC, Кирилл Шнуров wrote:

Also there's a typo: "term": { "tags.slug": [ "cars", "news" ] }
Should be "terms": { "tags.slug": [ "cars", "news" ] }

вторник, 30 октября 2012 г., 23:25:28 UTC+4 пользователь Кирилл Шнуров
написал:

Try to omit 'match_all' and 'and' (you don't need 'and' for 1 query) in
nested filter:

'{
"fields": ["title", "slug", "tags"],
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"nested": {
"_scope": "tag_search",
"path": "tags",
"query": {
"term": { "tags.slug": [ "cars", "news" ] }
}
}
}
}
}
}
}'

вторник, 30 октября 2012 г., 21:35:32 UTC+4 пользователь Harry Walter
написал:

Based on this data i've been testing with:
https://gist.github.com/a0d78e7c32ec76f03681 I've been trying to work
out how to get a set of documents back based on the nested objects.

The filter needs to be an AND however this seems to match nothing when
I try.

Does anyone know how to filter (or query) documents based on it's
nested objects and using an AND type operation to recover documents that
have matching nested objects?

--


(system) #6