Performing Lucene Search query "Contains"
I am doing search for documents that contains the text entered by user
It works fine if the searchText don't have any special characters in it.
Below is how i create my QueryParser. :
//analyzer is an StandardAnalyzer()
QueryParser parser = new QueryParser("Text", analyzer);
parser.SetAllowLeadingWildcard(true);
return parser.Parse(string.Format("*{0}*", searchText));
I get the following error if the search text contains any special character in it :
suppose say search text is "bed ["
Cannot parse '*bed [*': Encountered "<EOF>" at line 1, column 7.
How can i make my query parser not fail if there are any special characters in search text and also i 开发者_开发问答don't want to ignore the special characters.
Try using:
QueryParser parser = new QueryParser("Text", analyzer);
parser.SetAllowLeadingWildcard(true);
var escapedSearchText = QueryParser.Escape(searchText);
return parser.Parse(string.Format("*{0}*", escapedSearchText));
i.e. escape the search text before building your query.
Hope this helps,
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
精彩评论