Search multi word phrase with wild cards in lucene

649 Views Asked by At

Using the following code block:

 public void MultiField(string fieldValue, string[] fieldList)
    {
        List<Occur> occurs = new List<Occur>();
        foreach (string field in fieldList)
        {
            occurs.Add(Occur.SHOULD);
        }
        MultiFieldQueryParser parser = new MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_30, fieldList, analyzer);
        parser.AllowLeadingWildcard = true;
        Query qry = parser.Parse(fieldValue.ToLower());

        booleanQuery.Add(qry, Occur.MUST);
    }

where fieldValue is a user input and fieldList is a set list of fields. I am using a standard analyzer.

I need to be able to search multiple words with wildcards enabled. In it's current state when a user enters a search term (for instance "search") the logic in my application will add * to either side making it "*search*". This brings back the expected results.

If, however a user entered "search s" it would search all fields for "*search" and then all fields again for "s*"; returning way more than the desired results. I have tried to escape special characters/whitespace however this also removes the wildcard search as "*" is a special character. I've tried this using the escape method and by adding "\"" in to the fieldValue string. Is there a way to encapsulate the whole phrase to search and append asterisks at the start and end of the search term?

0

There are 0 best solutions below