Sitecore Custom Index with Selected Fields

It’s Thursday, midweek, there is no time for big blog post. But, there is something in my head that I can use to put together a quick blog post. First of all, I am not blogging about a ground breaking idea here. Anyone understand how Sitecore Index configuration work, can come up with this solution. The reason I am writing this, it can save some time for the developers, if they need anything like this.

The requirement is to create a custom index that will contain only the fields of my choice. My index name is predictive_search_index and the field that I want to add is ‘Predictive Search Keywords’. Following is the config that I need for creating this index with the field of my choice.

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <contentSearch>
      <configuration type="Sitecore.ContentSearch.ContentSearchConfiguration, Sitecore.ContentSearch">
        <indexes hint="list:AddIndex">
          <index id="predictive_search_index" type="Sitecore.ContentSearch.LuceneProvider.LuceneIndex, Sitecore.ContentSearch.LuceneProvider">
            <param desc="name">$(id)</param>
            <param desc="folder">$(id)</param>
            <!-- This initializes index property store. Id has to be set to the index id -->
            <param desc="propertyStore" ref="contentSearch/databasePropertyStore" param1="$(id)" />
            <strategies hint="list:AddStrategy">
              <!-- NOTE: order of these is controls the execution order -->
              <strategy ref="contentSearch/indexUpdateStrategies/onPublishEndAsync" />
            </strategies>
            <commitPolicyExecutor type="Sitecore.ContentSearch.CommitPolicyExecutor, Sitecore.ContentSearch">
              <policies hint="list:AddCommitPolicy">
                <policy type="Sitecore.ContentSearch.TimeIntervalCommitPolicy, Sitecore.ContentSearch" />
              </policies>
            </commitPolicyExecutor>
            <locations hint="list:AddCrawler">
              <crawler type="Sitecore.ContentSearch.SitecoreItemCrawler, Sitecore.ContentSearch">
                <Database>web</Database>
                <Root>/sitecore/content</Root>
              </crawler>
            </locations>
            <configuration ref="contentSearch/indexConfigurations/defaultLuceneIndexConfiguration">
				<indexAllFields>false</indexAllFields>
				<fieldMap ref="contentSearch/indexConfigurations/defaultLuceneIndexConfiguration/fieldMap">
					<fieldNames hint="raw:AddFieldByFieldName">
						<field fieldName="predictive_search_keywords" 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>
				<include hint="list:IncludeField">
					<fieldId>{58EDEA38-B1D9-4A17-9093-C10AAEE229BD}</fieldId> <!-- Predictive Search Keywords field -->
				</include>
            </configuration>
          </index>
        </indexes>
      </configuration>
    </contentSearch>
  </sitecore>
</configuration>

The trick is in the bolded lines in the config. <indexAllFields>false</indexAllFields> indicates that we don’t want to index all fields for the items and <include hint=”list:IncludeField”> has included the Field Id that we want to include in the index.

After building index, I opened the index file in Luke. We can see that predictive_search_keywords is included.There are bunch fields other than the one I choose to include in the index are also present. These are included from contentSearch/indexConfigurations/defaultLuceneIndexConfiguration. These fields are useful when we want to combine the search along with our chosen fields. For example, I can search all locked items with some Predictive Keyword. In this case we have to use the field ‘lock’ from the index. This is a computed field. If we want to exclude these extra fields, we have to replace the DefaultIndexConfiguration with our own IndexConfiguration.

 

 

Advertisements

About Himadri Chakrabarti

I am a software developer architect and a Sitecore MVP. My professional interest is everything and anything related to Software Architecture, .NET, Sitecore, Node.js, NoSQL etc. Outside of my profession, I am a hobbyist photographer. Link to my photography site http://himadriphotography.com/
This entry was posted in Sitecore and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s