简体   繁体   中英

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

ie 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM