Last active
March 8, 2016 00:59
-
-
Save RyanABailey/c6a2973db0053f792cc6 to your computer and use it in GitHub Desktop.
Facet Search Code
This file contains hidden or 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
| public static Expression<Func<SearchModel, bool>> GetSearchPredicate(string searchTerm, string type) | |
| { | |
| var predicate = PredicateBuilder.True<SearchModel>(); // Items which meet the predicate | |
| // Search the whole phrase - LIKE | |
| predicate = predicate.Or(x => x.DispalyName.Like(searchTerm)).Boost(1.2f); | |
| predicate = predicate.Or(x => x.PageDescription.Like(searchTerm)).Boost(1.2f); | |
| // Search the whole phrase - CONTAINS | |
| predicate = predicate.Or(x => x.DispalyName.Contains(searchTerm)).Boost(2.0f); | |
| predicate = predicate.Or(x => x.PageDescription.Contains(searchTerm)).Boost(2.0f); | |
| // Search the individual words | |
| foreach (var t in searchTerm.Split(' ')) | |
| { | |
| var tempTerm = t; | |
| predicate = predicate.Or(x => x.DispalyName.Contains(t)).Boost(20); | |
| predicate = predicate.Or(x => x.PageDescription.Contains(t)).Boost(20); | |
| } | |
| // Only show items which are not excluded from search | |
| predicate = predicate.And(x => x.ExcludeFromSearch == false); | |
| // Type filtering | |
| predicate = predicate.And(GetFacetPredicate(type)); | |
| return predicate; | |
| } | |
| public static Expression<Func<SearchModel, bool>> GetFacetPredicate(string type) | |
| { | |
| var predicate = PredicateBuilder.True<SearchModel>(); // Items which meet the predicate | |
| if (type != null) | |
| { | |
| predicate = predicate.Or(x => x.Type == type); | |
| } | |
| return predicate; | |
| } |
This file contains hidden or 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> | |
| /// Search item mapped to Lucene index | |
| /// </summary> | |
| public class SearchModel | |
| { | |
| [IndexField("_name")] | |
| public string ItemName { get; set; } | |
| [IndexField("_displayname")] | |
| public string DispalyName { get; set; } | |
| [IndexField("_templatename")] | |
| public string TemplateName { get; set; } | |
| [IndexField("urllink")] | |
| public string Url { get; set; } | |
| [IndexField("page_description")] | |
| public string PageDescription { get; set; } | |
| [IndexField("exclude_from_search")] | |
| public bool ExcludeFromSearch { get; set; } | |
| [IndexField("typefacet")] | |
| public string Type { get; set; } | |
| } |
This file contains hidden or 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
| var searchIndex = ContentSearchManager.GetIndex("MySearchIndex"); // Get the search index | |
| var searchPredicate = GetSearchPredicate(searchTerm, "Page"); // Build the search predicate | |
| using (var searchContext = searchIndex.CreateSearchContext()) // Get a context of the search index | |
| { | |
| var searchResults = searchContext.GetQueryable<SearchModel>().Filter(searchPredicate); // Search the index for items which match the predicate | |
| var searchFacets = searchContext.GetQueryable<SearchModel>().Filter(searchPredicate).FacetOn(x => x.Type).GetFacets(); // Gets facets - useful when you get paged results | |
| // This will get all of the results, which is not reccomended | |
| var fullResults = searchResults.GetResults(); | |
| // This is better and will get paged results - page 1 with 10 results per page | |
| //var pagedResults = searchResults.Page(1, 10).GetResults(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment