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
| pubic class Searcher | |
| { | |
| var searchIndex = ContentSearchManager.GetIndex("MySearchIndex"); // Get the search index | |
| var searchPredicate = BuildSearchPredicate(searchRequest); // 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 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 searchFacets = searchContext.GetQueryable<SearchModel>().Filter(searchPredicate).FacetOn(x => x.Category).GetFacets(); | |
| var categoryFacets = searchFacets.Categories.Where(x => x.Name == "categoryfacet").FirstOrDefault(); | |
| var facets = new List<SearchFacet>(); | |
| if(categoryFacets != null) | |
| { | |
| foreach (var facet in categoryFacets.Values) | |
| { | |
| facets.Add(new SearchFacet | |
| { |
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
| <fieldMap type="Sitecore.ContentSearch.FieldMap, Sitecore.ContentSearch"> | |
| <fieldNames hint="raw:AddFieldByFieldName"> | |
| <field fieldName="categoryfacet" storageType="YES" indexType="TOKENIZED" vectorType="NO" boost="1f" type="System.String" settingType="Sitecore.ContentSearch.LuceneProvider.LuceneSearchFieldConfiguration, Sitecore.ContentSearch.LuceneProvider"> | |
| <Analyzer type="Sitecore.ContentSearch.LuceneProvider.Analyzers.LowerCaseKeywordAnalyzer, Sitecore.ContentSearch.LuceneProvider" /> | |
| </field> | |
| </fieldNames> | |
| </fieldMap> | |
| <fields hint="raw:AddComputedIndexField"> | |
| <field fieldName="categoryfacet" storageType="yes" indexType="untokenized" |
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); |
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
| namespace MyProject | |
| { | |
| public class IndexType : IComputedIndexField | |
| { | |
| /// <inheritdoc /> | |
| public string FieldName { get; set; } | |
| /// <inheritdoc /> | |
| public string ReturnType { get; set; } | |
| /// <inheritdoc /> |
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 class IndexPDF : IComputedIndexField | |
| { | |
| /// <inheritdoc /> | |
| public string FieldName { get; set; } | |
| /// <inheritdoc /> | |
| public string ReturnType { get; set; } | |
| /// <inheritdoc /> | |
| public object ComputeFieldValue(IIndexable indexable) | |
| { |
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); // 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 | |
| // This will get all of the results, which is not reccomended | |
| var fullResults = searchResults.GetResults(); |
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) | |
| { | |
| var predicate = PredicateBuilder.True<SearchModel>(); // Items which meet the predicate | |
| // search logic cut down | |
| predicate = predicate.Or(x => x.DispalyName.Like(searchTerm)).Boost(60); | |
| // Only show items which are not excluded from search | |
| predicate = predicate.And(x => x.ExcludeFromSearch == false); |
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
| <!--exclude from search--> | |
| <field fieldName="exclude from search" storageType="YES" indexType="TOKENIZED" vectorType="NO" boost="1f" type="System.Boolean" | |
| settingType="Sitecore.ContentSearch.LuceneProvider.LuceneSearchFieldConfiguration, Sitecore.ContentSearch.LuceneProvider" | |
| patch:after="field[last()]" /> | |
| <field fieldName="exclude_from_search" storageType="YES" indexType="TOKENIZED" vectorType="NO" boost="1f" type="System.Boolean" | |
| settingType="Sitecore.ContentSearch.LuceneProvider.LuceneSearchFieldConfiguration, Sitecore.ContentSearch.LuceneProvider" | |
| patch:after="field[last()]" /> | |
| </fieldNames> |
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
| <fields hint="raw:AddComputedIndexField"> | |
| <field fieldName="childcontent" storageType="yes" indexType="untokenized" | |
| patch:after="field[last()]">MyNamespace.IndexChildContent, MyNamespace</field> | |
| </fields> |