Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do partial word searches in Lucene.NET?

Tags:

I have a relatively small index containing around 4,000 locations. Among other things, I'm using it to populate an autocomplete field on a search form.

My index contains documents with a Location field containing values like

  • Ohio
  • Dayton, Ohio
  • Dublin, Ohio
  • Columbus, Ohio

I want to be able to type in "ohi" and have all of these results appear and right now nothing shows up until I type the full word "ohio".

I'm using Lucene.NET v2.3.2.1 and the relevant portion of my code is as follows for setting up my query....

BooleanQuery keywords = new BooleanQuery();
QueryParser parser = new QueryParser("location", new StandardAnalyzer());
parser.SetAllowLeadingWildcard(true);
keywords.Add(parser.Parse("\"*" + location + "*\""), BooleanClause.Occur.SHOULD);
luceneQuery.Add(keywords, BooleanClause.Occur.MUST);

In short, I'd like to get this working like a LIKE clause similar to

SELECT * from Location where Name LIKE '%ohi%'

Can I do this with Lucene?