Skip to content

Instantly share code, notes, and snippets.

@sreekrishnan1993
Last active April 5, 2020 11:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sreekrishnan1993/373a8e860aa80cb4c44c7227aa9f8f33 to your computer and use it in GitHub Desktop.
Save sreekrishnan1993/373a8e860aa80cb4c44c7227aa9f8f33 to your computer and use it in GitHub Desktop.
Boosting, Keyword Splitting
public SearchResults<CustomSearchResultItem> SearchItemsByKeyword(string keyword)
{
IQueryable<CustomSearchResultItem> query = context.GetQueryable<CustomSearchResultItem>();
IQueryable<CustomSearchResultItem> query1 = context.GetQueryable<CustomSearchResultItem>();
IQueryable<CustomSearchResultItem> query2 = context.GetQueryable<CustomSearchResultItem>();
var expression = PredicateBuilder.True<CustomSearchResultItem>();
var expression1 = PredicateBuilder.True<CustomSearchResultItem>();
var expression2 = PredicateBuilder.True<CustomSearchResultItem>();
// Applying normal boosting to all the fields
var namePredicate = PredicateBuilder.False<CustomSearchResultItem>();
namePredicate = namePredicate.Or(w => w.Title.Equals(keyword).Boost(5f));
namePredicate = namePredicate.Or(w => w.Description.Equals(keyword).Boost(3f));
namePredicate = namePredicate.Or(w => w.Category.Equals(keyword).Boost(2f));
namePredicate = namePredicate.Or(w => w.Content.Contains(keyword));
// For splitting the keywords
var splitwords = keyword.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
var splitnamePredicate = PredicateBuilder.True<CustomSearchResultItem>();
foreach (string term in splitwords)
{
splitnamePredicate = splitnamePredicate1.And(p => p.Title.Contains(term.Trim()).Boost(4f));
}
namePredicate = namePredicate.Or(splitnamePredicate);
// For Permuted splitted words
var permutedSplitnamePredicate = PredicateBuilder.True<CustomSearchResultItem>();
var permutedSplitwords = findWords(splitwords);
int count = 0;
foreach (string term in permutedSplitwords)
{
float value = (float)(4 - (0.002 * count));
permutedSplitnamePredicate = permutedSplitnamePredicate.Or(p => p.Title.Contains(term.Trim()).Boost(value));
count++;
}
namePredicate = namePredicate.Or(permutedSplitnamePredicate);
// Final expression with all the predicates
expression = expression.And(namePredicate);
// Final query for performing the union of results
var statusActivePredicate = PredicateBuilder.False<CustomSearchResultItem>();
statusActivePredicate = statusActivePredicate.Or(w => w.status.Equals("active", StringComparison.InvariantCultureIgnoreCase));
expression1 = expression.And(statusActivePredicate);
query1 = query.Where(expression1);
var results1 = query1.GetResults();
var statusDiscontinuedPredicate = PredicateBuilder.False<CustomSearchResultItem>();
statusDiscontinuedPredicate = statusDiscontinuedPredicate.Or(w => w.status.Equals("discontinued", StringComparison.InvariantCultureIgnoreCase));
expression2 = expression.And(statusDiscontinuedPredicate);
query2 = query.Where(expression2);
var results2 = query2.GetResults();
var unionResults = results1.Union(results2);
SearchResults<CustomSearchResultItem> finalResults = new SearchResults<CustomSearchResultItem>(unionResults, unionResults.Count());
return finalResults;
}
public static String[] findWords(params string[] args)
{
if (args.Count() == 0)
{
return new String[] { "" };
}
else
{
String[] oldWords = findWords(args.Skip(1).ToArray());
String[] newWords = oldWords.Where(word => word == "" || word.Split(new String[] { " " }, StringSplitOptions.RemoveEmptyEntries)[0] == args[1])
.Select(word => (args[0] + " " + word).Trim()).ToArray();
return oldWords.Union(newWords).ToArray();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment