-
-
Save RyanABailey/c949566b50eb4279227e to your computer and use it in GitHub Desktop.
Sitecore searcher
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 class Searcher | |
| { | |
| public static List<SearchResult> DoSearch(string searchTerm) | |
| { | |
| var searchIndex = ContentSearchManager.GetIndex("MySearchIndex"); // Get the search index | |
| var searchPredicate = GetSearchPredicate(searchTerm); // Build the search predicate | |
| using (var searchContext = searchIndex.CreateSearchContext()) // Get a context of the search index | |
| { | |
| var searchResults = searchContext.GetQueryable<SearchModel>().Where(searchPredicate); // Search the index for items which match the predicate | |
| // 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(); | |
| var myResults = new List<SearchResult>(); | |
| foreach (var hit in fullResults.Hits) | |
| { | |
| myResults.Add(new SearchResult | |
| { | |
| PageDescription = hit.Document.PageDescription, | |
| Title = hit.Document.ItemName, | |
| Url = hit.Document.Url | |
| }); | |
| } | |
| return myResults; | |
| } | |
| } | |
| public static Expression<Func<SearchModel, bool>> GetSearchPredicate(string searchTerm) | |
| { | |
| 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); | |
| predicate = predicate.Or(x => x.Introduction.Like(searchTerm)).Boost(1.2f); | |
| predicate = predicate.Or(x => x.Maintext.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); | |
| predicate = predicate.Or(x => x.Introduction.Contains(searchTerm)).Boost(2.0f); | |
| predicate = predicate.Or(x => x.Maintext.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(1.0f); | |
| predicate = predicate.Or(x => x.PageDescription.Contains(t)).Boost(1.0f); | |
| predicate = predicate.Or(x => x.Introduction.Contains(t)).Boost(1.0f); | |
| predicate = predicate.Or(x => x.Maintext.Contains(t)).Boost(1.0f); | |
| } | |
| return predicate; | |
| } | |
| /// <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("introduction")] | |
| public string Introduction { get; set; } | |
| [IndexField("text")] | |
| public string Maintext { get; set; } | |
| } | |
| /// <summary> | |
| /// Custom search result model for binding to front end | |
| /// </summary> | |
| public class SearchResult | |
| { | |
| public string Title { get; set; } | |
| public string Url { get; set; } | |
| public string PageDescription { get; set; } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment