Baby.zhou
Baby.zhou

Reputation: 561

Range query for a keyword or a date type field?

I have a field which store the insert time,such as 2016-10-10 11:00:00.000,I tried keyword type and date type,they all meet the range requirements,such as

{
    "query": {
        "range" : {
            "time" : {
                "gte" : "2016-10-10 11:00:00.000",
                "lte" : "2016-10-10 12:00:00.000"
            }
        }
    }
}

keyword and date type which is better?

Upvotes: 0

Views: 1284

Answers (1)

Val
Val

Reputation: 217254

In your case, since you're storing dates, it's more appropriate to use the date data type, indeed. Internally, those dates will be stored as a long timestamps and the range query will be run on them, so that you have a numerical range.

keyword is intended to be used for string data. If you store those dates as keyword, your dates will be stored as unanalyzed strings and the range query that will be run on them will consider them as a lexical range.

If you ever need to create date_histogram aggregation out of those dates, the keyword type won't do it. So you should definitely prefer the date data type.

Upvotes: 5

Related Questions