Overwriting sxa content field in solr document

 Heythere! Welcome to "Sitecore Series"

Post is all about sharing the details about "Sitecore SXA search functionality and how to overwrite sxa content field".

Sxa search is not returning results even some fields contains the searched text? want to know why it is not searchable and how to fix this? 

Trying to generate a search query by passing an extra text like user category/content category and expecting a result with only the matched results where the item in droplist is selected with the same category? but not able to get the expected result?

Then this post is for you:


Sxa Search Functionality:

When Sxa search is configured, if user searched any text in the searchbox it returns the matched result but have you observed this? content that is searchable is actually limits to only few fields content. 

By Default the search is limits to single line, multi line and sxa tag fields content. 

Sitecore Creates a computed field called "sxacontent_txm" and stores the data of these fields. When user searched a keyword, it tries to match the content available in "sxacontent_txm" field and returns the results accordingly.

Want to check it , what value is populated in sxacontent field? refer below steps:

Open solr portal -> select the index -> access one particular item document by running: _group: itemid in the query -> search for sxacontent field and observe the value.

Note: In the query _group: itemid  -> while updating itemid, remove the braces, hyphens (-) and convert uppercase letters to lowercase .

In few scenarios, we may want to extend it or to limit the searchable content. To achieve this we have to override sxacontent computed field, refer below for steps:


Overwriting sxacontent field:


public class Test: AggregatedContent
{
public override object ComputedFieldValue(IIndexable indexable)
{
Item obj = (Item)(indexable as SitecoreIndexableItem);
var sxaContentValue = base.ComputedFieldValue(indexable);
if(obj == null)
return (object)null;

//executes the below piece of code only for the items from specified templates or items
// for the other items return the sxacontentvalue as it is retrieved from base class.
if( obj ! = null && obj.TemplateID == ID.Parse("{template-id -here}"))
{
var newValue= "Test Test"; 
//Add logic as per your requirement and get the new value
//Ex: Grab a value from the selected item from a multilist field

if(!string.IsNullOrEmpty(newValue))
{
var baseResult = sxaContentValue as IEnumerable<object>;
var result = baseResult.ToList();
result.add((object)newValue); //appends new value to the existing value
//update the result value as per your requirement

sxaContentValue = result.GroupBy(y => y).Select(x => x.First()).Distinct().ToList();
return (object)sxaContentValue;
}
}
return sxaContentValue;
}
}


Patch File:

Create a patch file to overwrite sxacontent field value.


<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"
xmlns:search="http://www.sitecore.net/xmlconfig/search/">
<sitecore>
<contentSearch>
<indexconfigurations>
<defaultSolrIndexConfiguration search:require="solr">
<documentOptions>
<fields hint="raw:AddComputedIndexField">
<field fieldName="sxacontent" patch:instead="*[@fieldName='sxacontent']" returnType="textCollection" type="your class, namespace">
	<mediaIndexing ref="contentSearch/indexConfigurations/defaultSolrIndexConfiguration/mediaIndexing" />
<field>
</fields>
</documentOptions>
</defaultSolrIndexConfiguration>
</indexconfigurations>
</contentSearch>
</sitecore>
</configuration>

Deploy the code and patch file to webroot.

Once the changes are deployed, rebuild the indexes and verify the sxa content field and observe the sxacontent field in solr , contains the modified value.


Hope this post helps you. Happy Sitecoring... 😊

Comments

Popular posts from this blog

Displaying Custom 404 error page for urls with unallowed file extension in sitecore

How to specify custom richtext menu as default richtext field setting when no source is specified for richtext field in sitecore

How to rebuild custom solr index programmatically in Sitecore

Sitecore - Task Schedulers and how to schedule a task

Handling URL requests with different file extensions in Sitecore

Accessing Items from Sitecore Database in different scenarios