siltalau
siltalau

Reputation: 549

Elasticsearch - provided multi-field value

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

Upvotes: 0

Views: 106

Answers (1)

Val
Val

Reputation: 217544

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
}

Upvotes: 1

Related Questions