1

I have added an SXA search box component on the page and now when I type a search term it returns the result based on the search term found through the aggregated content. But, I want that search box to search that word in a specific field only.

For example, I have a few pages of profile template with the following fields in it,

  1. First Name
  2. Last Name
  3. Email

For the search box, we have set up a scope to limit a few templates like profile template, insight template, etc.

Now when we try to find a person in the search box then according to scope it returns the result from scoped template's field content.

But, is there any way to modify the scope query or other solution to set up the search box to perform its search in the specific field (say First Name) only?

We are using Sitecore Experience Accelerator 10.2.0 rev. 04247 with Sitecore 10.2.

2 Answers 2

3

This can be achieved by overwriting the sxacontent field in Solr document.

  1. Create a class by inheriting AggregatedContent class and overriding ComputedFieldValue

  2. Inside override method, access ComputedFiedValue from base class which is nothing but sxacontent field value from Solr

  3. Write a condition for a specific item or items from the specific template and replace the existing sxacontent field value with the necessary field values only or add the extra field content to the existing sxacontent field value as per your requirement.

  4. Outside the condition, return the sxacontent field value grabbed from the base class as it is. So that it will not impact your changes to the other items content.

  5. Create a patch file and patch it instead of existing sxacontent computed field value,

    <field fieldName="sxacontent" patch:instead="*[@fieldName='sxacontent']" returnType="textCollection" type="your class, namespace"/>

deploy the code and patch file to webroot

  1. Rebuild the indexes

Refer to my blog on overwriting Sxacontent field for more details.

1
  • Thanks for posting this @Anjali, this answer will help definitely, and meanwhile, I found one more solution which seemed to fit my requirements so I will post that answer also so that in the future all combinations of answers will make a helful solution. Sep 20, 2022 at 6:16
1

I think you might be able to get somewhere using a custom SearchQueryToken. I wrote a blog post on this topic some years ago (including the code). In my case I had to specify a field that had to match to a value on the current item. In your case that field would have to match the entry from the searchbox but I assume you will have access to that value so you can create the necessary query.

Note that although the code in the post is tested and works, I haven't tested this in your scenario and access to the search term is an assumption but I believe it's definitely worth trying.

The code itself is a bit much to copy here, but in short you need to create a processor with a Process method that loops over all the SearchStringModel objects. This object is a piece of the query and you need to transform that model to a new one that can be used by the search engine.

Once you have created your custom querytoken you can use it to define the scope as it will appear in the query designer with the ootb sxa ones: enter image description here

3
  • Thanks for the blog @Gatogordo, I tried to follow the article and I can see that query is able to find the content in my custom field but in addition to this, it is still looking for the searched term in the sxacontent_txm due to which it is giving me the result where the aggregated content field has the search term available without having it in the custom field. So, I want to restrict that search should be only to my custom field only. Sep 11, 2022 at 11:55
  • Ok - haven't tried this at all but maybe the search in sxacontent is also present in the list of SearchStringModels.. if so, you would be able to remove it.
    – Gatogordo
    Sep 11, 2022 at 19:14
  • SearchStringModel is not giving this info so that I can remove it but if we can modify the query that gets created and then we can remove that sxacontent from the query used for getting the data from index then I guess it should work. Sep 12, 2022 at 4:54

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.