【问题标题】:Elasticsearch range filter on strings returning incorrect results对返回不正确结果的字符串进行 Elasticsearch 范围过滤
【发布时间】:2015-03-04 07:21:10
【问题描述】:

在我的索引中,我有一个名为“时间”的字段,其类型为“字符串”。还有另一个类型为“日期”的字段“日期”。

当我尝试查询索引并根据时间过滤结果时,我得到的结果不正确。例如,我的查询是: {"and": { "filters": [ { "term": { "date": "2015-02-06" } }, { "range": { "time": { "from": "22:11 ", "to": "23:59", "include_lower": true, "include_upper": true } } }

在结果中,我得到日期为“2015-02-06”但时间为“16:01”、“07:01”等的字段。

我做错了什么?

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    您需要像这样格式化您的时间字段,以便您的范围过滤器起作用。我不认为"range" 知道如何处理"string" 字段。

    这对我有用:"time" : { "type": "date", "format": "HH:mm" }

    这是我使用的完整示例:

    DELETE /test_index
    
    PUT /test_index
    {
       "settings": {
          "number_of_shards": 1
       },
       "mappings": {
          "doc": {
             "properties": {
                 "time" : { "type": "date", "format": "HH:mm" },
                 "date" : { "type": "date" }
             }
          }
       }
    }
    
    POST /test_index/_bulk
    {"index":{"_index":"test_index","_type":"doc","_id":1}}
    {"date": "2015-02-06","time": "16:01"}
    {"index":{"_index":"test_index","_type":"doc","_id":2}}
    {"date": "2015-02-06","time": "07:01"}
    {"index":{"_index":"test_index","_type":"doc","_id":3}}
    {"date": "2015-02-06","time": "22:30"}
    {"index":{"_index":"test_index","_type":"doc","_id":4}}
    {"date": "2015-02-08","time": "15:30"}
    
    POST /test_index/_search
    {
       "query": {
          "filtered": {
             "query": {
                "match_all": {}
             },
             "filter": {
                "and": {
                   "filters": [
                      {
                         "term": {
                            "date": "2015-02-06"
                         }
                      },
                      {
                         "range": {
                            "time": {
                               "from": "22:11",
                               "to": "23:59",
                               "include_lower": true,
                               "include_upper": true
                            }
                         }
                      }
                   ]
                }
             }
          }
       }
    }
    ...
    {
       "took": 5,
       "timed_out": false,
       "_shards": {
          "total": 1,
          "successful": 1,
          "failed": 0
       },
       "hits": {
          "total": 1,
          "max_score": 1,
          "hits": [
             {
                "_index": "test_index",
                "_type": "doc",
                "_id": "3",
                "_score": 1,
                "_source": {
                   "date": "2015-02-06",
                   "time": "22:30"
                }
             }
          ]
       }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-15
      • 1970-01-01
      • 2019-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多