Elastic search - Query String date search possibilities - elasticsearch

I have date field in ES data and I want to make query string search, to get rows, where date value is from last 3 months. I can do that like this: (created_at:[2022-03-01 10:50:00 TO now]), but I'm looking for something like this - (created_at:[now-3M TO now]) Looks like now-3M syntax can't be used in query string search. Is there a way, to search last 3 months without adding a strict date from?

Related

Using ranges for timestamp in query string query

I'm using query string query to retrieve data from api where NAME field equals TEST and its being updated TODAY.
Lastupdate field is timestamp format (2019-11-09 10:04:56.530000000)
I tried to do it this way, Which do not throw error but it clearly do not work as well, some of records are from months ago some of them yrs ago and I want to query only today.
/data/_search?q=name:TEST lastupdate:[now-1d/d TO now/D]
P.S I know how can I do it with query dsl with gte lt attributes of ranges but as I will be using this data in Power BI I have to feed json to it via URL so thats why I'm searching ways to do it in URI.
Maybe you're just missing an AND from your query param? Does the following give you your expected results?
GET /data/_search?q=name:TEST+AND+lastupdate:[now-1d/d+TO+now/d]

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.

ElasticSearch query specifying an indexname using todays date

I'm using logstash to populate ES with a number of metrics from our live services across a number of machines. Logstash creates a new index each day and i am finding that querying ES without specifying the index, is running slowly. ( i currently maintain 5 days of indicies). If i specify the specific index eg today
.es(index=logstash-2018.01.15, q= examplequery
it runs very quickly
Is there a way i can specify todays index using the date field?
eg
.es(index=logstash-'get date', q= examplequery
You can use the query for getting the indices of today's date:
.es(index='<logstash-{now/d}>')
An interesting read with all the options available in elastic search to include date math in index names:
https://www.elastic.co/guide/en/elasticsearch/reference/current/date-math-index-names.html
By looking at the syntax I guess you are using Timelion or something that uses query string. There is a good tutorial here that includes specifying index patterns:
https://www.elastic.co/blog/timelion-tutorial-from-zero-to-hero
In your case it will be
.es(index=logstash-*, q= examplequery
or
.es(index=logstash-2018.01.*, q= examplequery
if you need this year january and the index pattern is 'logstash-YYYY.MM.dd'

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 find the date range in uri request search in elasticsearch

I have a search in which i need to find the delta of data
http://localhost:9200/index/index_type/_search?q=sampledate[21-02-2015 TO 22-02-2015]
but this search is giving me error
could anybody help?
You can use below query:
GET /index_name/index_type/_search?q=dateCreated:[2016-01-06+TO+2016-01-07]
This will work only if dateCreated is a date field. Won't work with String
We had similar weird issue with this date field in Elastic Search 7.6.1.
We found working solution by removing colon(:) after date fields and surrounding entire date query part with brackets.
i.e.
GET /index_name/index_type/_search?q=dateCreated:[2016-01-06+TO+2016-01-07]
Above query changed to
GET /index_name/index_type/_search?q=(dateCreated[2016-01-06+TO+2016-01-07]) This should work

Resources