加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 16:54:16 所属栏目:PHP教程 来源:网络整理
导读:我想更新我的Elasticsearch索引文档的字段.就我而言,它是标签字段. 这是我目前的代码: // Index tags in the page document $es_client-update([ 'index' = 'myappname','type' = 'page','id' = $page_id,'body' = [ 'doc' = [ 'tags' = $tagsArray ] ] ]);
我想更新我的Elasticsearch索引文档的字段.就我而言,它是标签字段.
这是我目前的代码:

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

因此,这将通过向其添加tags数组来更新我的文档,但它不会删除旧标记.
如何在添加新标签时确保删除旧标签?

我确实查看了文档,但是众所周知,Elasticsearch文档可能非常混乱且无处不在.因此,经过几天的搜索,我在这里问.

任何帮助或建议将不胜感激.

解决方法

标准更新行为是合并数组/对象字段,如 update API documentation中所述.

…objects are merged together,existing scalar fields are overwritten
and new fields are added.

因此,您将使用脚本直接修改文档源.你可以使它具有通用性,从而可以缓存,并传递params以获得更好的性能.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读