Search.../

query( )

Sets the search phrase to search for.

Description

The query() function provides an alternate method for setting the search phrase for your search. Instead of setting the search phrase directly as a parameter of the search() function, you can set the search phrase using the query() function chained to a WixSearchBuilder.

Setting the search phrase with query() allows you to build searches dynamically. For example, you could use a different search phrase depending on whether a specific condition is met.

Syntax

function query(phrase: string): WixSearchBuilder

query Parameters

NAME
TYPE
DESCRIPTION
phrase

string

The phrase to run the search on.

Returns

A WixSearchBuilder object representing the refined search.

Return Type:

Was this helpful?

Add a query to a search

Code Example

Copy Code
1let newSearch = search.query(phrase);
Build and run a dynamic search

In this example, site visitors can search a site either by selecting a search option from a dropdown element, or by entering free text in an input element. If a dropdown option is selected, the dropdown value is searched. If there is no dropdown selection, the free text is searched.

Code Example

Copy Code
1import wixSearch from 'wix-search';
2
3// ...
4
5$w("#searchButton").onClick(event => {
6 const dropdownValue = $w("#myDropdown").value;
7 const freeText = $w('#myInput').value;
8
9 let searchBuilder = wixSearch.search();
10
11 if (dropdownValue) {
12 searchBuilder = searchBuilder.query(dropdownValue);
13 } else {
14 searchBuilder = searchBuilder.query(freeText);
15 }
16
17 searchBuilder
18 .find()
19 .then((results) => {
20 if (results.documents.length > 0) {
21 let documents = results.documents;
22 } else {
23 console.log("No matching results");
24 }
25 })
26 .catch((error) => {
27 console.log(error);
28 });
29
30});