Date format in elasticsearch-watcher - datetime

Im trying to make wacher and i need to compair datetime in document with today-24h. So now i have this code:
"condition": {
"compare": {
"ctx.payload.event_time": {
"lte": "<{now-24h}>"
}
}
}
And this doesnt work. I think this is due to the fact that docs have 2022-02-12 00:00:00 format and "<{now-24h}>" have 2022-02-15T10:22:59.728Z format. How can i fix that without changing date format of document?

Related

elasticsearch - Date format requires exactly 3 decimals

I'm having trouble with date parsing in elasticsearch 7.10.1.
Here's (a relevant part of) the mapping for the index:
"utcTime": {
"type": "date",
"format": "strict_date_optional_time_nanos"
}
Date format reference.
Some of the documents are accepted, for example documents with:
"utcTime": "2021-02-17T09:50:13.173Z"
"utcTime": "2021-02-17T09:51:44.158Z"
Note that in both cases, there are exactly 3 decimals to the seconds.
This, on the other hand, is rejected:
"utcTime": "2021-02-17T09:51:45.07Z"
illegal_argument_exception: failed to parse date field [2021-02-17T09:51:45.07Z] with format [yyyy-MM-dd''T''HH:mm:ss.SSSXX]
In this case, there are only two decimals. I'm using Newtonsoft's JSON.net to do the serialization, with a format that should always include 3 decimals, but it doesn't seem to do so anyway. It'll include at most 3 decimals, though.
How can I tell elasticsearch to accept date formats with anywhere between 0 and 3 decimals for the seconds?
EDIT
I finally found the issue, which had nothing to do with the mapping, but rather with a pipeline processor date_index_name.
PUT _ingest/pipeline/test_reroute_pipeline
{
"description" : "Route documents to another index",
"processors" : [
{
"date_index_name": {
"field": "utcTime",
"date_rounding": "d",
"index_name_prefix": "rerouted-"
}
}
]
}
Because the date_format parameter wasn't defined, it would remember the format of the first date received. If it was 2 decimals, it would require 2 every time. If it was 3, it would require three.
Specifying the date format solved the issue for good:
PUT _ingest/pipeline/test_reroute_pipeline
{
"description" : "Route documents to another index",
"processors" : [
{
"date_index_name": {
"field": "utcTime",
"date_rounding": "d",
"index_name_prefix": "rerouted-",
"date_formats": ["ISO8601"]
}
}
]
}
I just tried on a fresh new 7.10.1 cluster and it also accepted 1, 2, 3 decimals for the seconds part.
Looking at the error message you get
illegal_argument_exception: failed to parse date field [2021-02-17T09:51:45.07Z] with format [yyyy-MM-dd''T''HH:mm:ss.SSSXX]
The format that seems to be set is yyyy-MM-dd''T''HH:mm:ss.SSSXX and it is different from strict_date_optional_time_nanos which is yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ
If you check the real mapping from your index, I'm pretty sure the utcTime field doesn't have strict_date_optional_time_nanos as the format.

Date formatting using Moment library

data() {
return {
date: Moment().format("llll"),
console.log(date)
}
}
The type of format that I am trying to achieve is: Mon, 2 Jul, however through what I have implemented so far I get: Mon, Jul 2, 2018 5:08 PM.
I am currently using the Moment date library https://momentjs.com/. however the one I am looking for is not listed in the documentation.
The formatting is fairly self-explanatory in the docs on Moment's website. It's easy to work out the correct for your example. Here's what you need:
moment().format('ddd, D MMM');
It is not clear why you are trying to format the date with "llll".
If it is for locale settings you can override the settings when instantiating locale (for example french) using an object:
moment.locale('fr', {
longDateFormat : {
LLLL : 'ddd, D MMM'
}
}
And then return the date as
Moment().format("LLLL")
If you are not using a specific locale you can just go for what BenM wrote.

Saving date in microsecond format in ElasticSearch

I am trying to save set of events from MySQL database into elastic search using jdbc input plugin with Logstash. The event record in database contains date fields which are in microseconds format. Practically, there are records in database between set of microseconds.
While importing data, Elasticsearch is truncating the microseconds date format into millisecond format. How could I save the data in microsecond format? The elasticsearch documentation says they follow the JODA time API to store date formats, which is not supporting the microseconds and truncating by adding a Z at the end of the timestamp.
Sample timestamp after truncation : 2018-05-02T08:13:29.268Z
Original timestamp in database : 2018-05-02T08:13:29.268482
The Z is not a result of the truncation but the GMT timezone.
ES supports microseconds, too, provided you've specified the correct date format in your mapping.
If the date field in your mapping is specified like this:
"date": {
"type": "date",
"format": "yyyy-MM-dd'T'HH:mm:ss.SSSSSS"
}
Then you can index your dates with the exact microsecond precision as you have in your database
UPDATE
Here is a full re-creation that shows you that it works:
PUT myindex
{
"mappings": {
"doc": {
"properties": {
"date": {
"type": "date",
"format": "yyyy-MM-dd'T'HH:mm:ss.SSSSSS"
}
}
}
}
}
PUT myindex/doc/1
{
"date": "2018-05-02T08:13:29.268482"
}
Side note, "date" datatype stores data in milliseconds in elasticsearch so here in case nanoseconds precision level are wanted in date ranges queries; the appropriate datatype is date_nanos

How do I validate a string to be a date in the format dd-M-y, and not be a past date?

I have a date field and the format is "dd-M-y", example 01-Jan-2013. First I want to check the format which must be "dd-M-y" and secondly the date shouldn't be in the past but can be today and onward.
How would I do that? I would like to use regular expressions but I don't know what a suitable one would be.
You should use DateTime.TryParseExact rather than using Regex to validate your DateTime
string testDate = "01-Jan-2013";
DateTime temp;
if (DateTime.TryParseExact(testDate,
"dd-MMM-yyyy",
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out temp) &&
(temp > DateTime.Today))
{
//Valid date and greater than today
}
else
{
//Invalid date or less than today
}
I think you should bind the user to fill the date in correct format instead of checking for it...
The Best solution in this case would be MaskEditExtender

How to format time in yaml in symfony

I'm using symfony 1.4 doctrine. I have an output in my database/browser that looks something like this:
Start time: 12:00:00 End time: 05:00:00
Now I want to change this format like this:
Start time: 12:00 pm End time: 5:00 pm
I tried to convert this in yaml in the backend application like this:
apps/backend/modules/reservation/config/generator.yaml
//some yaml code
fields:
startime: { date_format: h:i A }
endtime: { date_format: h:i A }
I use this format but it did'nt work, probably I set in the schema.yml their data types in "time" format:
reservation/config/doctrine/schema.yml
//some yaml code
columns:
startime: { type: time, notnull: true }
endtime: { type: time, notnull: true }
I set my startime and endtime to time format just to get the time value only for my project. I did'nt set it to date type because it will affect my front end application. For example if I input 5:00 the display will be 12:00 am, its because I use date type.
To turn this 5:00:00 to 5:00 pm/am in display, I set it into time data type and I use this syntax like this which I used in showSuccess and Indexsuccess template.
<?php echo date('g:i A', strtotime($reservation_application->getStartime()));
this code will format 00:00:00 into 12:00 am or something. but it doesnt change (00:00:00) in the data base. ONLY in display.
Is there a way to format this 00:00:00 into 12 hr format in the yaml file? Many thanks
When you save a time, it's always stored without any culture information. (And as soon as you're going to create a multilingual website, you'll be glad of that!).
That said, it's best to use the DateHelper when formatting dates in the view. This has a format_date function. When you're using this function, don't forget to set the default_culture in the settings.yml (or set the correct culture in the sfUser).
For example format_date($date, 't'); prints the current time, in the users culture.
For an overview of which patterns you can use, see this page.

Resources