Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 仅当字段存在时按字段筛选,弹性搜索_Python_<img Src="//i.stack.imgur.com/RUiNP.png" Height="16" Width="18" Alt="" Class="sponsor Tag Img">elasticsearch - Fatal编程技术网 elasticsearch,Python,elasticsearch" /> elasticsearch,Python,elasticsearch" />

Python 仅当字段存在时按字段筛选,弹性搜索

Python 仅当字段存在时按字段筛选,弹性搜索,python,elasticsearch,Python,elasticsearch,我的文档中有一个日期字段,我只想返回日期小于现在-5m的文档,但是不是所有文档都有该字段,它们只在第一次单独查询时收到 documents = es.search(index='index_name', size=10000, body={ "query": { "bool": { "must": [ { "range": { "time_lockout":

我的文档中有一个日期字段,我只想返回日期小于现在-5m的文档,但是不是所有文档都有该字段,它们只在第一次单独查询时收到

 documents = es.search(index='index_name', size=10000, body={
          "query": {
    "bool": {
      "must": [
                {
              "range": {
                  "time_lockout": {
                      "lt": "now-5m"
                  }
              }
          }
      ],
      "filter": [

      ],
      "should": [

      ],

      "must_not": [
      ]
    }
  }})
所以我的伪代码是

if `time_lockout` exists
    give documents that are now-5 including all documents thats dont have `time_lockout` 

Exclude documents that dat range falls withon that 5 minute window

将查询更新到以下位置:

{
  "query": {
    "bool": {
      "should": [
        {
          "range": {
            "time_lockout": {
              "lt": "now-5m"
            }
          }
        },
        {
          "bool": {
            "must_not": [
              {
                "exists": {
                  "field": "time_lockout"
                }
              }
            ]
          }
        }
      ]
    }
  }
}