4

I honestly don't know who named it painless, because it really isn't, and the documentation isn't accurate for sure.

First of all, the field I'm trying to compare to a date is mapped as date.

The script itself looks something like this:

Date max = new Date(params.updated_at);
for (...) {
    if (ctx._source.arr[i]['updated_at'].after(max)) {
        max = ctx._source.arr[i]['updated_at'];
    }
}

ctx._source.updated_at = max;

And the error I get is: "caused_by":{"type":"illegal_argument_exception","reason":"Unable to find dynamic method [after] with [1] arguments for class [java.lang.String].".

Now my question is: why on Earth would a field that is mapped as date would be string?

Reading the API (https://www.elastic.co/guide/en/elasticsearch/painless/master/painless-api-reference.html) clearly says there's a Date with an after method.

So... Am I doing something completely wrong here, or what's the case? Also, if it's how it is... do I really have to parse the string back to date every single time? And if so how?

Andrew
  • 2,063
  • 3
  • 24
  • 40
  • 1
    Your script reads from [_source](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-source-field.html) which is the original JSON document body that was passed at index time. The value is a string in the original document. – Chin Huang Jan 18 '18 at 18:36
  • @ChinHuang Alright, but... you can't really store Date as Date in JSON, can you? So then what else can I do instead of parsing the string? Can I access the actual indexed field some way? – Andrew Jan 18 '18 at 18:44
  • It seems you want to change the value of a date field during a `_reindex` operation. In that case, you [must parse the string](https://stackoverflow.com/questions/45864201/elasticsearch-painless-manipulate-date). – Chin Huang Jan 18 '18 at 18:53
  • Actually it's an `_update` operation, but I went with parsing anyways. I would still say that it's either a bad thing (the need of parsing) or the documentation should be much clearer on this. – Andrew Jan 18 '18 at 19:20

0 Answers0