[英]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"
}
}
}
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.