Search.../

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:

  1. Create a WixSearchFilterBuilder using the filter() function.
  2. Create a WixSearchFilter by chaining a single WixSearchFilterBuilder filtering function, such as eq() or gt(), to the filter builder. You cannot chain more than 1 filtering function to a WixSearchFilterBuilder.
  3. Do one of the following:
  • 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).

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

Authorization

Request

This endpoint does not take any parameters

Response Object

A search filter builder object.

Returns an empty object.

Status/Error Codes

Was this helpful?

Create a filter builder

Copy Code
1import wixSearch from 'wix-search';
2
3// ...
4
5let filterBuilder = wixSearch.filter();
Create a filter

Copy Code
1import wixSearch from 'wix-search';
2
3// ...
4
5let filterBuilder = wixSearch.filter();
6let myFilter = filterBuilder.eq("inStock", true);
Create multiple search filters and add them to a search

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.

Copy Code
1import wixSearch from 'wix-search';
2
3// ...
4
5const filterBuilder = wixSearch.filter();
6
7const gtJanFilter = filterBuilder.gt("lastActivityDate", "2020-01-01T00:00:00.000Z");
8const ltFebFilter = filterBuilder.lt("lastActivityDate", "2020-02-01T00:00:00.000Z");
9
10const gtAprilFilter = filterBuilder.gt("lastActivityDate", "2020-04-01T00:00:00.000Z");
11const ltMayFilter = filterBuilder.lt("lastActivityDate", "2020-05-01T00:00:00.000Z");
12
13const januaryFilter = filterBuilder.and(gtJanFilter, ltFebFilter);
14const aprilFilter = filterBuilder.and(gtAprilFilter, ltMayFilter);
15
16const dateFilter = filterBuilder.or(januaryFilter, aprilFilter)
17
18const viewFilter = filterBuilder.gt("viewCount", 200)
19
20wixSearch.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 });