As we have shown in our blog, attributes can be set with JavaScript during crawling. In this article, we will show you how to actually check the facets in action and increase the number of attributes.
![](/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2Fzi1ovlormku8%2F2u8L11Ka40D0iuTkdIUTuJ%2F0a4039d7a024c58f3c98efae259180bb%2Fvladimir-mokry-G-4wX5tZNuE-unsplash.jpg&w=3840&q=75)
Check facets
First, access the page you are creating with the Sitecore Search SDK using the keyword "Headless". Then, you will see the following screen.
![searchfacet01.png](/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2Fzi1ovlormku8%2F2BTCRFDwiBBGvSei7cf5jE%2Ff35d27ed2235694c24692fa2cf39c80e%2Fsearchfacet01.png&w=3840&q=75)
The type of content is automatically displayed on the left side of the search results in the form of a Type. When you access the corresponding content from the administration page, you will see that the items set in Content Type are used in this facet.
![searchfacet02.png](/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2Fzi1ovlormku8%2Fe059udc85R4JOftT0mRX4%2F3ddff3ff43c334e2af74f389283a5cf5%2Fsearchfacet02.png&w=3840&q=75)
This item is handled as part of the JavaScript code set up in Document Extractor.
// URL からコンテンツタイプを指定する
let subtype;
if (url.includes('/products/')) {
subtype = 'Products';
} else if (url.includes('/solutions/')) {
subtype = 'Solutions';
} else if (url.includes('/knowledge-center/')) {
subtype = 'Knowledge Center';
} else if (url.includes('/partners/')) {
subtype = 'Partners';
} else if (url.includes('/company/')) {
subtype = 'Company';
} else {
subtype = 'website';
}
The facets are configured in Facets under Feature Configuration in Domain Settings.
![searchfacet03.png](/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2Fzi1ovlormku8%2FAsWi6HGY332psRy4h07RV%2F218bf0c1fab06e570ce2581c725faff7%2Fsearchfacet03.png&w=3840&q=75)
Now let's increase the number of Facets used in the search results.
Adding Attributes
This time, we will add two items. The first is to create a Product that will add attributes about the product.
Column | Value |
---|---|
Display Name | Product |
Attribute Name | product |
Placement | Standard |
Data Type | String |
Properties | Return in api response - Check |
![searchfacet04.png](/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2Fzi1ovlormku8%2F0RPJVUUXCZKp5jZSvbwf3%2Fba3e7acb08e4f5b0524237f3aeeabca8%2Fsearchfacet04.png&w=3840&q=75)
On the Use For Feature tab, check Facets and Personalize.
![searchfacet05.png](/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2Fzi1ovlormku8%2F4el1DDZnIOa4hLWwOBW9sH%2Fbe21a19673ef4e5be8a64750f172f32b%2Fsearchfacet05.png&w=3840&q=75)
The Attributes screen with the two additional items is as follows The Attributes screen with the two new items is as follows.
![searchfacet06.png](/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2Fzi1ovlormku8%2F2jsGvwCibkWq8HdAp56ZeH%2Fd48568c0d7846a8cb12deafe18bd99d2%2Fsearchfacet06.png&w=3840&q=75)
If you check the Facets screen in Domain Settings, you will see that Site Name and Product have been added.
![searchfacet07.png](/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2Fzi1ovlormku8%2F35SFj0zVck2WWvFMRYXKtH%2Fbda15ee1bbfde713fb3932b15652ecb4%2Fsearchfacet07.png&w=3840&q=75)
This completes the addition of Attributes; click Publish to update Domain Settings and activate the attributes.
Crawler Changes
In the already configured source settings for Sitecore.com, change the Document Extractors to use the new attributes. In this case, change the JavaScript Return as follows
return [{
'id': id,
'title': $('h1').text(),
'subtitle': $('meta[name="description"]').attr('content') || $('section[data-component-name="Hero Banner"] div[class*="side-content"]>div>p, header div.lead p').text(),
'description': $('meta[name="description"]').attr('content') || $('meta[property="og:description"]').attr('content') || $('p').text(),
'image_url': $('section[class*="component hero"] picture>img').attr('src') || $('meta[name="image"]').attr('content'),
'last_modified': $('meta[name="last_modified"]').attr('content'),
'name': $('meta[name="searchtitle"]').attr('content') || $('title').text(),
'type': subtype,
'product': $('meta[name="product"]').attr('content'),
'site_name': 'Sitecore.com',
'url': $('meta[property="og:url"]').attr('content')
}];
We have also made some changes to the title loading and images. The attributes we have added this time are product and site_name, but since the crawler is working with sitecore.com, we have set fixed values here.
When using Xpath in the above data setup, the following description is used to set fixed values.
![searchfacet08.png](/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2Fzi1ovlormku8%2F7Jd3S7HdW8rOds8faz9QLn%2F77e4386c3cb59defeaebca1585e6826e%2Fsearchfacet08.png&w=2048&q=75)
Please add an attribute and do a new crawl.
Content Verification
After the actual crawl is complete, you will be taken to the Search page. If you do a search using the keyword, you will see the facet added as follows.
![searchfacet09.png](/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2Fzi1ovlormku8%2F4OpZWKZdsGBEs5WgjqirdS%2F183920afb8e8b449c15caaa8ac719978%2Fsearchfacet09.png&w=3840&q=75)
Summary
In this case, we used attributes to set facets; if there is an item for which data can be set using Xpath, it can easily be set. It is also possible to use JavaScript to specify the configuration items and add values to the attributes. By designing well, such as which data can be used as facets, you can create pages that are easy to search.