简体   繁体   中英

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:
    "times": {
      "type": "nested",
      "include_in_parent": true,
      "properties": {
        "start": {
          "type": "date"
        }
      }
    }
  • and use the query_string syntax, without nested :
{
  "query": {
    "query": {
      "query_string": {
        "query": "times.start:<=now AND NOT times.start:>now"
      }
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM