Search.../

facets

Returns the facet results retrieved by the search.

Description

The facet results provide grouping information for documents returned by the search, based on the requested facets.

Facet results are structured as follows:

Each facet specified in the facets() function returns a single facet result. Each facet result includes a facet group containing the name of the specified facet and an array of facets listing the number of documents matching each facet value.

The following example shows the facet results for a store product search for which collections and onSale were specified as facets:

"facets":
[
{
"facet": "collections",
"facets":
[
{
"facetValue": "Boots",
"count": 13
},
{
"facetValue": "Running Shoes",
"count": 22
},
{
"facetValue": "Sandals",
"count": 18
}
]
},
{
"facet": "onSale",
"facets":
[
{
"facetValue": "true",
"count": 17
},
{
"facetValue": "false",
"count": 36
}
]
}
]
javascript | Copy Code

When no facets are specified in the facets() function, the returned facets array is empty.

Type:

Array<FacetResult>Read Only
NAME
TYPE
DESCRIPTION
facets
Array<FacetGroup>

A facet group with facet information. Includes the name of the specified facet and the results for each facet value.

Was this helpful?

Create a search with facets, run the search, and get the facet results

Copy Code
1import wixSearch from 'wix-search';
2
3// ...
4
5wixSearch.search()
6 .documentType("Stores/Products")
7 .facets("collections", "inStock")
8 .find()
9 .then( (results) => {
10 const facets = results.facets;
11 })
12 .catch( (error) => {
13 console.log(error);
14 });
15
16 /* Example facets array
17 *
18 * [
19 * {
20 * "facet": "collections",
21 * "facets":
22 * [
23 * {
24 * "facetValue": "Winter",
25 * "count": 29
26 * },
27 * {
28 * "facetValue": "Spring",
29 * "count": 17
30 * },
31 * {
32 * "facetValue": "Summer",
33 * "count": 36
34 * }
35 * ]
36 * },
37 * {
38 * "facet": "inStock",
39 * "facets":
40 * [
41 * {
42 * "facetValue": "true",
43 * "count": 67
44 * },
45 * {
46 * "facetValue": "false",
47 * "count": 15
48 * }
49 * ]
50 * }
51 * ]
52 *
53 */
Filter search results according to a selected facet

This example demonstrates how to display facet information for store products and let site visitors filter the products by selecting a facet. We use 2 repeaters, 1 to display the facet info and 1 for the products. We apply the collections field as a facet to our search for store products. Each facet value and number is loaded into the facet repeater. When a site visitor selects a facet, the products are filtered according to the selected collection.

Copy Code
1import wixSearch from 'wix-search';
2
3$w.onReady(function () {
4
5 // Define what happens when the facet repeater's data is set
6 $w("#facetRepeater").onItemReady(($item, itemData) => {
7 $item("#facetText").text = itemData.facetValue;
8 $item("#facetNumberText").text = "(" + itemData.count + ")";
9
10 // When a facet is selected, run the function
11 // that displays only the selected products
12 $item("facetText").onClick(event => {
13 const facet = $item("#facetText").text;
14 displaySelectedProducts(facet);
15 });
16 });
17
18 // Define what happens when the product repeater's data is set
19 $w("productRepeater").onItemReady(($item, itemData) => {
20 $item("#productTitleText").text = itemData.title;
21 $item("#productDescriptionText").text = itemData.description;
22 $item("#productImage").src = itemData.image;
23 });
24
25 // Run a search which applies a facet that categorizes store
26 // products according to the collection they belong to
27 wixSearch.search()
28 .documentType("Stores/Products")
29 .facets("collections")
30 .find()
31 .then((results) => {
32
33 // Get the first (and only) facet result
34 const facets = results.facets[0].facets;
35
36 // Add an ID to each object in the facets
37 // array (required for repeater data)
38 const newFacets = facets.map((facet) => {
39 facet._id = facet.facetValue;
40 return facet;
41 });
42
43 // Set the facet repeater's data
44 $w('#facetRepeater').data = newFacets;
45 // Set the initial pre-filtered product repeater's data
46 $w('#productRepeater').data = results.documents;
47 });
48});
49
50// Display only products from the collection
51// corresponding to the selected facet
52export function displaySelectedProducts(facet) {
53 wixSearch.search()
54 .documentType("Stores/Products")
55 .hasSome("collections", [facet])
56 .find()
57 .then((results) => {
58 $w('#productRepeater').data = results.documents;
59 });
60}