使用 Java API 更新 ElasticSearch 索引中的嵌套字段 [英] Update nested field in an index of ElasticSearch with Java API

查看:78
本文介绍了使用 Java API 更新 ElasticSearch 索引中的嵌套字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 elasticsearch 上使用 Java API 进行 CRUD 操作.

I am using Java API for CRUD operation on elasticsearch.

我有一个带有嵌套字段的类型,我想更新这个字段.

I have an typewith a nested field and I want to update this field.

这是我的类型映射:

"enduser": {
            "properties": {
                "location": {
                    "type": "nested",
                    "properties":{
                        "point":{"type":"geo_point"}
                    }
                }
            }
        }

当然,我的最终用户类型会有其他参数.

Of course my enduser type will have other parameters.

现在我想在我的嵌套字段中添加这个文档:

Now I want to add this document in my nested field:

"location":{
          "name": "London",
           "point": "44.5, 5.2"
}

我正在搜索有关如何更新嵌套文档的文档,但找不到任何内容.例如,我在一个字符串中有前一个 JSON 对象(我们称这个字符串为 json).我尝试了以下代码,但似乎不起作用:

I was searching in documentation on how to update nested document but I couldn't find anything. For example I have in a string the previous JSON obect (let's call this string json). I tried the following code but seems to not working:

params.put("location", json);
client.prepareUpdate(index, ElasticSearchConstants.TYPE_END_USER,id).setScript("ctx._source.location = location").setScriptParams(params).execute().actionGet();

我收到了来自 elasticsearch 的解析错误.有人知道我做错了什么吗?

I have got a parsing error from elasticsearch. Anyone knows what I am doing wrong ?

推荐答案

我试图重现你的情况,我通过使用 .setScript 方法的另一种方式解决了它.

I tried to recreate your situation and i solved it by using an other way the .setScript method.

您现在的更新请求如下:

Your updating request now would looks like :

client.prepareUpdate(index, ElasticSearchConstants.TYPE_END_USER,id).setScript("ctx._source.location =" + json).execute().actionGet()

希望能帮到你.

这篇关于使用 Java API 更新 ElasticSearch 索引中的嵌套字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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