Search.../

Introduction

The WixSearchFilterBuilder functions enable you to create filters which can be used to refine search results. Specifically, you need WixSearchFilterBuilders to create compound filters for filtering a search with the and(), or(), and not() functions.

You do not need WixSearchFilterBuilders for applying most filtering functions to a search. Instead you can chain WixSearchBuilder filtering functions directly to your search.

The following describes a typical filter builder flow:

  1. Create a WixSearchFilterBuilder using the filter() function.
  2. Create a WixSearchFilter by applying a single WixSearchFilterBuilder filter function, such as eq() or gt(), to the filter builder. You cannot chain more than 1 filtering function to a WixSearchFilterBuilder.
  3. You can use WixSearchFilters in one of 2 scenarios:
  • Create more filters: Pass one or more WixSearchFilters as parameters to an and(), or(), or not() WixSearchFilterBuilder function to create a new WixSearchFilter. For example, newFilter = wixSearch.filter().or(filter1, filter2).
  • Refine a search: Pass one or more WixSearchFilters as parameters to an and(), or(), or not() WixSearchBuilder function to refine a search. For example, newSearchBuilder = wixSearch.search().or(filter1, filter2).

Scenario 1: Create More Filters

In the following example we create filters and then join them to create an additional filter. The final filter contains functionality for searching for popular forum posts that have either more than 20 likes or more than 100 views.

import wixSearch from 'wix-search';
const filterBuilder = wixSearch.filter();
const gtLikeFilter = filterBuilder.gt("likeCount", 20);
const gtViewFilter = filterBuilder.gt("viewCount", 100);
const orFilter = filterBuilder.or(geLikeFilter, gtViewFilter)
javascript | Copy Code

At this point you can use the orFilter to either create additional filters or to refine a search.

Scenario 2: Refine a Search

In the following example we create filters and them pass them to an or() function appended to a search. We run a search for popular forum posts that have either more than 20 likes or more than 100 views:

import wixSearch from 'wix-search';
const filterBuilder = wixSearch.filter();
const gtLikeFilter = filterBuilder.gt("likeCount", 20);
const gtViewFilter = filterBuilder.gt("viewCount", 100);
wixSearch.search()
.documentType("Forum/Content")
.or(geLikeFilter, geViewFilter)
.find()
.then( (results) => {
console.log(results.documents);
} );
javascript | Copy Code

Note that only some fields can be filtered. Check which fields can be filtered in the supported schemas for each documentType:

Was this helpful?