title: How to perform a lucene query containing special character using QueryParser?
body: <p>Here is the thing. I have a term stored in the index, which contains special character, such as '-', the simplest code is like this:</p>
  
  <pre><code>Document doc = new Document();
  doc.add(new TextField("message", "1111-2222-3333", Field.Store.YES, Field.Index.NOT_ANALYZED));
  writer.addDocument(doc);
  </code></pre>
  
  <p>And then I create a query using QueryParser, like this:</p>
  
  <pre><code>String queryStr = "1111-2222-3333";
  QueryParser parser = new QueryParser(Version.LUCENE_36, "message", new StandardAnalyzer(Version.LUCENE_36));
  Query q = parser.parse(queryStr);
  </code></pre>
  
  <p>And then I use a searcher to search the query and get no result. I have also tried this:</p>
  
  <pre><code>Query q = parser.parse(QueryParser.escape(queryStr));
  </code></pre>
  
  <p>And still no result. </p>
  
  <p>Without using QueryParser and instead using TermQuery directly can do what I want, but this way is not flexible enough for user input texts.</p>
  
  <p>I think maybe the StandardAnalyzer did something to omit the special character in the query string. I tried debug, and I found that the string is splited and the actual query is like this:"message:1111 message:2222 message:3333". I don't know what exactly lucene has done...</p>
  
  <p>So if I want to perform the query with special character, what should I do? Should I rewrite an analyzer or inherit a queryparser from the default one? And how to?...</p>
  
  <p><strong>Update:</strong></p>
  
  <p>1 @The New Idiot @femtoRgon, I've tried QueryParser.escape(queryStr) as stated in the problem but it still doesn't work.</p>
  
  <p>2 I've tried another way to solve the problem. I derived a QueryTokenizer from Tokenizer and cut the word only by space, pack it into a QueryAnalyzer, which derives from Analyzer, and finally pass the QueryAnalyzer into QueryParser. </p>
  
  <p>Now it works. Originally it doesn't work because the default StandardAnalyzer cut the queryStr according to default rules(which recognize some of the special characters as splitters), when the query is passed into QueryParser, the special characters are already deleted by StandardAnalyzer. Now I use my own way to cut the queryStr and it only recognize space as splitter, so the special characters remain into the query waiting for processing and this works.</p>
  
  <p>3 @The New Idiot @femtoRgon, thank you for answering my question.</p>