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

时间:2016-04-20 01:56:29

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

2 个答案:

答案 0 :(得分:0)

我刚刚意识到的一个解决方案:

&#13;
&#13;
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: {}
    }
  }
}
&#13;
&#13;
&#13;

答案 1 :(得分:0)

这个怎么样:

    你的地图中的
  • 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"
      }
    }
  }
}
相关问题