Thursday 6 April 2017

Solr Keyword Search with hyphen not working

Hello Devs,

While doing hands on with SOLR, faced issues while searching for keywords with hyphen. Just an overview, I have created list of items in Sitecore which has below fields:

Employee Name, Employee Birthdate, Employee City and Employee Bio and I’m reading its values from my custom SOLR Index.

When we search with Employee Name (for e.g. John - Smith), it does’nt give any search results even if its present in index.

The reason is Sitecore field types of “html|rich text|single-line text|multi-line text|text|memo|image|reference” are defined as a text field and it uses solr.StandardTokenizerFactory.

This Standard Tokenizer treats whitespace and punctuation as delimiters and it discards them (except (dots)). To read more about Tokenizers click here.
First, you need to add the Field (e.g. Employee name) in

Sitecore.ContentSearch.Solr.DefaultIndexConfiguration.config under

<fieldNames hint="raw:AddFieldByFieldName"> 

section or you can create a custom index configuration file and add the require fields as below:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <contentSearch>
      <!-- Configuration sections for indexes -->
      <indexConfigurations>
        <!-- If an index has no configuration specified, it will use the configuration below. The configuration is not merged if the index also has
         configuration, it is either this configuration or the index configuration. -->
    <defaultSolrIndexConfiguration  type="Sitecore.ContentSearch.SolrProvider.SolrIndexConfiguration,    Sitecore.ContentSearch.SolrProvider">

          <!-- DEFAULT FIELD MAPPING
               This field map allows you to take full control over how your data is stored in the index. This can affect the way data is queried, performance of searching and how data is retrieved and casted to a proper type in the API.
            -->
          <fieldMap type="Sitecore.ContentSearch.SolrProvider.SolrFieldMap, Sitecore.ContentSearch.SolrProvider">
            <fieldNames hint="raw:AddFieldByFieldName">
                   <field fieldName="Employee Name" returnType="string" />
            </fieldNames>
         </fieldMap>
        </defaultSolrIndexConfiguration>
      </indexConfigurations>
    </contentSearch>
  </sitecore>
</configuration>

In Schema.xml file for the SOLR index you created, comment out

<fieldType name="string" class="solr.StrField" sortMissingLast="true" />

and add,

<fieldType name="string" class="solr.TextField" sortMissingLast="true">
      <analyzer type="index">
        <tokenizer class="solr.KeywordTokenizerFactory" />
        <filter class="solr.LowerCaseFilterFactory" />
      </analyzer>
      <analyzer type="query">
        <tokenizer class="solr.KeywordTokenizerFactory" />
        <filter class="solr.LowerCaseFilterFactory" />
      </analyzer>
 </fieldType>

Once you make all these changes restart your SOLR Service once and Re-build index. Now Search for keywords with – in it and you would see the results.

Happy Coding 😊

Wednesday 5 April 2017

Issue with Publishing related items from experience editor

Hello Devs,

We faced one issue while publishing related items from Experience Editor. We are passing a datasource item (e.g. XXX) to a component.

The Datasource item have a multilist field where we select the items (Item: A, B) to be displayed.

When we Update field values of the below selected items (Item: A or B) from the experience editor. The values are updated from the experience editor and were reflected in content editor as well but when we publish with (Publish sub Items and Publish Related Items) the changes are not getting reflected.

Question: Is this an intended behaviour? (We raised a support ticket for it)

Yes, Sitecore does’nt process second level reference and therefore it’s not process by Sitecore. The reason is to prevent huge number of items being published. Sitecore doesn't process references of references.

You can customize your solution to implement the behaviour in the following way:

1. Create your custom processor based on the Sitecore.Publishing.Pipelines.GetItemReferences.AddItemLinkReferences 
one (i.e. copy sources of processor).

2. Modify the GetReferences() method so that it would recursively process reference references if item is being referenced by the Renderings field (i.e. reference is a data source item).

3. Replace predefined processor with your custom one.

Click here to download the custom code file provided by Sitecore.

That’s it !!!

Happy Coding 😊