How to update / replace a field in an ElasticSearch document using PHP?

I want to update the field of an indexed Elasticsearch document. In my case, this is a tag field. This is the code I have:

    // Index tags in the page document
    $es_client->update([
        'index'       => 'myappname',
        'type'        => 'page',
        'id'          => $page_id,
        'body'         => [
            'doc' => [
                'tags' => $tagsArray
            ]
        ]
    ]);

So, this will update my document by adding an array of tags to it, but it will not delete the old tags. How can I make sure that old tags are deleted when new tags are added?

I looked through the documentation, but as we all know, Elasticsearch documents can be very confusing and overwhelming. So I ask here after a few days of searching.

Any help or advice is appreciated.

+4
source share
1 answer

/, API.

... , .

script . , , , params .

// Index tags in the page document
$es_client->update([
   'index'       => 'myappname',
   'type'        => 'page',
   'id'          => $page_id,
   'body'        => [
      'script'      => 'ctx._source.tags=tags',
      'params'      => ['tags' => $tagsArray]
   ]    
]);
+3

All Articles