Last active
July 14, 2016 15:49
-
-
Save StephenPAdams/cd72a071388ca41baf7b63cc3cd1e11e to your computer and use it in GitHub Desktop.
Weighted Examine Search
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
/// <summary> | |
/// List of products, ordered by weight | |
/// </summary> | |
/// <param name="searchTerm"></param> | |
/// <param name="maxItems"></param> | |
/// <param name="pageNumber"></param> | |
/// <param name="memberTypeIds"></param> | |
/// <param name="pagingInfo"></param> | |
/// <returns>List of SearchResultItems</returns> | |
public IList<BaseProduct> SearchProducts(string searchTerm, int maxItems, int pageNumber, IList<int> memberTypeIds, out PagingInfo pagingInfo) | |
{ | |
var memberType = MemberType.Retail; | |
if (memberTypeIds.HasItemsAndNotNull()) | |
memberType = (MemberType)memberTypeIds.FirstOrDefault(); | |
pagingInfo = new PagingInfo(); | |
if (String.IsNullOrEmpty(searchTerm)) | |
return null; | |
var searchProvider = ExamineManager.Instance.SearchProviderCollection["ProductAndVariationSearcher"]; | |
var criteria = ExamineManager.Instance.SearchProviderCollection["ProductAndVariationSearcher"].CreateSearchCriteria(); | |
// Terms and weight | |
var fields = new Dictionary<string, int> | |
{ | |
//get proper field names in here | |
{"DisplayName", 10}, | |
{"SKU", 10}, | |
{"UPC", 10}, | |
{"ShortDescription", 1}, | |
{"Description", 1} | |
}; | |
// Multipliers for match types. We want to give more weight to exact matches | |
const int exactMatch = 5; | |
const int termMatch = 2; | |
var fieldQuery = new StringBuilder(); | |
var splitSearch = searchTerm.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); | |
// Build field query | |
foreach (var field in fields) | |
{ | |
// Full exact match (which has a higher boost) | |
fieldQuery.Append(string.Format("{0}:{1}^{2}", field.Key, "\"" + searchTerm.EscapeForLucene() + "\"", field.Value * exactMatch)); | |
fieldQuery.Append(" "); | |
// NOTE: Phrase match wildcard isn't really supported unless you use the Lucene | |
// API like ComplexPhraseWildcardSomethingOrOther... | |
// Split match | |
foreach (var s in splitSearch) | |
{ | |
//match on each term, no wildcard, higher boost | |
fieldQuery.Append(string.Format("{0}:{1}^{2}", field.Key, s.EscapeForLucene(), field.Value * termMatch)); | |
fieldQuery.Append(" "); | |
//match on each term, with wildcard | |
fieldQuery.Append(string.Format("{0}:{1}*", field.Key, s.EscapeForLucene())); | |
fieldQuery.Append(" "); | |
} | |
} | |
var rawQueryString = String.Format("+nodeTypeAlias:({0}) AND +search_memberTypeIds:({1}) AND +({2}) AND +search_notDirectlyPurchasable:0", "Product ProductVariation", String.Join(" ", memberTypeIds), fieldQuery); | |
criteria.RawQuery(rawQueryString); | |
var results = searchProvider.Search(criteria); | |
pagingInfo = PagingHelpers.GetPagingInfo(results.Count(), maxItems, pageNumber); | |
var products = _umbracoHelper.TypedContent(results.Select(x => x.Id)) | |
.GetPagedItems(pageNumber, maxItems) | |
.ConvertToTypedList<BaseProduct>(); | |
foreach (var result in products) | |
{ | |
var searchResult = results.FirstOrDefault(x => x.Id == result.Id); | |
result.PriceMatrix = new PriceRangeMatrix() | |
{ | |
MinRetailPrice = searchResult.GetDecimalValueSafelyByKey("search_min_retailPrice"), | |
MaxRetailPrice = searchResult.GetDecimalValueSafelyByKey("search_max_retailPrice"), | |
MinDropshipPrice = searchResult.GetDecimalValueSafelyByKey("search_min_dropShipPrice"), | |
MaxDropshipPrice = searchResult.GetDecimalValueSafelyByKey("search_max_dropShipPrice"), | |
MinGovernmentPrice = searchResult.GetDecimalValueSafelyByKey("search_min_governmentPrice"), | |
MaxGovernmentPrice = searchResult.GetDecimalValueSafelyByKey("search_max_governmentPrice"), | |
MinTradePrice = searchResult.GetDecimalValueSafelyByKey("search_min_tradePrice"), | |
MaxTradePrice = searchResult.GetDecimalValueSafelyByKey("search_max_tradePrice"), | |
MinWholesalePrice = searchResult.GetDecimalValueSafelyByKey("search_min_wholesalePrice"), | |
MaxWholesalePrice = searchResult.GetDecimalValueSafelyByKey("search_max_wholesalePrice"), | |
}; | |
} | |
return products; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment