Elasticsearch 5.6.3 通過script添加、刪除數組元素

環境:Elasticsearch 5.6.3

需求描述:對ES文檔中一個數組字段tags添加或者刪除元素“3”,esIP代表Elasticsearch的IP,docid代表文檔ID


方法:

1、添加數組元素

curl -XPOST 'http://esIP:9200/in_news/document/docid/_update' -d '
{"script":{"inline":"ctx._source.tags.add(params.tag)","params":{"tag":"3"}}}'



2、刪除數組元素
curl -XPOST 'http://esIP:9200/in_news/document/docid/_update' -d '
{"script":{"inline":"ctx._source.tags.remove(ctx._source.tags.indexOf(params.tag))","params":{"tag":"3"}}}

3、通過查詢更新數組

curl -XPOST 'http://esIP:9200/in_news/document/_update_by_query' -d '
{"query":{"term":{"tags":"1"}},"script":{"inline":"ctx._source.tags.remove(ctx._source.tags.indexOf(params.tag))","params":{"tag":"1"}}}'

其中,add,remove都是Java中的java.util.ArrayList類的內置函數。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章