Convert Timestamp format in elasticsearch - elasticsearch

I want to change "2020-06-16T20:29:56.256+10:00" format to "2020-06-16T20:29:56.256Z" this format. How can I do that in Elasticsearch query.
Please help,

You can use a customized date format as shown in the below index mapping
{
"mappings": {
"properties": {
"timestamp": {
"type": "date",
"format": "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"
}
}
}
}

Related

how to set the default time in elasticsearch 6.2.1

_timestamp has been deprecated, reference documentation says we should have our own fields which will have time, how we set the default value to current timestamp fields in milliseconds in ElasticSearch 6.2:
{
"properties":{
"defautlt_time":{
"type":"date",
"default_value":"current_now()"
}
}
}
You could use a "date" type and use the build in format epoch_millis
That means first change your mappings
PUT my_index
{
"mappings": {
"_doc": {
"properties": {
"default_time": {
"type": "date",
"format": "epoch_millis"
}
}
}
}
}
and then set the current time in your client.

how to use filter in ElasticSearch?

I'm trying to implement filter using ElasticSearch I'm simply want to implement range filter I've the following data:
{
"result": [
{
"Id": "144039",
"posted_dt": 1506951883637,
"submit_dt": 1507609800000,
"title": "Request for Information (RFI) # 306-18-0018",
"fname": "RODRI",
"email": "",
"desc": "dummy Text"
}
]
}
I want to get data from last 3 or 5 days I'm using this :
query = {
"bool": {
"must": [
{
"range" : {
"posted_dt" : {
"gte" : "now-3d/d",
"lt" : "now/d"
}
}
} ]
}
}
My mapping for posted_dt is :
"posted_dt": {
"type": "long"
},
I did try the filter as well but didn't succeed.
Please help.
Thanks
Randheer
Your mapping of "posted_dt" field is incorrect. You intend to store date which is in epoch in millis but you are storing it as long type. So the date range filter won't work on long datatype. Update your "posted_dt" field's mapping like :
PUT my_index
{
"mappings": {
"my_type": {
"properties": {
"posted_dt": {
"type": "date",
"format": "epoch_millis"
}
}
}
}
}
Refer Date datatype in Elasticsearch.
First you need to share your mapping. Actually make sure that posted_dt and submit_dt are defined as date in your mapping. Here you are using a long which is incorrect to deal with dates.
A side note is that you should use filter instead of must in your case. Will be faster IMO.

elasticsearch-hadoop exception about date type mapping

I got the following exception, I'm very confused about this:
org.elasticsearch.hadoop.rest.EsHadoopParsingException:
Cannot parse value [2016-03-13T02:32:56] for field [create_time]
My mapping as following:
"mappings": {
"users": {
"properties": {
"create_time": {
"type": "date",
"format": "strict_date_optional_time||epoch_millis"
}
}
}
it should be
"format": "strict_date_time_no_millis"
date_time_no_millis or strict_date_time_no_millis
A formatter that combines a full date and time without millis,
separated by a T: yyyy-MM-dd'T'HH:mm:ssZZ. custom-date-format

elasticsearch- how to find exact date type values?

So I'm using elasticsearch with spring framework, and I'm having trouble get hits by exact date value.
Here's my property mapping:
{
"properties": {
"creationDate": {
"type": "date",
"format": "dd-MM-yyyy HH:mm:ss"
},
...
}
}
Here's the mapping in the java class:
#Field(type = Date, format = DateFormat.custom, pattern ="dd-MM-yyyy HH:mm:ss")
public Calendar creationDate;
The problem is when I try to search for an exact date:
GET test/searchableSo/_search
{
"query": {
"term": {
"creationDate": {
"value": "14-11-2014 05:55:46"
}
}
}
}
It doesn't return anything, only if I use the long equivalent:
{
"query": {
"term": {
"creationDate": {
"value": "1415987746214"
}
}
}
}
Any insight?
Usually it is safer to use the range filter instead of term/match when dealing with date fields.
Elasticsearch internally stores date type as a long value.
So I believe passing 1415987746214 while indexing should end up storing the value as is.
Hence 1415987746214 is not the same as "14-11-2014 05:55:46". because of the millisecond portion.
Try indexing it without the millisecond portion i.e. "1415987746000"
or you could use the numeric_resolution setting to be seconds in the mapping and specify timestamp in seconds since epoch while indexing i.e. 1415987746
Example:
"properties": { "creationDate": { "type": "date", "format": "dd-MM-yyyy HH:mm:ss" ,"numeric_resolution" : "seconds"},..}
either would work for the query :
{ "query": { "term": { "creationDate": { "value": "14-11-2014 17:55:46" } } } }
remember to use the 24 hour clock.

Date_histogram Elasticsearch facet can't find field

I am using the date_histogram facet to find results based on a Epoch timestamp. The results are displayed on a histogram, with the date on the x-axis and count of events on the y-axis. Here is the code that I have that doesn't work:
angular.module('controllers', [])
.controller('FacetsController', function($scope, $http) {
var payload = {
query: {
match: {
run_id: '9'
}
},
facets: {
date: {
date_histogram: {
field: 'event_timestamp',
factor: '1000',
interval: 'second'
}
}
}
}
It works if I am using
field: '#timestamp'
which is in ISO8601 format; however, I need it to now work with Epoch timestamps.
Here is an example of what's in my Elasticsearch, maybe this can lead to some answers:
{"#version":"1",
"#timestamp":"2014-07-04T13:13:35.372Z","type":"automatic",
"installer_version":"0.3.0",
"log_type":"access.log","user_id":"1",
"event_timestamp":"1404479613","run_id":"9"}
},
When I run this, I receive this error:
POST 400 (Bad Request)
Any ideas as to what could be wrong here? I don't understand why I'd have such a difference from using the two different fields, as the only difference is the format. I researched as best I could and discovered I should be using 'factor', but that didn't seem to solve my problem. I am probably making a silly beginner mistake!
You need to set the indexing initially. Elasticsearch is good at defaults but it is not possible for it to determine if the provided value is a timestamp, integer or string. So its your job to tell Elasticsearch about the same.
Let me explain by example. Lets consider the following document is what you are trying to index:
{
"#version": "1",
"#timestamp": "2014-07-04T13:13:35.372Z",
"type": "automatic",
"installer_version": "0.3.0",
"log_type": "access.log",
"user_id": "1",
"event_timestamp": "1404474613",
"run_id": "9"
}
So initially you don't have an index and you index your document by making an HTTP request like so:
POST /test/date_experiments
{
"#version": "1",
"#timestamp": "2014-07-04T13:13:35.372Z",
"type": "automatic",
"installer_version": "0.3.0",
"log_type": "access.log",
"user_id": "1",
"event_timestamp": "1404474613",
"run_id": "9"
}
This creates a new index called test and a new doc type in index test called date_experiments.
You can check the mapping of this doc type date_experiments by doing so:
GET /test/date_experiments/_mapping
And what you get in the result is an auto-generated mapping that was generated by Elasticsearch:
{
"test": {
"date_experiments": {
"properties": {
"#timestamp": {
"type": "date",
"format": "dateOptionalTime"
},
"#version": {
"type": "string"
},
"event_timestamp": {
"type": "string"
},
"installer_version": {
"type": "string"
},
"log_type": {
"type": "string"
},
"run_id": {
"type": "string"
},
"type": {
"type": "string"
},
"user_id": {
"type": "string"
}
}
}
}
}
Notice that the type of event_timestamp field is set to string. Which is why your date_histogram is not working. Also notice that the type of #timestamp field is already date because you pushed the date in the standard format which made easy for Elasticsearch to recognize your intention was to push a date in that field.
Drop this mapping by sending a DELETE request to /test/date_experiments and lets start from the beginning.
This time instead of pushing the document first, we will make the mapping according to our requirements so that our event_timestamp field is considered as a date.
Make the following HTTP request:
PUT /test/date_experiments/_mapping
{
"date_experiments": {
"properties": {
"#timestamp": {
"type": "date"
},
"#version": {
"type": "string"
},
"event_timestamp": {
"type": "date"
},
"installer_version": {
"type": "string"
},
"log_type": {
"type": "string"
},
"run_id": {
"type": "string"
},
"type": {
"type": "string"
},
"user_id": {
"type": "string"
}
}
}
}
Notice that I have changed the type of event_timestamp field to date. I have not specified a format because Elasticsearch is good at understanding a few standard formats like in the case of #timestamp field where you pushed a date. In this case, Elasticsearch will be able to understand that you are trying to push a UNIX timestamp and convert it internally to treat it as a date and allow all date operations on it. You can specify a date format in the mapping just in case the dates you are pushing are not in any standard formats.
Now you can start indexing your documents and starting running your date queries and facets the same way as you were doing earlier.
You should read more about mapping and date format.

Resources