1

I am trying to build some custom search logic in Umbraco 7 (7.3.6) which will search for multiple terms supplied by a user, where those terms may include phrases enclosed in quotes.

I have the following code which takes the supplied term, uses a regex to split individual terms (whilst maintaining those enclosed in quotes), then uses a series of GroupedOr calls to search against multiple fields

var searcher = Examine.ExamineManager.Instance.SearchProviderCollection[this.searchConfig.SiteSearchProviderName];

var searchCriteria = searcher.CreateSearchCriteria(Examine.SearchCriteria.BooleanOperation.Or);

var splitTerms = Regex.Matches(term, @"[\""].+?[\""]|[^ ]+")
    .Cast<Match>()
    .Select(m => m.Value)
    .ToArray();

var query = searchCriteria.GroupedOr(
    new[] { BaseContent.FIELD_NodeName },
    this.GetValues(splitTerms, 3, 0.8F))
.Or()
.GroupedOr(
    new[] { this.searchConfig.ContentFieldName },
    this.GetValues(splitTerms, 1, 0.8F));

This is the GetValues method:

private IExamineValue[] GetValues(string[] terms, float boost, float fuzziness)
{
    return terms
        .Select(t => t.Boost(boost).Value.Fuzzy(fuzziness))
        .ToArray();
}

I have a document in my index which contains the term "The quick brown fox jumps over the lazy dog". If I pass the string "\"brown fox\"" through the above logic then examine my query I can see my query object contains the following Lucene query:

(nodeName:"brown fox"~0.8) (_content:"brown fox"~0.8)

However, when I use this to build a search query as follows, I get no results.

var searchQuery = searcher
    .Search(query.Compile(), 100)
    .OrderByDescending(x => x.Score)
    .TakeWhile(x => x.Score > 0.05f);

But if I run the exact same Lucene query using Luke I get the result I was expecting.

Is anyone able to help me understand this? Extra marks if you can explain why my boost values aren't being added to the Lucene query!!

Tom Troughton
  • 3,233
  • 2
  • 25
  • 55

0 Answers0