简体   繁体   中英

Elasticsearch. Range query for strings with dashes

I'm using elasticsearch with data that have string-field with date values, like this:

"2016-01-25 18:40:18.933"

I'm trying to use range filter for getting values from date to date. For example:

"query" : {
        "filtered" : {
            "query" : {
                "range" : {
                    "createdDate" : {
                        "gte": "2015-11-01", 
                        "lte": "2016-01-25"
                    }
                }
            }
        }
      }
    }

But results doesn't contain values with "createdDate": "2015-12-14 20:28:23.557"

If I use "gte": "2015" or "gte": "2014-12-31" , then values with "createdDate": "2015-12-14" will be included in the results.

What's wrong in my query?

If you want to be able to run range queries on dates, you need to map your field as a date field , otherwise it won't work as you expect. In the mapping you shared, createdDate is a string . You need to wipe your index and create a new one with the proper mapping for the createdDate field, like this:

curl -XPOST localhost:9200/documents -d '{
   "mappings": {
      "order": {
         "properties": {
             "createdDate": {
                "type": "date"
             }
         }
      }
   }
}' 

Then you can reindex your data and your range query will work as expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM