簡體   English   中英

過濾文檔,其中數組的所有值都滿足elasticsearch中的某些條件

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

我的文檔看起來像這樣:

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

我希望獲得每個開始時間都在過去的所有文檔。 此查詢在所有時間都在將來或所有時間都在過去時有效,但如果將來只有一次(仍然匹配)則失敗。

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

我剛剛意識到的一個解決方案:

 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: {} } } } 

這個怎么樣:

  • include_in_parent: true映射中為include_in_parent: true
    "times": {
      "type": "nested",
      "include_in_parent": true,
      "properties": {
        "start": {
          "type": "date"
        }
      }
    }
  • 並使用query_string語法,而不是nested
{
  "query": {
    "query": {
      "query_string": {
        "query": "times.start:<=now AND NOT times.start:>now"
      }
    }
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM