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:
javascript | Copy Code"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}]}]
When no facets are specified in the facets()
function,
the returned facets
array is empty.
Type:
NAME
TYPE
DESCRIPTION
A facet group with facet information. Includes the name of the specified facet and the results for each facet value.
Was this helpful?
1import wixSearch from 'wix-search';23// ...45wixSearch.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 });1516 /* Example facets array17 *18 * [19 * {20 * "facet": "collections",21 * "facets":22 * [23 * {24 * "facetValue": "Winter",25 * "count": 2926 * },27 * {28 * "facetValue": "Spring",29 * "count": 1730 * },31 * {32 * "facetValue": "Summer",33 * "count": 3634 * }35 * ]36 * },37 * {38 * "facet": "inStock",39 * "facets":40 * [41 * {42 * "facetValue": "true",43 * "count": 6744 * },45 * {46 * "facetValue": "false",47 * "count": 1548 * }49 * ]50 * }51 * ]52 *53 */
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.
1import wixSearch from 'wix-search';23$w.onReady(function () {45 // Define what happens when the facet repeater's data is set6 $w("#facetRepeater").onItemReady(($item, itemData) => {7 $item("#facetText").text = itemData.facetValue;8 $item("#facetNumberText").text = "(" + itemData.count + ")";910 // When a facet is selected, run the function11 // that displays only the selected products12 $item("facetText").onClick(event => {13 const facet = $item("#facetText").text;14 displaySelectedProducts(facet);15 });16 });1718 // Define what happens when the product repeater's data is set19 $w("productRepeater").onItemReady(($item, itemData) => {20 $item("#productTitleText").text = itemData.title;21 $item("#productDescriptionText").text = itemData.description;22 $item("#productImage").src = itemData.image;23 });2425 // Run a search which applies a facet that categorizes store26 // products according to the collection they belong to27 wixSearch.search()28 .documentType("Stores/Products")29 .facets("collections")30 .find()31 .then((results) => {3233 // Get the first (and only) facet result34 const facets = results.facets[0].facets;3536 // Add an ID to each object in the facets37 // array (required for repeater data)38 const newFacets = facets.map((facet) => {39 facet._id = facet.facetValue;40 return facet;41 });4243 // Set the facet repeater's data44 $w('#facetRepeater').data = newFacets;45 // Set the initial pre-filtered product repeater's data46 $w('#productRepeater').data = results.documents;47 });48});4950// Display only products from the collection51// corresponding to the selected facet52export 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}