Using Solr field name resolution
Overview of how Sitecore automatically maps document (item) field names to Solr field names.
Solr as a search provider dictates conventions for indexing and searching documents. When Sitecore sends a document (item) to Solr for indexing, the item fields must map to Solr fields. An example of mapping is when the Sitecore Item Name
field maps to the Solr _name
field.
Unfortunately, it is not practical to manually match large numbers of Sitecore fields to Solr fields. However, you can use Solr dynamic fields to perform automatic mappings for Sitecore fields.
An example of automatic mapping is when Sitecore maps the Sitecore parsedcreatedby
computed string field to the Solr parsedcreatedby_s
dynamic string field, because parsedcreatedby
is associated with the Sitecore string
index field type that is mapped to the *_s
field name format.
All mappings are specified in the Sitecore.ContentSearch.Solr.DefaultIndexConfiguration.config
file.
Consider, for example, a Sitecore field named amount
. It holds numbers and is therefore a Sitecore number
type field. If the Sitecore field is not manually mapped by name to an index field type, Sitecore automatically maps it by Sitecore field type to a Solr dynamic field, in the following order:
Sitecore maps the Sitecore
number
type field to the Solrfloat
type field in theAddFieldByFieldTypeName
section:<fieldTypes hint="raw:AddFieldByFieldTypeName"> <fieldType fieldTypeName="number" returnType="float" /> ... </fieldType>
Sitecore maps the Solr
float
type field to the{0}_tf
field name format in theAddTypeMatch
section:<typeMatches hint="raw:AddTypeMatch"> <typeMatch typeName="float" type="System.Single" fieldNameFormat="{0}_tf" settingType="Sitecore.ContentSearch.SolrProvider.SolrSearchFieldConfiguration, Sitecore.ContentSearch.SolrProvider" /> ... </typeMatches>
Sitecore replaces the
{0}
part with the Sitecoreamount
field name to become the Solramount_tf
field name.Sitecore indexes the Sitecore
number
type field namedamount
as a Solr dynamicfloat
type field namedamount_tf
.
Overriding mappings by field name
Sitecore can override the mappings in the AddFieldByFieldTypeName
section for each field in the AddFieldByFieldName
section. You can use this feature, for example, to index the Sitecore title
field as a Solr text
type field named title_t
. Sitecore uses the Sitecore field name and not the field type to match the Solr field type:
<fieldTypes hint="raw:AddFieldByFieldName"> <field fieldName="title" returnType="text" /> ... </fieldTypes>
Indexing computed fields
Sitecore maps Sitecore computed fields to Solr dynamic fields in the AddComputedIndexField
and AddTypeMatch
sections. For example, Sitecore maps the Sitecore calculateddimension
computed field to the Solr stringCollection
type field and indexes it as calculateddimension_sm
:
<fields hint="raw:AddComputedIndexField"> <field fieldName="calculateddimension" returnType="stringCollection"> Sitecore.ContentSearch.ComputedFields.CalculatedDimension,Sitecore.ContentSearch </field> ... </fields> <typeMatches hint="raw:AddTypeMatch"> <typeMatch typeName="stringCollection" type="System.Collections.Generic.List`1[System.String]" fieldNameFormat="{0}_sm" multiValued="true" settingType="Sitecore.ContentSearch.SolrProvider.SolrSearchFieldConfiguration, Sitecore.ContentSearch.SolrProvider" /> ... </typeMatches>
Sitecore resolves fields in an almost identical way for searching and indexing. However, there is a small difference because information about the field and value type is missing. For example, when searching for title:Casablanca
, Sitecore resolves the title
field type to a Solr field name format, in the following order:
Sitecore locates the template containing the
title
field.Sitecore learns that the
title
field type is Single-Line Text.Sitecore maps the Sitecore
single-line text
field type to the Solrtext
field type in theAddFieldsByTypeName
section:<fieldTypes hint="raw:AddFieldByFieldTypeName"> <fieldType fieldTypeName="html|rich text|single-line text|multi-line text|text|memo|image|reference" returnType="text" /> ... </fieldTypes>
Sitecore maps the Solr
text
field type to the Solr{0}_t
field name format in theAddTypeMatch
section:<typeMatches hint="raw:AddTypeMatch"> <typeMatch typeName="text" type="System.String" fieldNameFormat="{0}_t" cultureFormat="_{1}" settingType="Sitecore.ContentSearch.SolrProvider.SolrSearchFieldConfiguration, Sitecore.ContentSearch.SolrProvider" /> </typeMatches>
Sitecore replaces the
{0}
part with the Sitecoretitle
field name to resolve the Solrtitle_t
field name.Sitecore sends the search query to Solr:
/select?q=title_t:Casablanca
Searching with LINQ
When Sitecore uses LINQ to run search requests with strongly typed properties, Sitecore uses the .NET runtime type of the property to resolve the Solr field name in the AddTypeMatch
section:
var queryable = index.CreateSearchContext().GetQueryable< CustomSearchResultItem >(); var q = queryable.Where(i => i.Airport.Equals("search term")).ToList(); public class CustomSearchResultItem : SearchResultItem { [IndexField("Airport")] [DataMember] public virtual string Airport { get; set; } }
However, in situations when Sitecore can map a field type name to multiple .NET runtime types in the AddTypeMatch
section, Sitecore uses the template resolver to inquire Solr for the template and the field type.
For example, the Sitecore airport
property is a System.String
type that maps to both the string
and text
.NET runtime types in the AddTypeMatch
section. This results in two field name formats ({0}_s
and {0}_t
) and two Solr dynamic field names (airport_s
and airport_t
):
<typeMatches hint="raw:AddTypeMatch"> <typeMatch typeName="string" type="System.String" fieldNameFormat="{0}_s" settingType="Sitecore.ContentSearch.SolrProvider.SolrSearchFieldConfiguration, Sitecore.ContentSearch.SolrProvider" /> <typeMatch typeName="text" type="System.String" fieldNameFormat="{0}_t" cultureFormat="_{1}" settingType="Sitecore.ContentSearch.SolrProvider.SolrSearchFieldConfiguration, Sitecore.ContentSearch.SolrProvider" /> ... </typeMatches>
Sitecore uses the template resolver to query Solr for the template and the field type, in the following order:
Sitecore sends the resolver request to Solr, for example:
/select?q=_name:(airport)&fq=_templatename:("Template+field")&fq=_indexname:(sitecore_master_index)&rows=10&version=2.2}
Important
If two templates both define the same field, for example the
airport
field, the template resolver returns two results in an undetermined order. Sitecore always chooses the first result, making the resolution inconsistent. This is especially a problem ifairport
is defined as a Single-Line Text field type in one template and a Droplist field type in another template.Solr returns the
single-line text
field type that Sitecore maps to the Solrtext
field type in theAddFieldByFieldTypeName
section:<fieldTypes hint="raw:AddFieldByFieldTypeName"> <fieldType fieldTypeName="html|rich text|single-line text|multi-line text|text|memo|image" returnType="text" /> </fieldTypes>
Sitecore maps the Solr
text
field type to the{0}_t
field name format in theAddTypeMatch
section:<typeMatches hint="raw:AddTypeMatch"> <typeMatch typeName="text" type="System.String" fieldNameFormat="{0}_t" cultureFormat="_{1}" settingType="Sitecore.ContentSearch.SolrProvider.SolrSearchFieldConfiguration, Sitecore.ContentSearch.SolrProvider" /> </typeMatches>
Sitecore replaces the
{0}
part with the Sitecoreairport
property name resolving it to the Solrairport_t
field.Sitecore sends the final search query to Solr:
/select?q=_airport_t:”search term”&fq=_indexname:(sitecore_master_index)
Sitecore Content Search is dependent on templates being indexed to deliver complete and satisfying search results based on correct field type resolutions. Examples of configurations that leaves the templates unindexed are:
The
Root
tag of the crawler, which does not include the/sitecore/templates
sub-tree:<crawler type="Sitecore.ContentSearch.SitecoreItemCrawler, Sitecore.ContentSearch"> <Database>master</Database> <Root>/sitecore/content</Root> </crawler>
The index document options, which are configured to exclude the template field items:
<exclude hint="list:AddExcludedTemplate"> <TemplateField>{455A3E98-A627-4B40-8035-E683A0331AC7}</TemplateField> </exclude>
If your configuration requires it, add the TemplateIndexName
tag to ensure that the templates are indexed. The tag must point to an index whose crawler indexes all Template Field items under the /sitecore/templates
root:
<index
id="sitecore_master_index"
type="Sitecore.ContentSearch.SolrProvider.SolrSearchIndex,
Sitecore.ContentSearch.SolrProvider">
...
<TemplateIndexName>TEMPLATE INDEX NAME</TemplateIndexName>
</index>
With the TemplateIndexName
specified, Sitecore Content Search is able to resolve Solr mappings correctly for Sitecore fields that are used in queries to Solr. Failure to index templates can result in failing search requests or empty search results.