2014-04-29 146 views
3

我正在使用Java API進行elasticsearch上的CRUD操作。使用Java API更新ElasticSearch索引中的嵌套字段

我有一個類型嵌套的字段,我想更新此字段。

這裏是我的類型映射:

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

當然我的最終用戶類型將有其他的參數。

現在我想在本文中我嵌套字段添加:

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

我如何更新嵌套的文檔中搜索在文檔,但我找不到任何東西。例如,我有一個字符串中的前一個JSON obect(讓我們調用這個字符串json)。我試着下面的代碼,但似乎不工作:

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

我已得到elasticsearch解析錯誤。任何人都知道我做錯了什麼?

回答

3

我試圖重新創建您的情況,我通過使用其他方式.setScript方法來解決它。

你的更新請求現在會是這樣的:

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

希望它會幫助你。

4

你不需要腳本,只需更新它。

UpdateRequestBuilder br = client.prepareUpdate("index", "enduser", "1"); 
    br.setDoc("{\"location\":{ \"name\": \"london\", \"point\": \"44.5,5.2\" }}".getBytes()); 
    br.execute(); 
+0

嘿。事實上,這個解決方案是可能的,但是我收到的文檔總是不同的,所以我不能硬編碼它,並且我用setDoc嘗試了很多方法,但沒有一個能正常工作。 最後,上面的腳本用法是我的問題 – razafinr

+1

根據你的例子的解決方案,但你有json - 它只是json.toString()。getBytes()而不是硬編碼的值...這可以避免調用一個腳本,使其整體更快。 – Alcanzar

1

一種使用Java API更新數組列表和對象值的簡單方法。

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": "[email protected]", 
     "name": "bass" 
     }, 
    "id": 1, 
} 

object value - 
"obj1": { 
    "id": 1, 
    "email": "[email protected]", 
    "name": "bass" 
} 
2

我不確定你使用的是哪個ES版本,但是下面的解決方案在2.2.0上完全適合我。我不得不存儲關於新聞文章的命名實體的信息。我想如果你想在你的案例中有多個地點,它也適合你。

這是嵌套對象我想更新:

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

,這是代碼

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的答案可能不適合我大氣壓,因爲腳本在我們的服務器中未啓用。我沒有嘗試Bask的答案 - Alcanzar給了我一個很難的時間,因爲我認爲無法正確表達setDoc接收的json字符串。我一直在收到錯誤,無論是使用對象而不是字段,反之亦然。我也嘗試用doc {}包裝json字符串,如here所示,但我沒有設法使其工作。正如你所提到的,很難理解如何在ES的Java API中制定一個curl語句。

相關問題