Elasticsearch在范围查询中找不到值

时间:2016-04-27 12:33:46

标签: date elasticsearch range

我启动以下查询:

GET archive-bp/_search
{
  "query": {
  "bool" : {
    "filter" : [ {
      "bool" : {
        "should" : [ {
          "terms" : {
            "naDataOwnerCode" : [ "ACME-FinServ", "ACME-FinServ CA", "ACME-FinServ NY", "ACME-FinServ TX", "ACME-Shipping APA", "ACME-Shipping Eur", "ACME-Shipping LATAM", "ACME-Shipping ME", "ACME-TelCo-CN", "ACME-TelCo-ESAT", "ACME-TelCo-NL", "ACME-TelCo-PL", "ACME-TelCo-RO", "ACME-TelCo-SA", "ACME-TelCo-Treasury", "Default" ]
          }
        },
        {
          "bool" : {
            "must_not" : {
              "exists" : {
                "field" : "naDataOwnerCode"
              }
            }
          }
        } ]
      }
    }, {
      "range" : {
        "bankCommunicationStatusDate" : {
          "from" : "2006-02-27T06:45:47.000Z",
          "to" : null,
          "time_zone" : "+02:00",
          "include_lower" : true,
          "include_upper" : true
        }
      }
    } ]
  }
}
}

我没有收到任何结果,但该字段存在于我的索引中。 当我剥离数据所有者部分时,我仍然没有结果。当我剥离bankCommunicationDate时,我得到10个结果,所以有问题。 只查询bankCommunicationDate:

GET archive-bp/_search 
{
  "query" :
     {
      "range" : {
        "bankCommunicationStatusDate" : {
          "from" : "2016-04-27T09:45:43.000Z",
          "to" : "2026-04-27T09:45:43.000Z",
          "time_zone" : "+02:00",
          "include_lower" : true,
          "include_upper" : true
        }
      }
  }
}

我的索引的映射包含以下bankCommunicationStatusDate字段:

"bankCommunicationStatusDate": {
                "type": "date",
                "format": "strict_date_optional_time||epoch_millis"
              }

弹性搜索中的字段 bankCommunicationStatusDate 有值:

  • “bankCommunicationStatusDate”:“2016-04-27T09:45:43.000Z”
  • “bankCommunicationStatusDate”:“2016-04-27T09:45:47.000Z”

有什么问题?

1 个答案:

答案 0 :(得分:2)

您使用的是哪种版本的弹性搜索? 我想原因是你应该使用" gte / lte"而不是" from / to / include_lower / include_upper"。

根据0.90.4版的文档 https://www.elastic.co/guide/en/elasticsearch/reference/0.90/query-dsl-range-query.html

Deprecated in 0.90.4.
The from, to, include_lower and include_upper parameters have been deprecated in favour of gt,gte,lt,lte.

奇怪的是,我在弹性搜索版本1.7上尝试了你的例子并返回数据! 我想真正的折旧发生的时间要晚得多 - 介于1.7和你可能的新版本之间。

顺便说一句。您可以使用Sense plugin for Chrome和此代码进一步隔离问题:

DELETE /test
PUT /test
{
      "mappings": {
          "myData" : {
              "properties": { 
                "bankCommunicationStatusDate": {
                "type": "date"
              }
        }
    }
    }
}

PUT test/myData/1
{
    "bankCommunicationStatusDate":"2016-04-27T09:45:43.000Z"
}
PUT test/myData/2
{
    "bankCommunicationStatusDate":"2016-04-27T09:45:47.000Z"
}


GET test/_search
{
  "query" :
     {
      "range" : {
        "bankCommunicationStatusDate" : {
          "gte" : "2016-04-27T09:45:43.000Z",
          "lte" : "2026-04-27T09:45:43.000Z"
        }
      }
  }
}