Wednesday, July 28, 2010

Wildcard Searches in Lucene.NET

Yes, you can do wild card searches with Lucene.Net. For example you can search for the term “Mc*” in a database with names it will then return names such as “McNamara” or “McLoud”. When you read more details about the query parser syntax (version 3.0.2) you will notice that the wildcard characters * (any number of characters) and ? (one character) are only allowed in the middle or at the end of the search term but not at the beginning.

But how about using wildcards at the beginning? Well you can but you should be aware of the consequences. You have to explicitly switch this on in your code as it comes with an additional performance hit with large indexes. So be careful and see whether the resulting performance is acceptable for your users.

And here is the code (C# in this case):

var index = FSDirectory.Open(new DirectoryInfo(IndexLocationPath));
var searcher = new IndexSearcher(index, true);
var queryParser = new QueryParser(LuceneVersion, "content", new StandardAnalyzer(LuceneVersion));
queryParser.SetAllowLeadingWildcard(true);
var query = queryParser.Parse("*" + searchterm + "*"); // Using wildcard at the beginning
Happy coding!

4 comments:

Anonymous said...

hey i just read your artical, that's very interessting, i thought a wildcard at the beginning of a searchterm.

very helpful
thank you

Anonymous said...

hey i was looking for preceding wildcard search and ur solution provided me precise solution thnx a lot

Anonymous said...

thanks alot :)

Anonymous said...

Thanks.

Post a Comment

All comments, questions and other feedback is much appreciated. Thank you!