2013-03-26 139 views
15

我試圖尋找所有誰有一個範圍之內日期字段中的項目,它失敗(返回任何結果)過濾按日期elasticsearch

查詢:

{ 
    "query": { 
    "filtered": { 
     "query": { 
     "match_all": {} 
     }, 
     "filter": { 
     "range": { 
      "last_updated": { 
      "from": "2013-01-01 00:00:00" 
      } 
     } 
     } 
    } 
    } 
} 

的映射:

{ 
     took: 1 
     timed_out: false 
     _shards: { 
      total: 1 
      successful: 1 
      failed: 0 
     } 
     hits: { 
      total: 0 
      max_score: null 
      hits: [ ] 
     } 
    } 

{ 
    "my_doctype": { 
     "_timestamp": { 
      "enabled": "true" 
     }, 
     "properties": { 
      "cards": { 
       "type": "integer" 
      }, 
      "last_updated": { 
       "type": "date", 
       "format": "yyyy-MM-dd HH:mm:ss" 
      } 
     } 
    } 
} 

在結果

使用數值篩選整數字段(「卡」)的範圍的相同查詢可以正常工作。 將日期更改爲較早的開始(1900-01-01 00:00:00)也不會顯示結果。

我在做什麼錯?

順便說一句,我知道我在映射中啓用了_timestamp,但那不是我正在過濾的字段。

回答

26

看起來完全正常工作對我說:

curl -XPUT localhost:9200/test -d '{ 
    "settings": { 
     "index.number_of_shards": 1, 
     "index.number_of_replicas": 0 
    }, 
    "mappings": { 
     "doc": { 
      "_timestamp": { 
       "enabled": "true" 
      }, 
      "properties": { 
       "cards": { 
        "type": "integer" 
       }, 
       "last_updated": { 
        "type": "date", 
        "format": "yyyy-MM-dd HH:mm:ss" 
       } 
      } 
     } 
    } 
} 
' 
curl -XPOST localhost:9200/test/doc/1 -d '{ 
    "last_updated": "2012-01-01 12:13:14" 
} 
' 
curl -XPOST localhost:9200/test/doc/2 -d '{ 
    "last_updated": "2013-02-02 01:02:03" 
} 
' 
curl -X POST 'http://localhost:9200/test/_refresh' 
echo 
curl -X GET 'http://localhost:9200/test/doc/_search?pretty' -d '{ 
    "query": { 
     "filtered": { 
      "query": { 
       "match_all": {} 
      }, 
      "filter": { 
       "range": { 
        "last_updated": { 
         "gte": "2013-01-01 00:00:00" 
        } 
       } 
      } 
     } 
    } 
} 
' 
+1

謝謝。這是我的一個錯字 - 多麼尷尬。 我要在這裏留下問題並標記爲已接受,供參考,如果有人需要看到測試用例。或者刪除它更有意義? – eran 2013-03-26 18:55:39

+14

@eran第四次在谷歌搜索「elasticsearch日期搜索(至少對我來說)。保留它,這對我很有用=) – markdsievers 2013-10-15 04:10:17

+0

@Gil你可以更具體一些嗎? – imotov 2014-03-04 15:33:39