Lucene queryparser with "/" in query criteria
Asked Answered
G

3

14

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 "/"?

Grieco answered 22/7, 2013 at 22:8 Comment(0)
A
21

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).

Alemanni answered 23/7, 2013 at 0:6 Comment(0)
C
3

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

Citriculture answered 13/3, 2015 at 0:1 Comment(0)
G
1

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*")
Greatgranduncle answered 28/8, 2017 at 13:10 Comment(1)
It worked for me. "xxxxtestxxxx" final Query query = queryParser.parse("\"*/test*\"");Popgun

© 2022 - 2024 — McMap. All rights reserved.