Skip to content

Instantly share code, notes, and snippets.

@rhullah
Created March 25, 2016 13:41
Show Gist options
  • Save rhullah/e0ab63c9c3acbbd14a33 to your computer and use it in GitHub Desktop.
Save rhullah/e0ab63c9c3acbbd14a33 to your computer and use it in GitHub Desktop.
Overriding Sitefinity's Lucene Search Service
// Example of custom Search Service that can add boosting
public class DaytonLuceneSearchService : LuceneSearchService
{
private static readonly Regex LuceneEscape = new Regex(@"(\+|-|&&|\|\||!|\(|\)|\{|\}|\[|\]|\^|""|~|\*|\?|:|\\)", RegexOptions.Compiled);
protected override string BuildQuery(ISearchQuery input)
{
input.Text = LuceneEscape.Replace(input.Text, match => @"\\" + match.Captures[0].Value);
string baseQuery = base.BuildQuery(input);
if (input is CustomSearchQuery)
{
var boostedQuery = input as CustomSearchQuery;
if (boostedQuery.BoostedField != null)
{
var kvp = boostedQuery.BoostedField.Value;
baseQuery += " AND (" + kvp.Key + ":\"" + kvp.Value + "\"^100 OR *:*)";
}
}
return baseQuery;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment