0

I have documents in my index with a duration field, represented in a period/duration format, e.g. P1DT3H4S.

Ideally I'd have Elasticsearch parse that into milliseconds in a multi-field. Apprently Elasticsearch cannot do that.

The next best thing is to convert the period into milliseconds myself and store it in a multi-field. Is this possible?

What I would like to end up with:

duration with a value like PT4M20S.

duration.millis with a corresponding value of 260000

2
  • it's possible to have ES compute it for you, but the result would not be stored in a multi-field, just in two separate fields. Would that be ok for you? Commented Jun 22, 2021 at 12:34
  • I could do that in code and store in a separate field. But how do you make Elasticsearch compute it? Commented Jun 22, 2021 at 12:41

1 Answer 1

1

You can save yourself from that logic and have ES do it for you using an ingest pipeline with a script processor.

First you need to create an ingest pipeline that will take the duration string and parse it into milliseconds:

PUT _ingest/pipeline/duration2ms
{
  "processors": [
    {
      "script": {
        "source": """
            ctx.durationInMillis = Duration.parse(ctx.duration).getSeconds() * 1000;
          """
      }
    }
  ]
}

Then when you index your documents you can reference that pipeline, like this:

PUT index/_doc/1234?pipeline=duration2ms
{
  "duration": "P1DT3H4S"
}

And the document that will be indexed will look like this:

{
  "duration" : "P1DT3H4S",
  "durationInMillis" : 97204000
}
Sign up to request clarification or add additional context in comments.

8 Comments

Could I in this case have an alias named 'duration.millis' that refers to durationInMillis?
What difference does it make? You can name that field however you like
Since duration is already a keyword, it cannot be an object. So I cannot simply store a document that has both a duration (keyword) field and a duration.millis (long) field
Yes, and that's why you need two different fields, multi-fields won't work here. You could have an object field {"duration": {"keyword": "P1DT3H4S", "millis": 97204000}}
Technically this works just fine. The problem I have is that the duration field is public and has to have the value as the ISO duration. Also, the users can query the data using lucene syntax. I would really have liked to have the users be able to refer to the millisecond value as duration.millis.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.