I have created an index named index_test
in which completedAt
field has the following mapping:
"completedAt": {
"type": "date",
"format": "epoch_millis",
"ignore_malformed": true,
},
I'm using the following query to search the data
select count(*) as count from index_test WHERE completedAt>=1677609000000 and completedAt<=1680287399999
But the above query is giving the following error:
{
"error": {
"reason": "Invalid SQL query",
"details": ">= function expected {[BYTE,BYTE],[SHORT,SHORT],[INTEGER,INTEGER],[LONG,LONG],[FLOAT,FLOAT],[DOUBLE,DOUBLE],[STRING,STRING],[BOOLEAN,BOOLEAN],[TIMESTAMP,TIMESTAMP],[DATE,DATE],[TIME,TIME],[DATETIME,DATETIME],[INTERVAL,INTERVAL],[STRUCT,STRUCT],[ARRAY,ARRAY]}, but get [TIMESTAMP,LONG]",
"type": "ExpressionEvaluationException"
},
"status": 400
}
If I use the Range query on the completedAt then its working fine
{
"query": {
"range": {
"completedAt": {
"gte": 1677609000000,
"lte": 1680287399999
}
}
}
}
How to make the above SQL query work fine?
UPDATE:
I tried the SQL query by converting the milliseconds to Timestamp using the following:
var startTimestamp = `cast(adddate(date('1970-01-01'), INTERVAL ${1677609000000 * 1000} MICROSECOND) as timestamp)`;
var endTimestamp = `cast(adddate(date('1970-01-01'), INTERVAL ${1680287399999 * 1000} MICROSECOND) as timestamp)`;
select count(*) as count from index_test WHERE completedAt>=${startTimestamp} and completedAt<=${endTimestamp}
And the query worked fine.
Is there any other approach? Can we access the milliseconds directly from completedAt
field e.g. with completedAt.millis
?