ES搜索特殊字符异常 :Encountered: <EOF> after

 

异常信息如下

org.elasticsearch.client.RestHighLevelClient.performRequest(RestHighLevelClient.java:1256)↵		... 48 more↵	Caused by: org.elasticsearch.client.ResponseException: method 

[POST], host [http://xxxxxxurl], URI [/xxxxxxxx/xxxxxxxx/_search?typed_keys=true&ignore_unavailable=false&expand_wildcards=open&allow_no_indices=true&sear
ch_type=query_then_fetch&batched_reduce_size=512], status line [HTTP/1.1 400 Bad 
Request]↵{"error":{"root_cause":[{"type":"token_mgr_error","reason":"token_mgr_error: 

Lexical error at line 1, column 5.  Encountered: <EOF> after : 
\"\""}],"type":"search_phase_execution_exception","reason":"all shards 
failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"xxxxxxxx","node":"uIjzNu0rRJew8dZu-9nWwA","reason":
{"type":"query_shard_exception","reason":"Failed to parse query [as./]","index_uuid":"uuid","index":"xxxxxxxx","caused_by":{"type":"parse_exception","reason":"parse_exception: Cannot parse 'as./': Lexical error at line 1, column 5.  Encountered: <EOF> after : \"\"","caused_by":{"type":"token_mgr_error","reason":"token_mgr_error: Lexical error at line 1, column 5.  Encountered: <EOF> after : \"\""}}}}]},"status":400}↵		at org.elasticsearch.client.RestClient$1.completed(RestClient.java:540)↵		at 
org.elasticsearch.client.RestClient$1.completed(RestClient.java:529)↵		at 
org.apache.http.concurrent.BasicFuture.completed(BasicFuture.java:122)↵		at 
org.apache.http.impl.nio.client.DefaultClientExchangeHandlerImpl.responseCompleted(Defaul
tClientExchangeHandlerImpl.java:181)↵		at 
org.apache.http.nio.protocol.HttpAsyncRequestExecutor.processResponse(HttpAsyncRequestExecutor.java:448)↵		at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.inputReady(HttpAsyncRequestExecutor.java:338)↵		at org.apache.http.impl.nio.DefaultNHttpClientConnection.consumeInput(DefaultNHttpClientConnection.java:265)↵		at org.apache.http.impl.nio.client.InternalIODispatch.onInputReady(InternalIODispatch.java:81)↵		at org.apache.http.impl.nio.client.InternalIODispatch.onInputReady(InternalIODispatch.java:39)↵		at org.apache.http.impl.nio.reactor.AbstractIODispatch.inputReady(AbstractIODispatch.java:114)↵		at org.apache.http.impl.nio.reactor.BaseIOReactor.readable(BaseIOReactor.java:162)↵		at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvent(AbstractIOReactor.java:337)↵		at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvents(AbstractIOReactor.java:315)↵		at org.apache.http.impl.nio.reactor.AbstractIOReactor.execute(AbstractIOReactor.java:276)↵		at org.apache.http.impl.nio.reactor.BaseIOReactor.execute(BaseIOReactor.java:104)↵		at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor$Worker.run(AbstractMultiworkerIOReactor.java:591)↵		... 1 more↵"

 

重点是这句

{"type":"token_mgr_error","reason":"token_mgr_error: Lexical error at line 1, column 5.  Encountered: <EOF> after : \"\""}

我们在查询的时候包含特殊字符:"\"

所以为了避免搜索出错,我们需要对查询的字符串先进行转义

转义可以参考如下方法

public static String escape(String s) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c == '\\' || c == '+' || c == '-' || c == '!' || c == '(' || c == ')' || c == ':'
                    || c == '^' || c == '[' || c == ']' || c == '\"' || c == '{' || c == '}' || c == '~'
                    || c == '*' || c == '?' || c == '|' || c == '&' || c == '/') {
                sb.append('\\');
            }
            sb.append(c);
        }
        return sb.toString();
    }

 

遇到了这个问题,在这里mark一下,希望帮助到更多研发同学。

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码者人生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值