ElasticSearch 脚本获取当年的值

ElasticSearch Script get value of current year

我正在尝试在我的 rescore 函数中制作一个脚本来检查书目发布日期 (doc['bibliografische_verschijningsdatum']) 是否与商业发布日期 (doc['verschijningsdatum']).

AND 文档['verschijningsjaar'].value == new Date.getYear()

如何轻松获得今天日期的年份?

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

您的问题已经有人问过 on the elastic forum。至少来自弹性团队成员的官方答案如下,(强调是我的):

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.