filter( )
Creates a filter builder for building search filters.
Description
The filter()
function is used for creating compound filters that are applied to a search using the
and()
, or()
, and not()
filtering functions. You do not need the filter()
function for applying most filtering
functions (such as eq()
or gt()
) to a search. Instead you can add WixSearchBuilder
filtering functions directly to your search.
The filter()
function creates a WixSearchFilterBuilder
object,
which contains functionality for creating compound filters for your site search.
The following describes a typical filter builder flow:
- Create a
WixSearchFilterBuilder
using thefilter()
function. - Create a
WixSearchFilter
by chaining a singleWixSearchFilterBuilder
filtering function, such aseq()
orgt()
, to the filter builder. You cannot chain more than 1 filtering function to aWixSearchFilterBuilder
. - Do one of the following:
- 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)
.
Note that only some fields can be filtered. Check which fields can be filtered in
the supported schemas for each documentType
.
Syntax
function filter(): WixSearchFilterBuilder
filter Parameters
This function does not take any parameters.
Was this helpful?
1import wixSearch from 'wix-search';23// ...45let filterBuilder = wixSearch.filter();
1import wixSearch from 'wix-search';23// ...45let filterBuilder = wixSearch.filter();6let myFilter = filterBuilder.eq("inStock", true);
This example demonstrates how to create multiple search filters and combine them to search for forum posts or comments with more than 200 views that were posted either in the month of January or April of 2020.
1import wixSearch from 'wix-search';23// ...45const filterBuilder = wixSearch.filter();67const gtJanFilter = filterBuilder.gt("lastActivityDate", "2020-01-01T00:00:00.000Z");8const ltFebFilter = filterBuilder.lt("lastActivityDate", "2020-02-01T00:00:00.000Z");910const gtAprilFilter = filterBuilder.gt("lastActivityDate", "2020-04-01T00:00:00.000Z");11const ltMayFilter = filterBuilder.lt("lastActivityDate", "2020-05-01T00:00:00.000Z");1213const januaryFilter = filterBuilder.and(gtJanFilter, ltFebFilter);14const aprilFilter = filterBuilder.and(gtAprilFilter, ltMayFilter);1516const dateFilter = filterBuilder.or(januaryFilter, aprilFilter)1718const viewFilter = filterBuilder.gt("viewCount", 200)1920wixSearch.search(phrase)21 .documentType("Forum/Content")22 .and(dateFilter, viewFilter)23 .find()24 .then( (results) => {25 if(results.documents.length > 0) {26 let documents = results.documents;27 } else {28 console.log("No matching results");29 }30 })31 .catch( (error) => {32 console.log(error);33 });