简体   繁体   English

过滤文档,其中数组的所有值都满足elasticsearch中的某些条件

[英]Filter documents where all values of an array meet some criteria in elasticsearch

I have documents that look like this: 我的文档看起来像这样:

{
  times: [{start: "1461116454242"},{start:"1461116454242"}]
}

I want to get all documents where every start time is in the past. 我希望获得每个开始时间都在过去的所有文档。 This query works when all times are in the future or all are in the past, but fails if only one time is in the future (still matches). 此查询在所有时间都在将来或所有时间都在过去时有效,但如果将来只有一次(仍然匹配)则失败。

 query: { filtered: { filter: { bool: { must: [ { nested: { path: "times", filter: { script : { script: "doc['start'].value < now", params: { now: Date.now() } } } } } ] } }, query: { match_all: {} } } } 

One solution that I just realized: 我刚刚意识到的一个解决方案:

 query: { filtered: { filter: { bool: { must: [ { nested: { path: "times", filter: { "bool": { "must": [ { "range": { "times.start": { lt: new Date() } } } ] } } } } ], must_not: [ { nested: { path: "times", filter: { "bool": { "must": [ { "range": { "times.start": { gte: new Date() } } } ] } } } } ] } }, query: { match_all: {} } } } 

How about this one: 这个怎么样:

  • include_in_parent: true in your mapping: include_in_parent: true映射中为include_in_parent: true
    "times": {
      "type": "nested",
      "include_in_parent": true,
      "properties": {
        "start": {
          "type": "date"
        }
      }
    }
  • and use the query_string syntax, without nested : 并使用query_string语法,而不是nested
{
  "query": {
    "query": {
      "query_string": {
        "query": "times.start:<=now AND NOT times.start:>now"
      }
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Elasticsearch:获取数组包含多个值之一的所有文档 - Elasticsearch: get all documents where array contains one of many values ElasticSearch:筛选文档,其中数组字段中的任何值都不在列表中 - ElasticSearch: Filter for documents where any value in an array field is not in a list 查询包含嵌套数组 Elasticsearch 中所有值的文档 - Query documents that contains all values in nested array Elasticsearch Elasticsearch - 过滤其中(嵌套数组之一)和(所有嵌套数组) - Elasticsearch - Filter where (one of nested array) and (all of nested array) 如何从 Elasticsearch 的过滤器中获取所有文档? - How to get all documents from a filter in Elasticsearch? Elasticsearch基于2个标准返回文档,其中一个基于另一个 - Elasticsearch to return documents based on 2 criteria where one is based on the other Elasticsearch:过滤嵌套文档中的数组元素 - Elasticsearch: Filter array elements in nested documents 如何通过数组中的计数过滤 ElasticSearch 中的文档? - How to filter documents in ElasticSearch by counting number in array? Elasticsearch:按数组过滤文档,请求中包含所有文档数组元素 - Elasticsearch: filter documents by array passed in request contains all document array elements 查找所有数组字段值均与谓词匹配的文档 - Find documents where all array field values matches predicate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM