Lucene queryparser with “/” in query criteria

时光总嘲笑我的痴心妄想 提交于 2019-12-08 15:27:49

问题


When I try to search for something such as "workaround/fix" within Lucene, it throws this error:

org.apache.lucene.queryparser.classic.ParseException: Cannot parse 'workaround/fix': Lexical error at line 1, column 15.  Encountered: <EOF> after : "/fix"
    at org.apache.lucene.queryparser.classic.QueryParserBase.parse(QueryParserBase.java:131)
    at pi.lucengine.LucIndex.main(LucIndex.java:112)
Caused by: org.apache.lucene.queryparser.classic.TokenMgrError: Lexical error at line 1, column 15.  Encountered: <EOF> after : "/fix"
    at org.apache.lucene.queryparser.classic.QueryParserTokenManager.getNextToken(QueryParserTokenManager.java:1133)
    at org.apache.lucene.queryparser.classic.QueryParser.jj_scan_token(QueryParser.java:599)
    at org.apache.lucene.queryparser.classic.QueryParser.jj_3R_2(QueryParser.java:482)
    at org.apache.lucene.queryparser.classic.QueryParser.jj_3_1(QueryParser.java:489)
    at org.apache.lucene.queryparser.classic.QueryParser.jj_2_1(QueryParser.java:475)
    at org.apache.lucene.queryparser.classic.QueryParser.Clause(QueryParser.java:226)
    at org.apache.lucene.queryparser.classic.QueryParser.Query(QueryParser.java:181)
    at org.apache.lucene.queryparser.classic.QueryParser.TopLevelQuery(QueryParser.java:170)
    at org.apache.lucene.queryparser.classic.QueryParserBase.parse(QueryParserBase.java:121)

This are my lines 111 and 112:

QueryParser parser = new QueryParser(Version.LUCENE_43, field, analyzer);
Query query = parser.parse(newLine);

What do I need to do to allow it to parse the "/"?


回答1:


The query parser interprets slashes as the beginning/end or a regex query (as of 4.0, see documentation here).

So, to incorporate slashes into the query, you will need to escape them by adding a backslash (\) before them.

You can handle escaping with QueryParser.escape(String).




回答2:


I encountered a similar problem when using '/' in lucene queries issued from the elastic search kibana dashboard. I was escaping the '/' characters as indicated in the documentation and still not getting any success. I think this is related to the template bug reported here : https://github.com/elastic/kibana/issues/789. Not sure yet, will update when we update the logstash components




回答3:


I had a case where when using forward slash with wildcard it just wouldn't return any result, even if escaped it:

+(*16/17*)
+(*16\/17*)

The solution was to add double quote:

+("*16/17*")
+("*16\/17*")


来源:https://stackoverflow.com/questions/17798300/lucene-queryparser-with-in-query-criteria

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!