4

I have create a new facet for a categories field for a bucketable item. This field is a treelistex which means the user can select multiple categories for an item. Each category is an item with a single field: Title.

When I'm running the search the facets are showing but the Category facet shows the guids instead of the title field for example: enter image description here

Here is how the facet is defined:

enter code here

any thoughts on what needs to be changed to start showing the facet title as opposed to it's guid?

6
  • 1
    I think the easiest way is going to be to create a computed index field that looks up the title and add that to your index. Some more info here: community.sitecore.net/technical_blogs/b/sitecorejohn_blog/… Commented Jul 21, 2017 at 18:23
  • 2
    can you check this setting BucketConfiguration.ResolveFacetValueToFriendlyName if is true on Sitecore.Buckets.config Commented Jul 21, 2017 at 19:17
  • that's the piece that was missing @SitecoreClimber. I set to true and it started showing by the name.
    – Diego
    Commented Jul 21, 2017 at 19:45
  • @SitecoreClimber you shoud add that as an answer
    – Richard Seal
    Commented Jul 21, 2017 at 20:52
  • Does BucketConfiguration.ResolveFacetValueToFriendlyName really exist @SitecoreClimber. I have been writing the code below forever. Learn something new every day.
    – Chris Auer
    Commented Jul 21, 2017 at 23:22

2 Answers 2

7

You need to set BucketConfiguration.ResolveFacetValueToFriendlyName equals to true.

 <!-- RESOLVE FACET VALUE TO FRIENDLY NAME
       If you are storing a field in the index that is being faceted on, it may be stored as an ID. This Setting
       when set to true, 
       will try and resolve this to the friendly item name instead. 
       USAGE: In an environment with huge amounts of items (e.g. 1 Million), this will not scale properly.           
  -->
  <setting name="BucketConfiguration.ResolveFacetValueToFriendlyName" value="false"/>
1

The field is a GUID because that is what is in the Sitecore value field, for the category field. I imagine its a droplist or a multilist. So what you need to do is create a new ComputedIndexField to turn that GUID into a name.

I am going to break the code up into Multilist field and a droplist field. They are a little different.

Code For Multilist Field

In the code below, I am taking the MultiList field and getting the selected items, find the Display Names of the items, adding them a list of strings that is my return. I return the list of strings to the indexer, which with then show me names in my facets.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Sitecore.Feature.Article.Search.ComputedFields
{
    using Sitecore.ContentSearch;
    using Sitecore.ContentSearch.ComputedFields;
    using Sitecore.ContentSearch.Utilities;
    using Sitecore.Data;
    using Sitecore.Data.Fields;
    using Sitecore.Foundation.SitecoreExtensions.Extensions;

    public class TagNameComputedField : IComputedIndexField
    {
        public object ComputeFieldValue(IIndexable indexable)
        {
            var item = (SitecoreIndexableItem)indexable;

            if (item == null)
            {
                return null;
            }

            if (!item.Item.IsDerived(Template.Article.ID)) return null;

            var db = item.Item.Database;
            MultilistField tags = item.Item.Fields[Template.Article.Fields.Tags];

            if (tags == null || tags.TargetIDs.Length == 0) return null;

            var list = new List<string>();
            this.GetTags(tags.TargetIDs, list, db);

            return list;
        }

        public void GetTags(ID[] tags, List<string> list, Database db)
        {
            tags.ForEach(tag => list.Add(db.GetItem(tag)?.DisplayName));
        }


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

The Config For Multilist Field

This is where we tie it together. I am adding my new tag_name index field to my default Lucene indexes. The most important this here is the use os UNTOKENIZED in the indexType. This makes Lucene save the whole name to the index in one piece. Not breaking up into terms. So My brown dog goes into the index as a whole and not three different words.

<?xml version="1.0" encoding="utf-8"?>

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/">
    <sitecore>
        <contentSearch>
            <indexConfigurations>
                <defaultLuceneIndexConfiguration type="Sitecore.ContentSearch.LuceneProvider.LuceneIndexConfiguration, Sitecore.ContentSearch.LuceneProvider">
                    <fieldMap type="Sitecore.ContentSearch.FieldMap, Sitecore.ContentSearch">
                        <fieldNames hint="raw:AddFieldByFieldName">
                            <field fieldName="tag_name" storageType="YES" indexType="UNTOKENIZED" vectorType="NO" boost="1f" type="System.Collections.Generic.List`1[[System.String, mscorlib]]" settingType="Sitecore.ContentSearch.LuceneProvider.LuceneSearchFieldConfiguration, Sitecore.ContentSearch.LuceneProvider">
                                <Analyzer type="Sitecore.ContentSearch.LuceneProvider.Analyzers.LowerCaseKeywordAnalyzer, Sitecore.ContentSearch.LuceneProvider" />
                            </field>
                        </fieldNames>
                    </fieldMap>
                    <documentOptions type="Sitecore.ContentSearch.LuceneProvider.LuceneDocumentBuilderOptions, Sitecore.ContentSearch.LuceneProvider">
                        <fields hint="raw:AddComputedIndexField">
                            <field fieldName="tag_name" storageType="no" indexType="untokenized">Sitecore.Feature.Article.Search.ComputedFields.TagNameComputedField, Sitecore.Feature.Article</field>
                        </fields>
                    </documentOptions>
                </defaultLuceneIndexConfiguration>
            </indexConfigurations>
        </contentSearch>
    </sitecore>
</configuration>

Sitecore facet

Now create your facet again, but use the index field name of tag_name. It will show you tags with real names and not GUIDs.

enter image description here

For a droplist field, we don't want a list of strings. We just need a single string of the name. So here is the code and config for a single item in a droplist.

The Code For Droplist Field

Here we just want to return a single string to the index.

namespace Sitecore.Feature.Article.Search.ComputedFields
{
    using Sitecore.ContentSearch;
    using Sitecore.ContentSearch.ComputedFields;
    using Sitecore.Data;
    using Sitecore.Data.Fields;
    using Sitecore.Diagnostics;
    using Sitecore.Foundation.SitecoreExtensions.Extensions;

    public class AuthorNameComputedField : IComputedIndexField
    {
        public object ComputeFieldValue(IIndexable indexable)
        {
            var item = (SitecoreIndexableItem)indexable;

            if (item == null)
            {
                return null;
            }

            if (!item.Item.IsDerived(Template.Article.ID)) return null;

            var db = item.Item.Database;
            var itemId = item.Item.Fields[Template.Article.Fields.Author].Value;
            if (itemId == null || !ID.IsID(itemId)) return null;

            var author = db.GetItem(new ID(itemId));

            Log.Info($"Saving the index author value {author.DisplayName}", "indexing");

            return author.DisplayName;
        }


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

The Config For Droplist Field

<?xml version="1.0" encoding="utf-8"?>

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/">
    <sitecore>
        <contentSearch>
            <indexConfigurations>
                <defaultLuceneIndexConfiguration type="Sitecore.ContentSearch.LuceneProvider.LuceneIndexConfiguration, Sitecore.ContentSearch.LuceneProvider">
                    <fieldMap type="Sitecore.ContentSearch.FieldMap, Sitecore.ContentSearch">
                        <fieldNames hint="raw:AddFieldByFieldName">
                            <field fieldName="author_name" storageType="YES" indexType="UNTOKENIZED" vectorType="NO" type="System.String" settingType="Sitecore.ContentSearch.LuceneProvider.LuceneSearchFieldConfiguration, Sitecore.ContentSearch.LuceneProvider" />
                        </fieldNames>
                    </fieldMap>
                    <documentOptions type="Sitecore.ContentSearch.LuceneProvider.LuceneDocumentBuilderOptions, Sitecore.ContentSearch.LuceneProvider">
                        <fields hint="raw:AddComputedIndexField">
                            <field fieldName="author_name" storageType="no" indexType="untokenized">Sitecore.Feature.Article.Search.ComputedFields.AuthorNameComputedField, Sitecore.Feature.Article</field>
                        </fields>
                    </documentOptions>
                </defaultLuceneIndexConfiguration>
            </indexConfigurations>
        </contentSearch>
    </sitecore>
</configuration>
0

Your Answer

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

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