RESULT ITEMS FOR SITECORE CONTENT SEARCH

  • By Andrew Ward
Andrew Ward

The base result item for the Sitecore content search is called SearchResultItem and is located with the Sitecore.ContentSearch.SearchTypes namespace, this by default has quite a fair few useful properties attached to it out of the box such as:

1) TemplateName
2) TemplateId
3) Content
4) Language
5) Paths


The properties above are available for use within queries against the index to refine your result, you can read more on writing these here. You’re not limited to just the out of the box properties, you are able to create your Models that inherit from the SearchResultItem and then add your own properties that are mapped to index fields using the IndexField data annotation with in your code file.

What does that look like?


The below is a model of what my news items look like, the properties you can see that I have added are

1) Title 
a. Potential Sitecore field types: single-line text, multi-line text, rich text, text and memo field types.
2) Date 
a. Potential Sitecore field types: date and datetime field types.
3) News Type 
a. Potential Sitecore field types: droplink, droptree, grouped droplink and tree field types.
4) Authors
a. Potential Sitecore field types: checklist, multilist, treelist, treelistex, tree list, multilist with search and treelist with search field types.
5) Show In Search Listing
a. Sitecore field: checkbox field type.


public class NewsResultItem : SearchResultItem
{
[IndexField("title")]
public string Title { get; set; }
[IndexField("date")]
[TypeConverter(typeof(IndexFieldDateTimeValueConverter))]
public virtual System.DateTime Date { get; set; }
[IndexField("news_type")]
[TypeConverter(typeof(IndexFieldIDValueConverter))]
public virtual ID NewsType { get; set; }
[IndexField("authors")]
[TypeConverter(typeof(IndexFieldEnumerableConverter))]
public virtual IEnumerable Authors { get; set; }
[IndexField("show_in_search_listings")]
[TypeConverter(typeof(IndexFieldBooleanValueConverter))]
public virtual bool ShowInSearchListings { get; set; }
}

Using the Properties in Queries


Now that you have your very own result item, you are now able to use these properties directly within your search queries, however be sure that the index does actually contain your field :).

You are able to construct a Sitecore content search predicate using our NewsResultItem like so:

var predicate = PredicateBuilder.True();
predicate = predicate.And(p => p.NewsType == new ID("{8C853CC0-3E02-4830-9BA3-055A680B74CC}"))
practices = practices.And(r => r.Authors.Contains(new ID("{8C853CC0-3E02-4830-9BA3-055A680B74CC}")));


More on querying within the sitecore content search can be found here.

Field Type Converters

There is a whole host of converters that come with the Sitecore content search and they are all located under the Sitecore.ContentSearch.Coverters namespace, apart from the Boolean converter which is located under Sitecore.ContentSearch.LuceneProvider.Converters.

You can find the type converts section within the following config files, depending on what provider you are using:

1) Sitecore.ContentSearch.Lucene.DefaultIndexConfiguration.config

2) Sitecore.ContentSearch.Solr.DefaultIndexConfiguration.config
scroll back to the top of the current web page