1

How to delete and add item in nested property in ElasticSearch? most of the examples not working.

1
  • 1
    How are they not working? What have you tried? Please consider expanding your question with some examples and add clarity. Commented Jun 5, 2020 at 9:16

2 Answers 2

1

I've spent a lot of hours on figuring out how to add new object into nested array in ElasticSearch and how to delete it. There are a llot of answers on StackOverflow but most of them are outdated. So there are examples:

    $this->getClient()->update(
        [
            'index' => $this->getIndexName(),
            'id' => $Item->getReportId(),
            'body' => [
                'script' => [
                    'lang' => 'painless',
                    'inline' => 'int idx = -1;
                     for (int i = 0; i < ctx._source.reports.length; ++i) {
                      if (ctx._source.reports[i].entry_id == params.entry_id && ctx._source.reports[i].source == params.source) { 
                       idx=i; 
                      } 
                     } 
                     if (idx != -1)
                      ctx._source.reports.remove(idx);',
                    'params' => ['entry_id' => (string)$Item->getEntryId(), 'source' => $Item->getTableName()],
                ],
            ],
        ]
    );

    $this->getClient()->update(
        [
            'index' => $this->getIndexName(),
            'id' => $Item->getReportId(),
            'body' => [
                'script' => [
                    'lang' => 'painless',
                    'inline' => 'ctx._source.reports.add(params.object)',
                    'params' => ['object' => $entry]
                ],
            ],
        ]
    );
Sign up to request clarification or add additional context in comments.

1 Comment

(string) conversion here due to == cares about types
1

A simpler way of removing items (inspired from my other answer here).

No for loops, no ifs :-)

$this->getClient()->update(
    [
        'index' => $this->getIndexName(),
        'id' => $Item->getReportId(),
        'body' => [
            'script' => [
                'lang' => 'painless',
                'inline' => 'ctx._source.reports.removeAll{ it -> it.entry_id == params.entry_id && it.source == params.source }',
                'params' => ['entry_id' => (string)$Item->getEntryId(), 'source' => $Item->getTableName()],
            ],
        ],
    ]
);

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.