Elasticsearch date field: epoch millis input, string output? - elasticsearch

Steps:
1. Define a date field in a mapping.
2. Insert a epoch millisecond (long) value into that field.
Can elastic search returns a string value (yyyy-MM-ddTHH:mm:SS) of that field for a search?

From what I understand of the date-format documentation of ElasticSearch, it will always accept a milliseconds-since-epoch input next to input in the format given by the format, and it will produce a String output using the (first) format given. If you don't provide a format, then the "date_optional_time" format will be used (yyyy-MM-dd’T'HH:mm:ss.SSSZZ).
If the time zone in there is a problem for you, you'd need to give ElasticSearch your intended format.

I don't have the code to hand, but in my testing I believe I managed to do the following:
I used the date formatter on the field and the query fields definition to do this:
curl -XGET 'http://localhost:9200/twitter/tweet/1?fields=title,date_field.date_time'
using the date formats specified here: http://www.elasticsearch.org/guide/reference/mapping/date-format/
If you want a full document returned, this may be onerous. In which case is it possible to use an alias 'view' mapping to get the result to return differently from your primary mapping? Possibly this has become a half-answer.

Related

Keyword search using Elastic search

I am new to Elasticsearch and I am trying to achieve a Text Search functionality using Elasticsearch. I have over 100 documents and every document has lines starting with timestamp notations.
Eg.
00:00:00 - 00:01:00 This is the first line
00:01:01 - 00:02:30 This is the second line
00:02:30 - 00:03:45 This is the third line
00:03:46 - 00:05:00 This is the fourth line
00:05:01 - 00:06:00 This is fifth line
...
And so on.
I am splitting each of these lines into different paragraphs and performing a text search over the documents.
Now, I want to search by keyword wherein 1 or more keywords would be defined for let's say lines between timestamp 00:00:00 - 00:05:00. So based on the keyword search, the entire data from 00:00:00 - 00:05:00 should be returned. As in all the lines in between these timestamps should be returned based on keyword search.
Can you please help me understand how to achieve this functionality using Elasticsearch?
Thanks in advance!!
As per i understand below is my opinion:
It is better to create one more field (type can be datetime, timestamp) in your schema and perform range query on that field. Because it will be going to use very frequently and your data will store in time series manner.
[Not recommended] If you field type is "keyword" where you have stored your whole string, Then you need use wildcard query with '%youstring'. But this will return partial data only. And offcourse it is heavy cost and slow. It is like query in SQL.
[Not recommended] If you field type is "text" then you need to check wheather date time terms is created or not. This is also return partial data only.
It is best to design you schema according to your search query. 1st option will be better for you and it will help to scale in future.

storing date value as "Long" instead of "Date" in Elastic-Search index

I have an Elastic-Search index which stores products with 'createdTime' field which signifies when a document/product is added, currently i am saving 'createdTime' as 'Long' instead of 'Date' and in that field i am storing milliseconds from epoch, My question is will storing 'createdTime' as 'Long' instead of 'Date' makes sorting or any such operations slow.
ES version: 2.4.1
Yes, Long is faster but let Elastic handle that.
Internally, fields with Date datatype are stored as long.
Following is the excerpt from Elastic Docs:
Internally, dates are converted to UTC (if the time-zone is specified) and stored as a long number representing milliseconds-since-the-epoch
Storing the date as long datatype requires you to do extra processing on your side.
In addition, you are loosing the ability to view the date field as like date. For ease of use, looking at a long date isn't right.
If you use Date datatype, elastic will store the field in its inverted index as a long value. However, it will convert it to viewable date format before retrieving it back to you.
So, just use Date datatype.

How to get timestamp from date in elastic search script

As the title suggests, I want to get the timestamp (as a number) from a date type in an elastic search painless script. The following attempts didn't work: doc["date_field"].value, doc["date_field"].date.getMillis().
According to the Painless docs you should be able to access the milliseconds since epoch like this: doc.date_field.millis.
Date fields are exposed as ReadableDateTime, so they support methods
like getYear, getDayOfWeek or e.g. getting milliseconds since epoch
with getMillis. To use these in a script, leave out the get prefix and
continue with lowercasing the rest of the method name.
You should also be able to use the methods from ReadableDateTime. https://www.elastic.co/guide/en/elasticsearch/painless/6.4/painless-api-reference.html#painless-api-reference-org-joda-time-ReadableDateTime

Visualizing a single string of text in Kibana

In Kibana, I have an index that looks like as follows
type (String)
value (String)
timestamp (Date)
I would like to have a visualization that shows the most recent value field where the type is equal to "battery", for example.
I would like the visualization to be similar to the "Metric" one, but displaying a string of text instead of a number, of course.
Is this possible with Kibana? If not, how can I get a similar result?
You can use a Data Table visualization.
In the search query you would specify type: "Battery"
In the metric section you would specify Max timestamp
In the Split Rows section you would specify Aggregation=Terms, Field=value, OrderBy=metric:Max timestamp, Order=descending, Size=1
You will have a result that is a table with 1 row and 2 columns, one of which being a value and the other a timestamp
If this does not satisfy your needs, you may look into available Kibana plugins that allow new visualizations (see the list of known plugins) or modify one of them to suite your needs.

How to query a blank date in elasticsearch

Is there a way in elasticsearch to query for a date type with a blank/empty value? What value gets assigned in the index to blank date fields?
Must I use the missing filter, or is there a way to use a query - a term maybe?
Thanks.
Unless you have a null_value specified on the date field, I believe missing filter is the recommended way.
This answer in elasticsearch discussion group talks about value being null in query is treated similar to the value not present the way elasticsearch looks at it.

Resources