代码之家  ›  专栏  ›  技术社区  ›  loretoparisi

在ElasticSearch批量JavaScript API中使用脚本更新索引

  •  0
  • loretoparisi  · 技术社区  · 7 年前

    我在用ElasticSearch 采取行动 Upserts bulk 像这样的api

    {
                        index: myIndex,
                        type: '_doc',
                        body: [
                        { index:  {_id: docItemID1 } },
                        docItem1,
                        { index:  {_id: docItemID2 } },
                        docItem2
    ]
                    }
    

    这行得通。现在我想更新/附加一个新值到 tag scripted_upsert script 更新操作,例如:

    {
       "scripted_upsert":true,
        "script" : {
            "source": "if ( !ctx._source.tags.contains(params.tag) ) ctx._source.tag.concat( params.tag)",
            "lang": "painless",
            "params" : {
                "tag" : "blue"
            }
        },
        "upsert" : {
            "tag" : ["red","green"]
        }
    }
    

    脚本化的 从这两个世界中取其精华,所以我想象这样的事情——如果它是正确的(这是我的问题)

        {
            "script" : {
                "source": "if ( !ctx._source.tags.contains(params.tag) ) ctx._source.tag.concat( params.tag)",
                "lang": "painless",
                "params" : {
                    "tag" : myNewTag
                }
            },
            "upsert" : docItem
        }
    

    哪里 docItem 将包含 标签 要更新的项。此标记项是一个逗号分隔的标记列表,如 red,green .

    bulk api,即在使用 body 作为 [] 有更新的操作 脚本 加上 脚本化的 一个或多个项目的标志?

    0 回复  |  直到 7 年前
        1
  •  1
  •   villasv    7 年前

    没错,那个 docs say :

    scripted_upsert 真的吗

    因此,同时拥有“upsert”和“script”部分是一种方法,但是您必须保留 "scripted_upsert":true (最后一个片段中缺少)。

    await client.bulk({
      index: 'myIndex',
      type: 'myType',
      body [
        { update: { _id: docId } },
        { script: { source, params }, scripted_upsert: true, upsert: docItem },
      ],
    });