3

Does anyone have an example of the sitecore index configuration while using SOLR, for keyword tokenizer? I am trying to facet on a field which has multiword strings, but the facets currently being returned are splitting the words in the fields and returning facets.

Eg. I have items with a field for state, and i am trying to facet on the state field - which has values like New Hampshire, South Dakota. But in the results, I get facet values with

Name = New, Aggregate = xx
Name = Hampshire, Aggregate = xx
Name = South, Aggregate = xx
Name = Dakota, Aggregate = xx

Could anyone please help me with the right configuration to change this?

This is my current configuration:

      <index id="site_search_web_index" type="Sitecore.ContentSearch.SolrProvider.SolrSearchIndex, Sitecore.ContentSearch.SolrProvider">
        <param desc="name">$(id)</param>
        <param desc="core">site_search_web</param>
        <param desc="propertyStore" ref="contentSearch/databasePropertyStore" param1="$(id)" />
        <strategies hint="list:AddStrategy">
          <strategy ref="contentSearch/indexUpdateStrategies/onPublishEndAsync" />
        </strategies>

        <locations hint="list:AddCrawler">
          <crawler type="Sitecore.ContentSearch.SitecoreItemCrawler, Sitecore.ContentSearch">
            <Database>web</Database>
            <Root>/sitecore/content/Home</Root>
          </crawler>
        </locations>
      </index>
3
  • You should be able to change the type of the field to be string instead of using a text type with a tokenizer. Tokenizers break the text on token boundaries like space, hyphen, etc., which is why you are seeing the individual tokens in the facets.
    – arun
    Jan 23, 2015 at 19:55
  • Would it be possible to do this on an index level instead of field level? I would really appreciate sample config in that case! Thanks! Jan 24, 2015 at 1:04
  • I believe you're locked to the field level, but you can always use a computed field or declare an alias to that field with a different datatype and use that instead for any special cases. Jan 28, 2015 at 18:06

1 Answer 1

4

You can achieve this by one of the following solutions:

Solution 1

You can create a computed field that returns the facet value and set the computed field type to "string" to avoid tokenization. Your computed field should look like:

public class TitleComputedField : IComputedIndexField
{
    public object ComputeFieldValue(IIndexable indexable)
    {
        if (indexable == null) throw new ArgumentNullException("indexable");
        var scIndexable = indexable as SitecoreIndexableItem;

        if (scIndexable == null)
        {
            Log.Warn(
                this + " : unsupported IIndexable type : " + indexable.GetType(), this);
            return false;
        }

        var item = (Item)scIndexable;
        if (item == null)
        {
            Log.Warn(
                this + " : unsupported SitecoreIndexableItem type : " + scIndexable.GetType(), this);
            return false;
        }

        if (String.Compare(item.Database.Name, "core", StringComparison.OrdinalIgnoreCase) == 0)
        {
            return false;
        }

        return = item.Fields["Title"];
    }

    public string FieldName { get; set; }
    public string ReturnType { get; set; }
}

And configure the computed field in Sitecore.ContentSearch.Solr.Indexes.config as follows:

      <fields hint="raw:AddComputedIndexField">
        ...
        <field fieldName="plaintitle"             returnType="string">YourNamespace.TitleComputedField, YourAssembly</field>
      </fields>

And finally if you facet on the "plaintitle" field, you should get the expected results.

Solution 2

You can create the field on the index level by updating the solr schema.xml as follows:

Create a new field in solr of type string

<fields>
   ...
   <field name="plaintitle" type="string" indexed="true" stored="true" />
</fields>

and then create a "copyfield" to copy the original field into the new one

<copyField source="title_t" dest="plaintitle" />

in both solutions you can facet on the new field using the following code:

query.FacetOn(i => i["plaintitle"]);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.