Skip to content

Instantly share code, notes, and snippets.

@RyanABailey
RyanABailey / Search code
Last active March 9, 2016 02:20
Sitecore boost template type
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
}
}
@RyanABailey
RyanABailey / C#
Created March 2, 2016 02:04
Get Facets
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
{
@RyanABailey
RyanABailey / Search index XML
Last active March 2, 2016 01:26
Lucene facet phrase
<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"
@RyanABailey
RyanABailey / Build Predicate
Last active March 8, 2016 00:59
Facet Search Code
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);
@RyanABailey
RyanABailey / IndexType
Created March 1, 2016 22:02
Lucene facet computed field
namespace MyProject
{
public class IndexType : IComputedIndexField
{
/// <inheritdoc />
public string FieldName { get; set; }
/// <inheritdoc />
public string ReturnType { get; set; }
/// <inheritdoc />
@RyanABailey
RyanABailey / IndexPDF
Created March 1, 2016 03:37
Sitecore PDF Indexing
public class IndexPDF : IComputedIndexField
{
/// <inheritdoc />
public string FieldName { get; set; }
/// <inheritdoc />
public string ReturnType { get; set; }
/// <inheritdoc />
public object ComputeFieldValue(IIndexable indexable)
{
@RyanABailey
RyanABailey / search code
Last active March 1, 2016 03:45
Lucene search
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();
@RyanABailey
RyanABailey / predicate
Created February 29, 2016 20:10
Lucene search code
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);
@RyanABailey
RyanABailey / exclude from search
Created February 29, 2016 19:58
Lucene search index
@RyanABailey
RyanABailey / Lucene index
Last active February 28, 2016 22:17
Sitecore custom index field
<fields hint="raw:AddComputedIndexField">
<field fieldName="childcontent" storageType="yes" indexType="untokenized"
patch:after="field[last()]">MyNamespace.IndexChildContent, MyNamespace</field>
</fields>