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:
- Create a
WixSearchFilterBuilder
using thefilter()
function. - Create a
WixSearchFilter
by applying a singleWixSearchFilterBuilder
filter function, such aseq()
orgt()
, to the filter builder. You cannot chain more than 1 filtering function to aWixSearchFilterBuilder
. - You can use
WixSearchFilters
in one of 2 scenarios:
- Create more filters: Pass one or more
WixSearchFilters
as parameters to anand()
,or()
, ornot()
WixSearchFilterBuilder
function to create a newWixSearchFilter
. For example,newFilter = wixSearch.filter().or(filter1, filter2)
. - Refine a search: Pass one or more
WixSearchFilters
as parameters to anand()
,or()
, ornot()
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.
javascript | Copy Codeimport 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)
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:
javascript | Copy Codeimport 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);} );
Note that only some fields can be filtered. Check which fields can be filtered in the supported schemas for each documentType
:
Was this helpful?