Skip to content

Instantly share code, notes, and snippets.

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<contentSearch>
<configuration>
<DefaultIndexConfiguration>
<fields hint="raw:AddComputedIndexField">
<field fieldName="myCustomComputedIndexField" returnType="string">
<!-- Fully qualified assembly path to the class -->
YourAssembly.ComputedFields.MyComputedIndexField,YourAssembly
</field>
public class MyComputedIndexField : IComputedIndexField
{
public object ComputeFieldValue(IIndexable indexable)
{
// Check that we have something to index
if (indexable == null)
return null;
// Check that it is a Sitecore indexable item
var indexableItem = indexable as SitecoreIndexableItem;
using System;
using Sitecore.ContentSearch;
using Sitecore.ContentSearch.ComputedFields;
using Sitecore.ContentSearch.Utilities;
using Sitecore.Data;
using Sitecore.Data.Items;
namespace Examples.ComputedIndexFields
{
public class CombinedDateTime : IComputedIndexField
ISearchIndex index = ContentSearchManager.GetIndex("sitecore_master_index")
using (IProviderSearchContext context = index.CreateSearchContext())
{
// Notice that I've swapped out the 'SearchItemResult' with 'MySearchItemResult'
// in the method call GetQueryable<T>, and now I'm able to use the property
// 'MyTextField' in the filtering
var results = context.GetQueryable<MySearchItemResult>()
.Where(x => x.MyTextField.Contains("Some value"));
}
using System.Collections.Generic;
using System.Linq;
using Sitecore.ContentSearch;
using Sitecore.ContentSearch.ComputedFields;
using Sitecore.ContentSearch.Utilities;
using Sitecore.Data;
using Sitecore.Data.Items;
namespace Examples.ComputedIndexFields
{
// A custom implementation of the SearchResultItem
public class MySearchResultItem : SearchResultItem
{
// I've added a new property, that will be mapped to the Solr field named 'mytextfield'
[IndexField("mytextfield")]
public string MyTextField { get; set; }
}
using (var context = ContentSearchManager.GetIndex("sitecore_master_index").CreateSearchContext())
{
var facets = new FacetResults();
facets = context.GetQueryable<SearchResultItem>()
.FacetOn(x => x.Language)
.GetFacets();
foreach (var facetCategories in facets.Categories)
{
public class ProductSearcher
{
public ProductSearchResult Search(SearchCriteria criteria)
{
using (var context = ContentSearchManager.GetIndex("sitecore_master_index").CreateSearchContext())
{
var filterPredicate = PredicateBuilder.True<SearchResultItem>();
// Only take products created over the past year
filterPredicate = filterPredicate
// Lower and upper dates are still the same as in the previous example
var results = context.GetQueryable<SearchResultItem>()
.Where(x => x.CreatedDate.Between(lowerDate, upperDate, Inclusion.Both));
var lowerDate = new DateTime(2015, 10, 1);
var upperDate = new DateTime(2015, 10, 14);
var results = context.GetQueryable<SearchResultItem>()
.Where(x => x.CreatedDate >= lowerDate && x.CreatedDate <= upperDate);