简体   繁体   English

获取 Elasticsearch 中字段值与任何数组元素匹配的所有文档

[英]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 结果与 should 并列出每个 id

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 中有两种类型的查询

  1. Term Queries - which searches the exact terms mentioned in the value术语查询 - 搜索值中提到的确切术语
  2. Full-Text queries - Which first analyses the query values, by the analyser mentioned in the field in the mapping全文查询 - 首先通过映射字段​​中提到的分析器分析查询值

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.

相关问题 ElasticSearch:筛选文档,其中数组字段中的任何值都不在列表中 - ElasticSearch: Filter for documents where any value in an array field is not in a list 查找所有数组字段值均与谓词匹配的文档 - Find documents where all array field values matches predicate 如何查找一个字段的值与另一个字段的值匹配的所有文档 - How to find all documents where the value of one field matches that of another Elasticsearch - 以任何方式找出所有字段值为文本的文档 - Elasticsearch - Any way to find out all the documents with field value as text Elasticsearch:获取字段中具有特定值的文档 - Elasticsearch: getting documents with exactly a certain value in a field 查找所有弹性搜索文档,其字段为数组 - Find all elasticsearch documents with a field that is an array Elasticsearch查询以选择包含一个字段的值的所有文档多值字段 - Elasticsearch query to select all documents where one field's value is contained multi-valued field 查找字段存在的所有文档:它有一个值,或者它是 Elasticsearch 中的 null - Find all documents where field exists literally: either it has a value or it is null in Elasticsearch ElasticSearch:从存在的所有文档中删除字段(使用 Painless?) - ElasticSearch: Delete field from all documents where it exists (with Painless?) ElasticSearch - 将一个字段值复制到所有文档的其他字段 - ElasticSearch - Copy one field value to other field for all documents
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM