簡體   English   中英

如何使用 PHP 更新/替換 ElasticSearch 文檔中的字段?

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

我想更新我的 Elasticsearch 索引文檔的字段。 就我而言,它是標簽字段。 這是我目前擁有的代碼:

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

因此,這將通過向其添加標簽數組來更新我的文檔,但不會刪除舊標簽。 如何確保在添加新標簽時刪除舊標簽?

我確實查看了文檔,但眾所周知,Elasticsearch 文檔可能非常混亂且無處不在。 因此,經過幾天的搜索,我在這里問。

任何幫助或建議將不勝感激。

標准更新行為是合並數組/對象字段,如更新 API 文檔中所述

...對象合並在一起,現有標量字段被覆蓋並添加新字段。

因此,您可以使用腳本直接修改文檔源。 您可以使其通用並因此可緩存,並傳入params以獲得更好的性能。 PHP API 文檔

// 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]
   ]    
]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM