[英]Getting all documents in Elasticsearch where a field value matches any of array element
What I would like to do is to retrieve all documents where a field value matches any of the values in an array.我想要做的是检索字段值与数组中的任何值匹配的所有文档。
So from an array of ids: [id1, id2, id3, ..., idn]
, find all documents which has any of these ids.所以从一个 id 数组:
[id1, id2, id3, ..., idn]
,找到所有具有这些 id 的文档。
I can currently achieve that by repeating a match
inside of a should
, but it does not seem like the right approach.我目前可以通过在
should
内重复match
来实现这一点,但这似乎不是正确的方法。
{
"query": {
"bool": {
"should": [
{
"match": {
"activityOwnerId": "id1"
}
},
{
"match": {
"activityOwnerId": "id2"
}
},
... and so on
]
}
}
}
How can I get all documents which matches one of the elements in an array?如何获取与数组中的元素之一匹配的所有文档?
EDIT编辑
Using terms
query as suggested by @Val does not return any results in Kibana:使用 @Val 建议的
terms
查询不会在 Kibana 中返回任何结果:
No results when querying with Terms
:使用
Terms
查询时没有结果:
Results found (with the same id), when using should
:找到的结果(具有相同的 id),使用时
should
:
If you're looking at exact matches, you can leverage the terms
query instead, which takes an array as input:如果您正在查看完全匹配,则可以改用
terms
查询,它将数组作为输入:
{
"query": {
"terms": {
"activityOwnerId.keyword": [id1, id2, id3, ..., idn]
}
}
}
There are two types of queries in elasicsearch在 elasicsearch 中有两种类型的查询
If you want to go form term query, then Answer mentioned by @Val, is perfect,如果您想进行术语查询,那么@Val 提到的答案是完美的,
if you want to write a full-text query, you can use this如果你想写一个全文查询,你可以使用这个
GET /_search
{
"query": {
"query_string": {
"query": "activityOwnerId:(id1 OR id2 OR id3...)"
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.