簡體   English   中英

如何在彈性搜索中僅批量更新某些值?

[英]How to update some values only in bulk in elasticsearch?

在彈性搜索中,我有類似post_en的數據作為索引,並保存了多個帖子,其中包含id, title, description, price, status等字段。

現在我如何使用批量更新每個帖子的特定字段。 (通過單個帖子,我可以輕松更新字段。)

這是批量請求的 json 示例。

{"_index":"post_en","_id":"966156"}
{"id":966156,"status":2}
{"_index":"post_en","_id":"966157"}
{"id":966157,"title":"some title","status":1}

使用這個 json 更新給定的字段,但它也刪除了其他現有的字段,這意味着整個對象被給定的更新對象替換。 但我只想更新給定的字段並保持其余字段不變。

您可能需要更新而不是插入(doc_as_upsert 選項)

在此處引用文檔

https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html#bulk-update

更新操作負載支持以下選項:doc(部分文檔)、upsert、doc_as_upsert、script、params(用於腳本)、lang(用於腳本)和 _source。 有關選項的詳細信息,請參閱更新文檔。

{
    "properties": {
        "id": {
            "type": "long"
        },
        "status": {
            "type": "integer"
        }
    }
}

使用映射創建索引

索引 2 個文檔

{"id":966157,"status":2}
{"id":966157,"status":3}

執行了批量操作

{ "update" : {"_id" : "ISpCB4IBCOifrGItSDgU", "_index" : "post_en"} }
{ "doc" : {"status" : "4"} }
{ "update" : {"_id" : "ICpCB4IBCOifrGItKTgF", "_index" : "post_en"} }
{ "doc" : {"status" : "5"} }

搜索中

        "hits": [
            {
                "_index": "post_en",
                "_id": "ISpCB4IBCOifrGItSDgU",
                "_score": 1.0,
                "_source": {
                    "id": 966157,
                    "status": "4"
                }
            },
            {
                "_index": "post_en",
                "_id": "ICpCB4IBCOifrGItKTgF",
                "_score": 1.0,
                "_source": {
                    "id": 966156,
                    "status": "5"
                }
            }
        ]

暫無
暫無

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

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