Created
March 25, 2016 13:41
-
-
Save rhullah/e0ab63c9c3acbbd14a33 to your computer and use it in GitHub Desktop.
Overriding Sitefinity's Lucene Search Service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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