2015-09-27 190 views
0

我想更新特定字段中的某些值,我希望它是唯一的。無法執行腳本Elasticsearch

但是,當我給腳本條件.unique()它返回一個錯誤。

如果我沒有給出獨特的條件,那麼它會不斷更新。 意味着價值將

update_field =陣列( '世界', '值', '霜', '世界', '值', '霜',...)

我在做什麼這裏錯了有人知道!

 $script['script'] = 'ctx._source.update_field = (ctx._source.update_field + new_value).unique()'; 
     // unique() is giving the error but why 

錯誤消息---

{ 「錯誤」:「ElasticsearchIllegalArgumentException [未能執行 腳本];嵌套: GroovyScriptExecutionException [MissingMethodException [ 方法的無簽名:java.lang中。 String.unique()適用於參數類型:() values:[] \ n可能的解決方案:minus(java.lang.Object), minus(java.util.regex.Pattern),minus(java.lang.Object ),size(), size(),use([Ljava.lang.Object;)]];「,」status「: 400}

樣本文件---

hits: { 
     total: 19, 
     max_score: 5.5197725, 
     hits: [ 
      { 
       _index: "myIndex", 
       _type: "myType", 
       _id: "pdd9da099f380b3951a627a5d5a38758680016f16", 
       _score: 5.5197725, 
       _source: { 
        content: "After shooting attacks are several police cars sent to the streets to watch. 
        title: "The police in Copenhagen when bødemål despite extra tasks", 
        update_field: "[funny]" 

      }},} 

所以你可以看到,update_field不是空的,並有一定的價值 - 時,當我嘗試---

$script['script'] = 'ctx._source.update_field = ((ctx._source.update_field ?: []) - new_value).unique()'; 

它也會返回相同的錯誤! 不知道爲什麼!

回答

1

從錯誤中,關鍵點是No signature of method: java.lang.String.unique()。這可能是因爲update_field不存在或在某個點是空的,因此+運算符將簡單地嘗試將它連接爲您指定的數組的字符串。

解決此問題的一種方法是確保update_field被視爲一個數組,無論如何。你應該可以通過用(ctx._source.update_field ?: [])代替ctx._source.update_field來實現這一點,那樣如果update_field不存在或爲空,那麼將使用空數組而不是在字符串字段上使用數組。

$script['script'] = 'ctx._source.update_field = ((ctx._source.update_field ?: []) + new_value).unique()'; 
+0

感謝您的答案和解釋,但它也返回我同樣的錯誤,我也有一個樣本文檔更新我的問題,能不能請看看,非常感謝 –

+1

你可能需要擦拭你的'myIndex'索引並重新創建它,因爲你的'update_field'已經被索引爲一個字符串,例如''[funny]「'。 – Val

+0

@Oops我的不好,非常感謝Val,非常感謝您的幫助:),你真棒 –