Category: ContentSearch

  • Customize Solr Managed Schema in Sitecore 9+

    In Sitecore 9+ we can now populate Solr managed schema through the Sitecore control panel. It is still possible to use the legacy schema.xml files, but it is actually a lot more comfortable to be able to just deploy the schema with one click.

    Sitecore currently has some basic schema which it populates and has created the contentSearch.PopulateSolrSchema pipeline as extension point. Mikael Högberg has written a post describing this extension point and I’ve built the following simple processor based on that. It reads Managed Schema update commands as XML and applies them to the indexes you have specified (pipe-separated)

    Feel free to use and extend it.

    Caveat: Solr only accepts the add-field-type command if the field is not contained in the managed schema yet. The replace-field-type will only work if it is already there. This is a bit annoying and could be worked around by checking if the field exists in the schema in code and then changing add to replace and vice versa.

    Also, the schema update will fail silently if there are any errors in the update commands. I’ve found it helpful to first test the update commands as JSON against Solr’s API directly and then convert that JSON to XML.

    Update 05.11.2018:
    Added a add-or-replace-field-type command which is automatically replaced to “add-field-type” or “replace-field-type” depending if the field type already exists in the schema.

  • Duplicate entries in custom index on incremental update

    I’ve had a problem with a custom index some time ago. Entries were showing up multiple times when doing incremental updates of the index i.E. when saving an item. The only thing that would clean up the index again would be a full index rebuild.

    The reason for this happening was, on incremental updates, Sitecore would add a new document to the index and leave the old one untouched thus duplicating the entry. The default indexes (core,master,web) would not show the same behaviour which made me check each and every field of the default fieldMap configuration… and voilà: The _uniqueid field needs to be converted to lowercase in order to be matched correctly. (GUIDs are saved as lowercase in lucene but are uppercase in .NET) To resolve the issue, all I needed to do is add the _uniqueid field to the fieldNames configuration as follows:

    I also added the _template field to ensure filtering by template ID works correctly. You generally need to do this with every GUID field you’re querying.

    Unfortunately, there are still several custom index tutorials out there which don’t take this into account. If you’re looking for a clean template to start a new custom index, check out Mikkel’s post.

  • Use System.Boolean when indexing checkbox fields

    I faced the issue that checkbox values were not being updated in the index correctly. When I unchecked a checkbox and saved, the index would still have “1” stored as value for my field. To prevent this issue, use Sytem.Boolean as type instead of System.String.

    <field fieldName=”MyCheckboxField” storageType=”YES” indexType=”TOKENIZED” vectorType=”NO” boost=”1f” type=”System.Boolean” settingType=”Sitecore.ContentSearch.LuceneProvider.LuceneSearchFieldConfiguration, Sitecore.ContentSearch.LuceneProvider” />

    Internally, lucene still stores “0” or “1”, but when using System.String, values of false would be considered as “nothing” when updating the index, leading to not updating the value of the field. Changing to System.Boolean made it work.

     

  • Determining the Context Index when performing bucket queries

    We have faced a problem when working with multilists and custom indexes. Some of our multilists would not contain any data even though the lucene queries would have matches when querying the index directly. We found that the problem had to do with the way Sitecore determines the index to perform the bucket query on.

    Reason

    When working with custom indexes in the Backend (i.E. bucket queries, multilist with search,…), Sitecore has a method to determine which index is most appropriate to use for the base item (i.E. the bucket container) you’re querying on.

    In version 7.2, basically, Sitecore checks, which indexes are defined for the context database and then checks if the base item is included in the respective index by checking include/exclude templates. The first matching index is used. It is important to know that the root paths configured on the crawlers don’t have any affect on this. That means in some cases an index may be selected that doesn’t actually contain any information about the data i’m querying. I bug imo.

    Workaround

    To work around this you have the following options:

    a) Index config files are read in alphabetical order. You might be able to move around your custom indexes to let Sitecore select the appropriate index.
    b) Hook into the contentSearch.getContextIndex pipeline and implement your own logic.

    Since a) is pretty straighforward but might not work for all scenarios, I’ll show an example of b):

    I wrote a simple hook that allows me to read the name of a custom index from a field on the base item (i.E. the item bucket base item):This allows me to use a field named CustomSearchIndex to optionally set the index name used for querying.

    AppliedIndexChecker admin page

    To check which context index is resolved on specific items, i have implemented a simple admin page that allows me to resolve the index based on a GUID or an item path.

    Summary

    This workaround does the job, but it would of course be nicer to have Sitecore correctly resolving context indexes. Also it would be good to be able to set the index directly in the multilist / bucket configuration. I hope there will be an improvement here in future versions.

  • Unable to create document builder – crawling exception after upgrading to SC 7.2

    After upgrading to Sitecore 7.2 I ran into this exception in the crawling log when trying to index PDF documents:
    Unable to create document builder (). Please check your configuration. We will fallback to the default for now.

    The solution was to add the document builder type to my custom index configuration:

    <documentBuilderType>Sitecore.ContentSearch.LuceneProvider.LuceneDocumentBuilder, Sitecore.ContentSearch.LuceneProvider</documentBuilderType>

    After that, reading PDF content through my iFilter provider worked fine again.

  • NullReferenceException in custom Index after upgrading to Sitecore 7.2

    After upgrading to Sitecore 7.2 we got the following exception when trying to build our custom indexes:

    System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NullReferenceException: Object reference not set to an instance of an object.
     at Sitecore.ContentSearch.LuceneProvider.LuceneIndex.Reset()
     at Sitecore.ContentSearch.LuceneProvider.LuceneIndex.PerformRebuild(IndexingOptions indexingOptions, CancellationToken cancellationToken)
     at Sitecore.ContentSearch.LuceneProvider.LuceneIndex.Rebuild()
     --- End of inner exception stack trace ---
     at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
     at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
     at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
     at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
     at (Object , Object[] )
     at Sitecore.Pipelines.CorePipeline.Run(PipelineArgs args)
     at Sitecore.Jobs.Job.ThreadEntry(Object state)
    

    The resolution to this is quite simple. Sitecore has included <initializeOnAdd> to the index configuration. If this field is missing in the configuration, building the index fails. Just make sure you have the following line to your custom index configuration:

    <myCustomIndexConfiguration type="Sitecore.ContentSearch.LuceneProvider.LuceneIndexConfiguration, Sitecore.ContentSearch.LuceneProvider">
    
    <indexAllFields>true</indexAllFields>
    
    <!-- Needed since SC7.2 -->
    <initializeOnAdd>true</initializeOnAdd>
     ....