而更新索引对象值有双引号抛出异常

时间:2014-01-28 10:23:28

标签: java elasticsearch

我正在尝试使用更新脚本更新索引,如果索引对象值有双引号,我将获得异常。

使用以下代码:

Employee employee = Employee();
employee.setId(16661L);
employee.setEmployeeId(11026L);
employee.setEmployeeName("Ashok"s Kumar");
employee.setEmailId("ashokkumar@yahoo.com");

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":"ashokkumar@yahoo.com"}); ....}]
                                                                                           ^
[Line: 1, Column: 250]
    at org.elasticsearch.common.mvel2.util.ParseTools.balancedCapture(ParseTools.java:1395)

任何人都有解决方案吗?

1 个答案:

答案 0 :(得分:0)

请尝试替换

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