Indexing Sitecore field in Solr

Hey I am new to this technology , How I can index a particular field of sitecore in solr ?

  • - You can create computed Index for same. By default it index all the fields on Item but if you want to make it forcefully then follow below steps -


    Add a class file for computed index as below -

    public class EventType : IComputedIndexField
    {
    /// <summary>
    /// Gets or sets the name of the field.
    /// </summary>
    /// <value>
    /// The name of the field.
    /// </value>
    public string FieldName { get; set; }

    /// <summary>
    /// Gets or sets the return type.
    /// </summary>
    /// <value>
    /// The return type.
    /// </value>
    /// <remarks>
    /// Used for index schema generation.
    /// </remarks>
    public string ReturnType { get; set; }

    /// <summary>
    /// Computes the field value.
    /// </summary>
    /// <param name="indexable">The indexable item.</param>
    /// <returns>Object for date and time string.</returns>
    public object ComputeFieldValue(IIndexable indexable)
    {
    var indexableItem = indexable as SitecoreIndexableItem;
    if (indexableItem == null)
    {
    return null;
    }

    var item = (Item)indexableItem;
    if (item.FieldHasValue(Search.Constants.EventTypeFieldID))
    {
    var eventType = item.ReferenceFieldTargetItem(Xcentium.Feature.Search.Constants.EventTypeFieldID);
    if (eventType != null)
    {
    return eventType[Xcentium.Foundation.Settings.Templates.EventType.Fields.Title];
    }
    }

    return string.Empty;
    }
    }

    This is sample code you can update it as per your need.


    Create a config file or you can add it to any other config. And add the class file reference -

    <fields hint="raw:AddComputedIndexField">
    <field fieldName="eventtype">
    Xcentium.Feature.Search.ComputedFields.EventType , Xcentium.Feature.Search
    </field>
    </fields>

    This way you can add a field in solr.
  • In reply to Harsh Thakur:

    - C# code you need to create new class file and placed there and for config code need to create new config file and place there let me paste complete config file for you -


    <?xml version="1.0" encoding="utf-8"?>
    <configuration xmlns:patch="www.sitecore.net/.../" xmlns:role="www.sitecore.net/.../" xmlns:search="www.sitecore.net/.../">
    <sitecore>
    <contentSearch>
    <indexConfigurations>
    <defaultSolrIndexConfiguration type="Sitecore.ContentSearch.SolrProvider.SolrIndexConfiguration, Sitecore.ContentSearch.SolrProvider">
    <fieldMap type="Sitecore.ContentSearch.SolrProvider.SolrFieldMap, Sitecore.ContentSearch.SolrProvider">
    <fieldNames hint="raw:AddFieldByFieldName">
    <field fieldName="eventtype" returnType="string" />
    </fieldNames>
    </fieldMap>

    <documentOptions type="Sitecore.ContentSearch.SolrProvider.SolrDocumentBuilderOptions, Sitecore.ContentSearch.SolrProvider">
    <fields hint="raw:AddComputedIndexField">
    <field fieldName="eventtype" returnType="string">Xcentium.Feature.Search.ComputedFields.EventType , Xcentium.Feature.Search</field>
    </fields>
    </documentOptions>
    </defaultSolrIndexConfiguration>
    </indexConfigurations>
    </contentSearch>
    </sitecore>
    </configuration>


    Just create a config file and paste the above code change you class name and field name and I think will work for you.