Use runtime field in App Search

Hi,

I have a rather complex use case for App Search with engines based on Elasticsearch indexes. I managed to build ES indexes and AP engines that match almost all of my criteria but I stumble on one of the last things I'd like to setup.

I have products and these products can have promotions, only applicable between two dates. I'd like to add fields to my indexes to be able to easily know if a product is currently promoted and prioritize those who are.

As I don't want to rebuild products index every day, I imagined a solution with a promoted_on mapping field (of type date), to store dates when a product (document) is promoted and a promoted runtime field (of type long as boolean does not seem to be supported), that emits 0 or 1 depending if today appears in doc['promoted_on'].

My questions are:

What is the best match in App Search supported field types for a boolean?

I used byte for some other fields in my indexes and the closest field type supported on a runtime field seem to be long but is this the best way to do?

According to documentation, number fields are the only one that can be used in all of boosts, facets and filters App Search APIs but this post suggest to use keywords.

Is it possible to use a runtime field declared on an ES index in an App Search query?

I'd also like to have the runtime field value returned for every documents in an App Search response.

What should be the script of my promoted runtime field?

I tried this:

{
  "runtime": {
    "promoted": {
      "type": "long",
      "script": {
        "source": "emit(doc['promoted_on'].contains(LocalDate.now()) ? 1 : 0);"
      }
    }
  }
}

But I don't think it will work. It seems there is a lot of different ways (libraries) to manipulate dates (without time nor timezone) in Java but no simple one and different sources between painless documentation, this forum or SO do not even agree on of what type are exactly values in a script.

What should be the script of my promoted runtime field?

Due to method restrictions in Painless scripts, LocalDate.now() and alike are not available. The only way I could get current date and check if it is included in field values is to convert everything into strings, which sounds counter-intuitive.

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String today = formatter.format(new Date());
List dates = new ArrayList();
for (ZonedDateTime zdt : field('promoted_on')) dates.add(formatter.format(zdt));

emit(dates.contains(today) ? 1 : 0);

Is it possible to use a runtime field declared on an ES index in an App Search query?

The answer looks like it is not.

Runtime fields are not part of the _source document in Elasticsearch response. To get their value in the response, you have to use the fields parameter and that does not seem to be possible through App Search queries.

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