Search code examples
lucenespecial-characters

Lucene 5.0.0 - search string with special characters


I am using Lucene version 5.0.0. In my search string, there is a minus character like “test-”. I read that the minus sign is a special character in Lucene. So I have to escape that sign, as in the queryparser documentation:

Escaping Special Characters: Lucene supports escaping special characters that are part of the query syntax. The current list special characters are:

- + - && || ! ( ) { } [ ] ^ " ~ * ? : \ /`

To escape these character use the \ before the character. For example to search for (1+1):2 use the query:

\(1\+1\)\:2

To do that I use the QueryParser.escape method:

query = parser.parse(QueryParser.escape(searchString));

I use the classic Analyzer because I noticed that the standard Analyzer has some problems with escaping special characters.

The problem is that the Parser deletes the special characters and so the Query has the term content:test

How can I set up the parser and searcher to search for the real value “test-“? I also created my own query with the content test- but that also didn’t work. I recieved 0 results but my index has entries like:

  • Test-VRF
  • Test-IPLS

I am really confused about this problem.


Solution

  • The goal to fix this issue is to store the value content in an NOT_ANALYZED way. Field fieldType = new Field(key.toLowerCase(),value, Field.Store.YES, Field.Index.NOT_ANALYZED);

    Someone who has the same problem has to take care how to store the contents in the index. To request the result create a query in this way searchString = QueryParser.escape(searchString); and use for example a WhitespaceAnalyzer.