弹性搜索插入并附加到数组 [英] Elasticsearch upserting and appending to array

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

问题描述

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

  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
}
}
}'
pre>

这几乎可以正确插入文档,或者如果用户存在相同的ID,则更新文档,但是缺少添加的方面如果用户已经存在,该paymentInfo将发送给用户的paymentInfos数组。现在,它只是覆盖了paymentInfo对象。我尝试将此脚本添加到更新JSON中:

 script:if(ctx._source.containsKey 付款信息)){ 
$ b

,但指定脚本元素时,弹性搜索会忽略 doc 元素。



我觉得我在这里缺少一些愚蠢的东西,但我不确定。任何人都可以帮助我吗?



编辑:
我也尝试了以下内容:

  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},
up:{
customerId:1,
firstName:Mark,
lastName:Z,
emailAddress:foo .bb @ gmail.com,
paymentInfo:{
pid:1,
amt:10
}
} ,
params:{
paymentInfo:{
pid:1,
amt:10
}
}
}'

哪些也几乎想要它,因为它在我运行脚本几次时追加了paymentInfo对象,但否则我会t不更新文档本身(即如果我再次运行脚本,将Mark更改为Mindy,它不会更新,因为 c>元素仅在文档不存在的情况下被使用。

解决方案

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

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

第一节中的'paymentInfos'属性定义为对象,这样也可能导致你掉下来。


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"
        }
    }
}'

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}"

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?

Edit: I've tried the following as well:

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"
         }
    }
}'

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]}"

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

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

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