Search based on runtime boolean calculated from now

I need to be able to search on a field that is not part of the stored document but is derived from a field on that document.

The document field is a timestamp called expiry_utc_timestamp. I need to be able to filter on a field called isexpired that is not part of the document. It is a boolean that will be true or false depending on the time of the query (i.e. now) and the static expiry_utc_timestamp value.

I tried creating a runtime field as follows:

POST messages/_mapping
{
  "runtime": {
    "isexpired": {
        "type": "boolean",
        "script": {
          "source": "emit(doc['expiry_utc_timestamp'].value <= now)"
        }
      }
  }
}

But this is not a valid request. It appears that the painless scripting does not provide the use of now.

Please can someone advise on how I can achieve searching on a runtime field that is not part of the actual document where this field is calculated taking into account the current date & time.

I cannot filter on "expiry_utc_timestamp > now" without making large architectural changes to my application code. I need to be able to filter on "isexpired:true" or "isexpired:false".

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.