Array intersect filter?

I'm trying to filter results based on whether an array field has at least
one element from another set.

I have a field containing an array of integers eg:

_source: {
...
thing_ids: [
21029,
21115,
20996,
21080,
20997
]
}

I'd like to filter the results such that thing_ids contains at least one of
a list of values, eg:

[1, 2, 3, 21115] # would return the above document
[1, 2, 3, 4] # would not

It doesn't really matter how many it contains, just that it matches at
least one of them.

I'm trying something like this but can't quite tell if this is correct:

{
"query":{"bool":{"must":[{"query_string":{"query":"search
query","fields":["description","title"],"default_operator":"AND","use_dis_max":true}}]}},
"filter":{
"or":[
{"term":{"thing_ids":22003}},
{"term":{"thing_ids":21757}},
{"term":{"thing_ids":21610}}
]
}
}

Any thoughts?

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

I would probably use: Elasticsearch Platform — Find real-time answers at scale | Elastic
{
"constant_score" : {
"filter" : {
"terms" : { "thing_ids" : [22003, 21757, 21610]}
}
}
}

Does it fit to your use case?

--
David Pilato | Technical Advocate | Elasticsearch.com
@dadoonet | @elasticsearchfr | @scrutmydocs

Le 11 mars 2013 à 17:17, Rue rue@thinlayer.co.uk a écrit :

I'm trying to filter results based on whether an array field has at least one element from another set.

I have a field containing an array of integers eg:

_source: {
...
thing_ids: [
21029,
21115,
20996,
21080,
20997
]
}

I'd like to filter the results such that thing_ids contains at least one of a list of values, eg:

[1, 2, 3, 21115] # would return the above document
[1, 2, 3, 4] # would not

It doesn't really matter how many it contains, just that it matches at least one of them.

I'm trying something like this but can't quite tell if this is correct:

{
"query":{"bool":{"must":[{"query_string":{"query":"search query","fields":["description","title"],"default_operator":"AND","use_dis_max":true}}]}},
"filter":{
"or":[
{"term":{"thing_ids":22003}},
{"term":{"thing_ids":21757}},
{"term":{"thing_ids":21610}}
]
}
}

Any thoughts?

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

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

Thanks David.

From the docs it looked like that was exactly what I was looking for (it
may still be) but with it I am getting every document returned whatever I
put in it. Whatever values (including one that should return no results)
gives me all docs in the index.

It's bound to be something in my usage:

{
"query": { "bool": { "must": [ { "term": { "state": { "term": "live" } }
}, { "term": { "library_id": { "term": 11 } } } ], "must_not": [ { "term":
{ "state": { "term": "deleted" } } } ] },
"constant_score": {
"filter": {
"terms": {
"thingy_ids": [
21029
]
}
}
}
},
"sort": [ { "taken_at": "desc" } ],
"size": 25,
"from": 0
}

On Monday, 11 March 2013 16:32:43 UTC, David Pilato wrote:

I would probably use:
Elasticsearch Platform — Find real-time answers at scale | Elastic

{
"constant_score" : {
"filter" : {
"terms" : { "thing_ids" : [22003, 21757, 21610]}
}
}
}

Does it fit to your use case?

--
David Pilato | Technical Advocate | Elasticsearch.com
@dadoonet https://twitter.com/dadoonet | @elasticsearchfrhttps://twitter.com/elasticsearchfr
| @scrutmydocs https://twitter.com/scrutmydocs

Le 11 mars 2013 à 17:17, Rue <r...@thinlayer.co.uk <javascript:>> a écrit
:

I'm trying to filter results based on whether an array field has at least
one element from another set.

I have a field containing an array of integers eg:

_source: {
...
thing_ids: [
21029,
21115,
20996,
21080,
20997
]
}

I'd like to filter the results such that thing_ids contains at least one
of a list of values, eg:

[1, 2, 3, 21115] # would return the above document
[1, 2, 3, 4] # would not

It doesn't really matter how many it contains, just that it matches at
least one of them.

I'm trying something like this but can't quite tell if this is correct:

{
"query":{"bool":{"must":[{"query_string":{"query":"search
query","fields":["description","title"],"default_operator":"AND","use_dis_max":true}}]}},
"filter":{
"or":[
{"term":{"thing_ids":22003}},
{"term":{"thing_ids":21757}},
{"term":{"thing_ids":21610}}
]
}
}

Any thoughts?

--
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 elasticsearc...@googlegroups.com <javascript:>.
For more options, visit https://groups.google.com/groups/opt_out.

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

I am not even sure if your query is valid the way it is written. The terms
filter example is wrapped by a constant score query. Try moving it to be
directly after your query.

{
"query": {
"bool": {
"must": [
{
"term": {
"state": {
"term": "live"
}
}
},
{
"term": {
"library_id": {
"term": 11
}
}
}
],
"must_not": [
{
"term": {
"state": {
"term": "deleted"
}
}
}
]
}
},
"filter": {
"terms": {
"thingy_ids": [
21029
]
}
},
"sort": [
{
"taken_at": "desc"
}
],
"size": 25,
"from": 0
}

On Tue, Mar 12, 2013 at 3:16 AM, Rue rue@thinlayer.co.uk wrote:

Thanks David.

From the docs it looked like that was exactly what I was looking for (it
may still be) but with it I am getting every document returned whatever I
put in it. Whatever values (including one that should return no results)
gives me all docs in the index.

It's bound to be something in my usage:

{
"query": { "bool": { "must": [ { "term": { "state": { "term": "live" } }
}, { "term": { "library_id": { "term": 11 } } } ], "must_not": [ { "term":
{ "state": { "term": "deleted" } } } ] },
"constant_score": {
"filter": {
"terms": {
"thingy_ids": [
21029
]
}
}
}
},
"sort": [ { "taken_at": "desc" } ],
"size": 25,
"from": 0
}

On Monday, 11 March 2013 16:32:43 UTC, David Pilato wrote:

I would probably use: http://www.elasticsearch.**
org/guide/reference/query-dsl/**terms-filter.htmlhttp://www.elasticsearch.org/guide/reference/query-dsl/terms-filter.html

{
"constant_score" : {
"filter" : {
"terms" : { "thing_ids" : [22003, 21757, 21610]}
}
}
}

Does it fit to your use case?

--
David Pilato | Technical Advocate | Elasticsearch.com
@dadoonet https://twitter.com/dadoonet | @elasticsearchfrhttps://twitter.com/elasticsearchfr
|** @scrutmydocs https://twitter.com/scrutmydocs

Le 11 mars 2013 à 17:17, Rue r...@thinlayer.co.uk a écrit :

I'm trying to filter results based on whether an array field has at least
one element from another set.

I have a field containing an array of integers eg:

_source: {
...
thing_ids: [
21029,
21115,
20996,
21080,
20997
]
}

I'd like to filter the results such that thing_ids contains at least one
of a list of values, eg:

[1, 2, 3, 21115] # would return the above document
[1, 2, 3, 4] # would not

It doesn't really matter how many it contains, just that it matches at
least one of them.

I'm trying something like this but can't quite tell if this is correct:

{
"query":{"bool":{"must":[{"query_string":{"query":"search
query","fields":["description"
,"title"],"default_operator":"**
AND","use_dis_max":true}}]}},
"filter":{
"or":[
{"term":{"thing_ids":22003}},
{"term":{"thing_ids":21757}},
{"term":{"thing_ids":21610}}
]
}
}

Any thoughts?

--
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 elasticsearc...@**googlegroups.com.

For more options, visit https://groups.google.com/**groups/opt_outhttps://groups.google.com/groups/opt_out
.

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

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

I think you have it there Ivan - thanks!

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