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
The phrase to run the search on.
Was this helpful?
1let newSearch = search.query(phrase);
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.
1import wixSearch from 'wix-search';23// ...45$w("#searchButton").onClick(event => {6 const dropdownValue = $w("#myDropdown").value;7 const freeText = $w('#myInput').value;89 let searchBuilder = wixSearch.search();1011 if (dropdownValue) {12 searchBuilder = searchBuilder.query(dropdownValue);13 } else {14 searchBuilder = searchBuilder.query(freeText);15 }1617 searchBuilder18 .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 });2930});