Elasticsearch 更新插入并附加到数组 [英] Elasticsearch upserting and appending to array

查看:47
本文介绍了Elasticsearch 更新插入并附加到数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个脚本,该脚本会将新用户记录更新到 ElasticSearch,如果用户已存在则更新任何信息,如果更新对象中存在新的 PaymentInfo 对象,则将其附加到用户的 Payments 数组.这是我目前使用的简化版本:

I'm trying to write a script that will upsert a new user record to ElasticSearch, updating any information if the user already exists, and appending a new PaymentInfo object to the user's Payments array if it exists in the update object. Here's a simplified version of what I'm working with so far:

curl -XPOST 'http://localhost:9200/usrtest/usr/1/_update' -d '
{
    "doc_as_upsert": true, 
    "doc": {
        "customerId": "1",
        "firstName": "Mark",
        "lastName": "Z",
        "emailAddress": "foo.bar@gmail.com",
        "paymentInfo": {
            "pid": "1",
            "amt": "10"
        }
    }
}'

这几乎可以满足我的要求,因为它可以正确插入文档,或者如果用户存在具有相同 ID 的用户会更新文档,但是如果用户已经存在,则缺少将此 paymentInfo 添加到用户的 paymentInfos 数组的方面.就像现在一样,它只是覆盖了 paymentInfo 对象.我已经尝试将此脚本添加到更新 JSON:

This almost does what I want in that it inserts the doc properly, or updates the doc if a user exists with the same ID, but it's missing the aspect to add this paymentInfo to the user's paymentInfos array if the user exists already. As it is right now, it just overrides the paymentInfo object. I've tried adding this script to the update JSON:

"script": "if (ctx._source.containsKey("paymentInfos")) {ctx._source.paymentInfos += paymentInfo;} else {ctx._source.paymentInfos = paymentInfo}"

但是当指定 script 元素时,elasticsearch 会忽略 doc 元素.

but elasticsearch ignores doc elements when the script element is specified.

我觉得我在这里遗漏了一些愚蠢的东西,但我不确定.这里有人能帮我吗?

I feel like I'm missing something silly here, but I'm not sure. Can anyone here help me out?

我也尝试了以下方法:

curl -XPOST 'http://localhost:9200/usrtest/usr/1/_update' -d '
{    
    "script": "if (ctx._source.containsKey("paymentInfos")) {ctx._source.paymentInfos += paymentInfo;} else {ctx._source.paymentInfos = paymentInfo}",
    "upsert": {
        "customerId": "1",
        "firstName": "Mark",
        "lastName": "Z",
        "emailAddress": "foo.bar@gmail.com",
        "paymentInfo": {
            "pid": "1",
            "amt": "10"
         }
    },
    "params": {
         "paymentInfo": {
            "pid": "1",
                "amt": "10"
         }
    }
}'

这也几乎做我想做的事,因为它在我多次运行脚本时附加 paymentInfo 对象,但否则它不会更新文档本身(即,如果我运行再次编写脚本,将 Mark 更改为 Mindy,它不会更新,因为 upsert 元素仅在文档不存在时使用).

Which also almost does what I want it to, in that it appends the paymentInfo objects when I run the script several times, but otherwise it doesn't update the document itself (i.e. if I run the script again, changing Mark to Mindy, it doesn't update since upsert elements are only used if the doc doesn't exist already).

推荐答案

您需要在脚本的插入部分添加一些数组括号.

you'll want to add some array brackets to the insert part of the script.

"script": "if (ctx._source.containsKey("paymentInfos")) {ctx._source.paymentInfos += paymentInfo;} else {ctx._source.paymentInfos = [paymentInfo]}"

第一部分中的'paymentInfos'属性被定义为一个对象,所以这也可能导致你摔倒.

the 'paymentInfos' property in the first section is defined as an object, so that may also be causing you to fall down.

这篇关于Elasticsearch 更新插入并附加到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆