Filtering items in search results
You can add a filter object to a Search and Recommendation request for Sitecore Search to limit the items in the search results. To do this, add a widget.items.search.filter object.
Before you can pass filters in an API call, make sure that an administrator configures attributes for filtering in Search.
For the data model of this object, see the Search and Recommendation API reference.
The following are some examples that show you how to create requests for filters at the request level.
Equals filter
A simple way of filtering is to use the eq filter type.
For example, you want to show only blog content. The following code sample shows how to create a request:
{
"widget": {
"items": [
{
"rfk_id": "rfkid_7",
"entity": "content",
"search": {
"content": {},
"filter": {
"name": "type",
"type": "eq",
"value": "blogs"
}
}
}
]
},
"context": {
"locale": {
"country": "us",
"language": "en"
}
}
}The following is the response to the above request:
{
"widgets": [
{
"rfk_id": "rfkid_7",
"entity": "content",
"content": [
{
"description": "Last week, our...",
"id": "https___www_sitecore_com_blog_cloud_what-is-cloud-native-saas",
"image_url": "https://wwwsitecorecom.azureedge.net/-/media/sitecoresite/images/home/blog/commerce/bp/26gettyimage159993825_herobig_272.jpg?mw=600&md=20210629T181219Z&hash=37F6BE28053A4ABE6B681D50313E44EB&t=544x310",
"source_id": "388218",
"subtitle": null,
"title": "What is cloud-native SaaS?",
"type": "blogs",
"url": "https://www.sitecore.com/blog/cloud/what-is-cloud-native-saas"
},
{
"description": "What is serverless...",
"id": "https___www_sitecore_com_blog_cloud_what-is-serverless-architecture",
"image_url": "https://wwwsitecorecom.azureedge.net/-/media/sitecoresite/images/home/blog/commerce/bp/28gettyimage686721451_herobig_272.jpg?mw=600&md=20210629T182737Z&hash=C6AFD2B27D694091D473C4694E694BA2&t=544x310",
"source_id": "388218",
"subtitle": null,
"title": "What is serverless architecture?",
"type": "blogs",
"url": "https://www.sitecore.com/blog/cloud/what-is-serverless-architecture"
},
...
],
"total_item": 220,
"limit": 10,
"offset": 0
}
],
"dt": 39,
"ts": 1669044489693
}Or filter
Use the or filter when you want any of two or more content or facet types. The or filter is a composite filter, meaning you must nest other filters within it.
For example, you want to show either blog or product content. Use an or filter and nest two eq filters within it.
The following code sample shows how to create a request:
{
"widget": {
"items": [
{
"rfk_id": "rfkid_7",
"entity": "content",
"search": {
"content": {},
"filter": {
"type": "or",
"filters": [
{
"name": "type",
"type": "eq",
"value": "blogs"
},
{
"name": "type",
"type": "eq",
"value": "products"
}
]
}
}
}
]
},
"context": {
"locale": {
"country": "us",
"language": "en"
}
}
}The following code shows the corresponding response:
{
"widgets": [
{
"rfk_id": "rfkid_7",
"entity": "content",
"content": [
{
"description": "Last week, our...",
"id": "https___www_sitecore_com_blog_cloud_what-is-cloud-native-saas",
"image_url": "https://wwwsitecorecom.azureedge.net/-/media/sitecoresite/images/home/blog/commerce/bp/26gettyimage159993825_herobig_272.jpg?mw=600&md=20210629T181219Z&hash=37F6BE28053A4ABE6B681D50313E44EB&t=544x310",
"source_id": "388218",
"subtitle": null,
"title": "What is cloud-native SaaS?",
"type": "blogs",
"url": "https://www.sitecore.com/blog/cloud/what-is-cloud-native-saas"
},
{
"description": "What is serverless...",
"id": "https___www_sitecore_com_blog_cloud_what-is-serverless-architecture",
"image_url": "https://wwwsitecorecom.azureedge.net/-/media/sitecoresite/images/home/blog/commerce/bp/28gettyimage686721451_herobig_272.jpg?mw=600&md=20210629T182737Z&hash=C6AFD2B27D694091D473C4694E694BA2&t=544x310",
"source_id": "388218",
"subtitle": null,
"title": "What is serverless architecture?",
"type": "blogs",
"url": "https://www.sitecore.com/blog/cloud/what-is-serverless-architecture"
},
...
],
"total_item": 410,
"limit": 10,
"offset": 0
}
],
"dt": 21,
"ts": 1669044585539
}Geographic filters
Use geographic filters to define an area within which you want Search to look for and return results. To do this, use the geoDistance or geoWithin filter types.
Filtering within a circular area
To only show results that are relevant to a defined circular area, use the geoDistance value for the filter object. You specify the center of the circle using the latitude and longitude parameters, and the radius of the circle using the distance parameter.
If you do not specify a latitude and longitude inside the filter object, Search uses the following mechanism to get these values:
-
First, Search looks in the
geocontext object for the latitude and longitude values. -
Next, if there is no
geocontext object, Search infers the approximate latitude and longitude from the IP address of the visitor's browser or native app.
For example, you have a parks and recreation website, and your visitors have the option to select a show results near me option on your search page. You want to show results that are relevant for a 50 kilometer radius from a visitor who is at 59.11309610169502 degrees South and 37.329354589260596 degrees West. To do this, use the geoDistance filter and pass the coordinates of the visitor as the lat and lon values.
The following code sample shows how to create a request:
{
"widget": {
"items": [
{
"rfk_id": "rfkid_10",
"entity": "product",
"search": {
"content": {},
"filter": {
"type": "geoDistance",
"name": "location",
"distance": "50km",
"lat": -59.11309610169502,
"lon": -37.329354589260596
}
}
}
]
}
}Filtering within a polygonal area
To only show results that are relevant to a defined polygonal area, use the geoWithin value for the filter object. You specify the vertexes of the polygon using the latitude and longitude parameters. Define at least three vertexes for this filter to work.
For example, you have a website that shows local events, and you want to show events that are relevant to visitors in a four-sided area. To do this, create a geoWithin filter request and pass and array with the coordinates of the four vertexes of the area.
The following code sample shows how to create a request:
"widget":{
"items": [
{
"rfk_id": "rfkid_10",
"entity": "product",
"search": {
"content": {},
"filter": {
"type": "geoWithin",
"name": "location",
"coordinates":[
{
"lat": 43.194852,
"lon": -79.570522
},
{
"lat": 43.176515,
"lon": -79.929493
},
{
"lat": 43.521512,
"lon": -79.912504
},
{
"lat": 43.871014,
"lon": -78.780655
}
]
}
}
}
]
}Complex filters
You can use more than one operator to create a complex filter.
For example, you want to see items that cost more than 5000 but are not of the brand bigbrand. To do this, use a not filter and a greater-than (gt) filter.
The following code sample shows how to create a request:
{
"widget": {
"items": [
{
"rfk_id": "rfkid_7",
"entity": "product",
"search": {
"content": {},
"filter": {
"type": "and",
"filters": [
{
"type": "not",
"filter": {
"name": "brand",
"type": "eq",
"value": "bigbrand"
}
},
{
"name": "price",
"type": "gt",
"value": 5000
}
]
}
}
}
]
},
"context": {
"locale": {
"country": "us",
"language": "en"
}
}
}The following code shows the corresponding response:
"widgets": [
{
"rfk_id": "rfkid_7",
"entity": "product",
"content": [
{
"brand": "S&S Cycle",
"id": "1400066",
"name": "Wrinkle Black Powder-Coat 124 in. Hot Set-up Kit - 900-0564",
"price": 5948.96,
"sku": "1400066",
},
{
"brand": "S&S Cycle",
"id": "1400067",
"name": "Wrinkle Black Powder-Coat 124 in. Hot Set-up Kit - 900-0568",
"price": 5804.96,
"sku": "1400067",
},
...
],
"total_item": 52,
"limit": 10,
"offset": 0
}
],
"dt": 125,
"ts": 1669045174268
}