2

I'm trying to add or update a nested object in Elasticsearch using a script. Below script works fine if integrations is already an array, but when it is null, below script throws a null pointer exception. How do I initialize ctx._source.integrations to be an empty array if its value is null? (Something like the equivalent of JavaScript's myObject.integrations = myObject.integrations ?? [])

POST /products/_update/VFfrnQrKlC5bwdfdeaQ7
{
  "script": {
    "source": """
      ctx._source.integrations.removeIf(i -> i.id == params.integration.id);
      ctx._source.integrations.add(params.integration);

      ctx._source.integrationCount = ctx._source.integrations.length;
    """,
    "params": {
      "integration": {
        "id": "dVTV8GjHj8pXFnlYUUlI",
        "from": true,
        "to": false,
        "vendor": "sfeZWDpZXlF5Qa8mUsiF",
        "targetProduct": {
          "id": "nyILhphvCrGYm53cfaOx",
          "name": "Test Product",
          "categoryIds": []
        }
      }
    }
  }
}

1 Answer 1

4

ok i think this does the trick:

if (ctx._source.integrations == null) {
  ctx._source.integrations = new ArrayList();
}

is there a short hand to this like in the JS example?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.