2014-01-28 34 views
0

我想更新索引使用更新腳本,如果索引對象值有雙引號我得到異常。雖然更新索引對象值有雙引號拋出異常

使用下面的代碼:

Employee employee = Employee(); 
employee.setId(16661L); 
employee.setEmployeeId(11026L); 
employee.setEmployeeName("Ashok"s Kumar"); 
employee.setEmailId("[email protected]"); 

final StringBuilder updateScript = new StringBuilder("ctx._source.employees.add(" 
       + employee + ");"); 

final UpdateRequestBuilder request = CLIENT.prepareUpdate(indexName, String.valueOf("88"), "14344"); 
final UpdateResponse response = request.setScript(updateScript.toString()).execute().actionGet(); 

而執行此得到以下異常:

Exception in thread "main" org.elasticsearch.ElasticSearchIllegalArgumentException: failed to execute script 
    at org.elasticsearch.action.update.UpdateHelper.prepare(UpdateHelper.java:124) 
    at org.elasticsearch.action.update.UpdateHelper.prepare(UpdateHelper.java:60) 
    at org.elasticsearch.action.update.TransportUpdateAction.shardOperation(TransportUpdateAction.java:187) 
    at org.elasticsearch.action.update.TransportUpdateAction.shardOperation(TransportUpdateAction.java:183) 
    at org.elasticsearch.action.update.TransportUpdateAction.shardOperation(TransportUpdateAction.java:63) 
    at org.elasticsearch.action.support.single.instance.TransportInstanceSingleOperationAction$AsyncSingleAction$1.run(TransportInstanceSingleOperationAction.java:191) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) 
    at java.lang.Thread.run(Thread.java:662) 
Caused by: [Error: unterminated string literal] 
[Near : {... umar","emailId":"[email protected]"}); ....}] 
                         ^
[Line: 1, Column: 250] 
    at org.elasticsearch.common.mvel2.util.ParseTools.balancedCapture(ParseTools.java:1395) 

任何一個有此解決方案嗎?

回答

0

請更換

employeeName = employeeName.replace("\"", "\\\""); 
+0

我已經用這個代替它不會工作嘗試嘗試。在我的代碼Employee對象employee name字段中有雙引號。如果我們替換也更新腳本將在此格式 ctx._source.projectPeople.add({ 「ID」:16661, 「僱員」:11026, 「employeeName」: 「阿肖克\\」 S庫馬爾」, 「emailId」:「[email protected]」 }); so employeeName獲得「Ashok \\」只有這樣才能得到例外。 – Mathi