Search code examples
dateelasticsearchelasticsearch-painless

ElasticSearch Script get value of current year


I'm trying to make a script within my rescore function to check if the bibliographic release date (doc['bibliografische_verschijningsdatum']), is the same as the commercial release date (doc['verschijningsdatum']).

AND the doc['verschijningsjaar'].value == new Date.getYear()

How can I get the year of todays date in painless?

"script_score": {
          "script": "((doc['verschijningsdatum'] == doc['bibliografische_verschijningsdatum']) && (doc['verschijningsjaar'].value == new Date().getYear()) ? 1 : 0)"
        }
      }

Solution

  • Your question has already been asked on the elastic forum. The official answer, at least from a member of the elastic team, is the following, (emphasis is mine):

    Painless intentionally doesn't have any access to now style variables so they can be better cached. The current recommendation is for you to send now style variables in over scripts params.

    BTW, you should know that this query isn't able to take advantage of the index at all. It'll have to load the @timestamp field for every document and compare it. This much much slower than extracting the day of the week on ingest and adding it to the document.