Here is the thing. I have a term stored in the index, which contains special character, such as '-', the simplest code is like this:
Document doc = new Document();
doc.add(new TextField("message", "1111-2222-3333", Field.Store.YES, Field.Index.NOT_ANALYZED));
writer.addDocument(doc);
And then I create a query using QueryParser, like this:
String queryStr = "1111-2222-3333";
QueryParser parser = new QueryParser(Version.LUCENE_36, "message", new StandardAnalyzer(Version.LUCENE_36));
Query q = parser.parse(queryStr);
And then I use a searcher to search the query and get no result. I have also tried this:
Query q = parser.parse(QueryParser.escape(queryStr));
And still no result.
Without using QueryParser and instead using TermQuery directly can do what I want, but this way is not flexible enough for user input texts.
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...
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?...
Update:
1 @The New Idiot @femtoRgon, I've tried QueryParser.escape(queryStr) as stated in the problem but it still doesn't work.
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.
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.
3 @The New Idiot @femtoRgon, thank you for answering my question.
TextField
coming from? Lucene'sTextField
does not take aField.Index
argument (Field.Index
is deprecated). To create a field like what you have here, you would instead use aStringField
. Is this some sort of customTextField
or something? – femtoRgondoc.add(new Field("message", "1111-2222-3333", Field.Store.YES, Field.Index.NOT_ANALYZED));
Lucene 4.x and 3.x APIs are very different, I'm still trying to understand lucene 4.x APIs. – Yuanchao TangI derived a QueryTokenizer from Tokenizer and cut the word only by space
– rrsk