Sitecore SXA search components
Sitecore eXperience Accelerator comes with a bunch of components to create a search solution for your site. A few years ago -with SXA 1.1- I wrote a post on (some) of the search components and how to use them to create a site search.
It wouldn't be Sitecore nor SXA when you could not tweak this a lot.. you can create specific searches, facetted results, and lots more.. Just check out the official docs.
When I found out that we can trigger the search results without user interaction I saw new possibilities. We can create overview pages with the search components! This is no rocket science but probably worth sharing for people who are not so acquainted with SXA yet.
Create an overview page with SXA search components
Let's explain our setting: we have a number of products and want to create a page where the visitor can easily find the product (s) needs. We'll show the products and let them be filtered based on a few facets in order to get the visitor quickly to the wanted products with a link to the details.
So we have items of a template "Product" that are located in folders underneath our "product root node".
Search scope
First thing to do is define our search scope. A search scope is used to limit the search results based on a query. Go to /Settings/Scopes in your sites folder to define a scope. In our case we created a Product Scope with a query on location (our product root node) and template (our product template). Use the "Build Query" dialog to create the query you need (the dialog performs the actual query so can preview the results).
Search results
We can already create the search results to see if our listing is working. Select a page (or partial design) where you want the overview. Go to the experience editor and from the SXA toolbox drop the Search Results component on the page.
The datasource of the search component is not really important here. This item contains the "Results not found" text, but we should always have results.
What is important though are the properties of the control in the SearchCriteria section.
Make sure to:
- select the scope (your created scope should be available here)
- select "Automatically fire search when no criteria set" - this will make sure that your search is done automatically on the page
Variant
You'll probably want to create a variant for your search results. Create a new variant definition under /Presentation/Rendering Variants/Search Results in your site and select the variant on your search results component. In this variant you can define which fields (from the Product template) should be shown. Don't forget to set "Is Link" on at least one (the title) to have a link to the detail page.
More information on creating a variant on the official doc site.
More...
You can extend your page with more search components as needed. You'll probably want the "Page Selector" and "Page Size" components, maybe also the "Sort Results".. More info on the available components for search can be found on the official doc site.
Facets
Next step is to add our facets to have filters on the results. Our template contains a few fields that we want to filter on. In our case those fields were of type DropList. This means that the raw value of the field is actually just a Guid. Of course, you don't want to display guids to your visitors.. so we had to add extra fields to our index to do this.
Facet index field
It's been asked on StackExchange, but the information can be found on the official doc site. And yes, it's as simple as adding a few definitions for computed fields (not even code, as the code has been provided by SXA).
To be able to create the facet to a field that has a guid, add the following in a config patch file (check the config file for your search provider for the exact location where to patch this):
<field contentfield="title" fieldname="referenced" referencefield="link" type="Sitecore.XA.Foundation.Search.ComputedFields.ResolvedLinks, Sitecore.XA.Foundation.Search"></field>
where:
- fieldName: defines the index field name
- referenceField: name of the field in your template
- contentField: name of the field to be fetched from the referenced item
Don't forget to rebuild your index after these changes...
Once we have the field in our index, we can create the facet in SXA.
SXA Facet
We will use facets to refine the search results. Facets in SXA are added to /Settings/Facets in your site.
There are a few facet types (bool, date, integer, list ...) - we used list types in our case. It might be a bit tricky to get the "Field Name" field correctly as this has to be like in your index. Easiest way to be sure is to check your index (e.g. use the Solr admin interface) to find the exact field name in the index. In my case it was "fieldName_sm" (where fieldName is the one from our patch above).
Adding the facets to the page
Last thing we need to do is add the facet components to our page, using the created facet settings.
Go back to the page (item) where you added the search results and open it again in the experience editor.
For each facet you have, add the Filter component you want. I used Dropdown and Checklist, but this will depend on what you actually want to filter on and how you want to present it to the visitor.
When the component is dropped on the page, create a new datasource for it. In this datasource item, you can alter a few texts used on the component but the most important one is the Facet field. Select the appropriate facet here.
The results
When all this is done and published, you should have a nice overview page that has filters, paging, .. and everything you need to get your visitors to your detail pages. Good luck!
Extra
You will notice that filtering the results creates a new url each time. This means we could immediatly link to a filtered version of our page if we would want to...
ps: we are using SXA 1.7.1 at the time of writing
great post; thanks
ReplyDeleteHi, I Implement all the steps but i can't find the new field in solr index
ReplyDelete1- Sitecore.XA.Foundation.Search.ComputedFields.ResolvedLinks, Sitecore.XA.Foundation.Search
2-
I added my configration in
Also i take care ab out reindex and reset the solr services
You might want to take questions like this to Sitecore StackExchange or Slack. Blog comments are not so handy to debug issues.
DeleteHi, Is it possible to make the searchbox search results specific to the fields? Means the search should happen for the specific fields only.
ReplyDeleteHi, the computed index field(a multi list field) is not searchable in the site via sxa searchbox. Please help.
ReplyDeleteFor support, please use Sitecore Slack or Sitecore StackExchange (https://sitecore.stackexchange.com).
Delete