How to 'OR' two different nested queries?

I have a User mapping like:

name: {
type: "string"
},
followed_groups: {
type: "nested",
properties: {
created_at: {
type: "string"
},
group_id: {
type: "string"
}
}
},
id: {
type: "integer"
},
member_of_groups: {
type: "nested",
properties: {
created_at: {
type: "string"
},
group_id: {
type: "string"
}
}
}

And I'd like to get users who are either following a group or a member of
it.

Each individual query is ok using nested query like:

"query":{
"nested":{
"query":{
"terms":{
"followed_groups.group_id":[
"22"
]
}
},
"path":"followed_groups"
}
}

But I cannot find a way to combine the two with a OR condition. Is it
possible please?

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Hiya

You want to wrap both nested clauses in a bool-should clause. Also, given
that you're not doing any scoring here, just using group membership as a
filter, it'd be faster to use filters instead of queries:

{
"query": {
"filtered": {
"filter": {
"bool": {
"should": [
{
"nested": {
"path": "followed_groups",
"filter": {
"terms": { "followed_groups.group_id": [22]}
}
}
},
{
"nested": {
"path": "member_of_groups",
"filter": {
"terms": { "member_of_groups.group_id": [22]}
}
}
}
]
}
}
}
}
}

While we're about it - you're not correlating eg group_id with created_at,
so you may want to consider adding include_in_parent: true to your nested
definitions. This will index the group_id and created_at fields in the
parent document as well, in which case the query just becomes:

{
"query": {
"filtered": {
"filter": {
"bool": {
"should": [
{ "terms": { "followed_groups.group_id": [22]}},
{ "terms": { "member_of_groups.group_id": [22]}}
]
}
}
}
}
}

clint

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.