[英]Update nested field in an index of ElasticSearch with Java API
I am using Java API for CRUD operation on elasticsearch. 我正在使用Java API对Elasticsearch进行CRUD操作。
I have an typewith a nested field and I want to update this field. 我有一个带有嵌套字段的类型,我想更新此字段。
Here is my mapping for the type: 这是我对类型的映射:
"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"
}
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). 例如,我在字符串中具有先前的JSON对象(我们将此字符串称为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();
I have got a parsing error from elasticsearch. 我有来自Elasticsearch的解析错误。 Anyone knows what I am doing wrong ? 有人知道我在做什么错吗?
You don't need the script, just update it. 您不需要脚本,只需对其进行更新。
UpdateRequestBuilder br = client.prepareUpdate("index", "enduser", "1");
br.setDoc("{\"location\":{ \"name\": \"london\", \"point\": \"44.5,5.2\" }}".getBytes());
br.execute();
I tried to recreate your situation and i solved it by using an other way the .setScript method. 我试图重新创建您的情况,并通过其他方法使用.setScript方法来解决。
Your updating request now would looks like : 您的更新请求现在看起来像:
client.prepareUpdate(index, ElasticSearchConstants.TYPE_END_USER,id).setScript("ctx._source.location =" + json).execute().actionGet()
Hope it will help you. 希望对您有帮助。
I am not sure which ES version you were using, but the below solution worked perfectly for me on 2.2.0. 我不确定您使用的是哪个ES版本,但是下面的解决方案在2.2.0上对我来说效果理想。 I had to store information about named entities for news articles. 我必须为新闻文章存储有关命名实体的信息。 I guess if you wish to have multiple locations in your case, it would also suit you. 我想如果您希望在您的案件中有多个地点,那也很适合您。
This is the nested object I wanted to update: 这是我要更新的嵌套对象 :
"entities" : [
{
"disambiguated" : {
"entitySubTypes" : [],
"disambiguatedName" : "NameX"
},
"frequency" : 1,
"entityType" : "Organization",
"quotations" : ["...", "..."],
"name" : "entityX"
},
{
"disambiguated" : {
"entitySubType" : ["a", "b" ],
"disambiguatedName" : "NameQ"
},
"frequency" : 5,
"entityType" : "secondTypeTest",
"quotations" : [ "...", "..."],
"name" : "entityY"
}
],
and this is the code : 这是代码 :
UpdateRequest updateRequest = new UpdateRequest();
updateRequest.index(indexName);
updateRequest.type(mappingName);
updateRequest.id(url); // docID is a url
XContentBuilder jb = XContentFactory.jsonBuilder();
jb.startObject(); // article
jb.startArray("entities"); // multiple entities
for ( /*each namedEntity*/) {
jb.startObject() // entity
.field("name", name)
.field("frequency",n)
.field("entityType", entityType)
.startObject("disambiguated") // disambiguation
.field("disambiguatedName", disambiguatedNameStr)
.field("entitySubTypes", entitySubTypeArray) // multi value field
.endObject() // disambiguation
.field("quotations", quotationsArray) // multi value field
.endObject(); // entity
}
jb.endArray(); // array of nested objects
b.endObject(); // article
updateRequest.doc(jb);
Blblblblblblbl's answer couldn't work for me atm, because scripts are not enabled in our server. Blblblblblblbl的答案对我的atm无效,因为在我们的服务器中未启用脚本。 I didn't try Bask's answer yet - Alcanzar's gave me a hard time, because I supposedly couldn't formulate the json string correctly that setDoc receives. 我还没有尝试过Bask的答案-Alcanzar给了我很大的麻烦,因为据称我无法正确设置setDoc接收到的json字符串。 I was constantly getting errors that either I am using objects instead of fields or vice versa. 我经常收到错误消息,要么我使用对象而不是字段,反之亦然。 I also tried wrapping the json string with doc{} as indicated here , but I didn't manage to make it work. 我还尝试按照此处所示用doc {}包装json字符串,但是我没有设法使其工作。 As you mentioned it is difficult to understand how to formulate a curl statement at ES's java API. 正如您提到的,很难理解如何在ES的Java API中制定curl语句。
A simple way to update the arraylist and object value using Java API. 一种使用Java API更新arraylist和对象值的简单方法。
UpdateResponse update = client.prepareUpdate("indexname","type",""+id)
.addScriptParam("param1", arrayvalue)
.addScriptParam("param2", objectvalue)
.setScript("ctx._source.field1=param1;ctx._source.field2=param2").execute()
.actionGet();
arrayvalue-[
{
"text": "stackoverflow",
"datetime": "2010-07-27T05:41:52.763Z",
"obj1": {
"id": 1,
"email": "sa@gmail.com",
"name": "bass"
},
"id": 1,
}
object value -
"obj1": {
"id": 1,
"email": "sa@gmail.com",
"name": "bass"
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.