Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

filtering by date in elasticsearch

Tags:

I'm Trying to search for all the items who have a field of date inside a range, and it fails (returns no results)

The query:

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

The mapping:

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

results in:

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

The same query filtered by a range for an integer field("cards") with a numeric value works fine. Changing the date to a very early start (1900-01-01 00:00:00) also shows no results.

What am I doing wrong?

BTW, I know I have the _timestamp enabled in the mapping, but that is not the field I am filtering by.

like image 781
eran Avatar asked Mar 26 '13 17:03

eran


People also ask

What are filters in Elasticsearch?

Frequently used filters will be cached automatically by Elasticsearch, to speed up performance. Filter context is in effect whenever a query clause is passed to a filter parameter, such as the filter or must_not parameters in the bool query, the filter parameter in the constant_score query, or the filter aggregation.

What is term query in Elasticsearch?

You can use the term query to find documents based on a precise value such as a price, a product ID, or a username. Avoid using the term query for text fields. By default, Elasticsearch changes the values of text fields as part of analysis. This can make finding exact matches for text field values difficult.


1 Answers

Seems to be working fine to me:

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"                     }                 }             }         }     } } ' 
like image 93
imotov Avatar answered Sep 20 '22 08:09

imotov