Search.../

search( )

Creates a search builder.

Description

The search() function builds a search for a site and returns a WixSearchBuilder object.

The returned object contains a search builder, which is used to run the search with the find() function.

Before running find(), you can refine the search by chaining WixSearchBuilder functions on to the search. WixSearchBuilder functions enable you to filter, sort, apply facets, and control the results a search returns.

search() runs with the following WixSearchBuilder defaults that you can override:

find() returns results in the form of a WixSearchResult, which contains documents that match the search, faceting and paging information, and functions for paging through the search results.

search() is not case sensitive.

If a search phrase is not specified, search returns all documents refined by the search builder. You can leave out the search phrase if you want to filter, sort, and/or apply facets to your site content without searching for a particular phrase.

Syntax

function search([phrase: string]): WixSearchBuilder

search Parameters

NAME
TYPE
DESCRIPTION
phrase
Optional

string

The phrase to run the search on.

Returns

A search builder object.

Return Type:

Was this helpful?

Build a search

Code Example

Copy Code
1import wixSearch from 'wix-search';
2
3// ...
4
5let search = wixSearch.search(phrase);
Build and perform a search

Code Example

Copy Code
1import wixSearch from 'wix-search';
2
3// ...
4
5$w("#searchInput").onKeyPress((keyPress) => {
6 if (keyPress.key === "Enter") {
7 const phrase = $w("#searchInput").value;
8 wixSearch.search(phrase)
9 .find()
10 .then((results) => {
11 if (results.documents.length > 0) {
12 let documents = results.documents;
13 let firstDocument = documents[0];
14 let facets = results.facets;
15 let totalCount = results.totalCount;
16 let pageSize = results.pageSize;
17 let currentPage = results.currentPage;
18 let totalPages = results.totalPages;
19 let hasNext = results.hasNext();
20 let hasPrev = results.hasPrev();
21 let length = results.length;
22 } else {
23 console.log("No matching results");
24 }
25 })
26 .catch((error) => {
27 console.log(error);
28 });
29 }
30});
Build and perform a search and get the first document in the search results

Code Example

Copy Code
1import wixSearch from 'wix-search';
2
3// ...
4
5$w("#searchInput").onKeyPress((keyPress) => {
6 if (keyPress.key === "Enter") {
7 const phrase = $w("#searchInput").value;
8 wixSearch.search(phrase)
9 .find()
10 .then((results) => {
11 if (results.documents.length > 0) {
12 let firstDocument = results.documents[0];
13 } else {
14 console.log("No matching results");
15 }
16 })
17 .catch((error) => {
18 console.log(error);
19 });
20 }
21});
22
23
24/** Example firstDocument:
25 *
26 * {
27 * "_id": "18176e85-ef8b-447c-b66f-f7b7ca360a5d",
28 * "url": "/about",
29 * "documentType" "Site/Pages",
30 * "image": "wix:image://v1/67...4df7.jpg#originWidth=1920&originHeight=2891",
31 * "title": "The Leader in Website Creation",
32 * "description": "Create your own professional web presence—exactly the way you want. Our powerful..."
33 * }
34 */
Build and perform a search and display the results in a repeater

This example demonstrates how to set up a search for store products and display the results in a repeater. Each button in the repeater links to the product page for the selected product.

Code Example

Copy Code
1import wixSearch from 'wix-search';
2
3// ...
4
5// Before search runs, set the repeater data as empty
6$w('#repeater').data = [];
7
8$w("#searchInput").onKeyPress((keyPress) => {
9 if (keyPress.key === "Enter") {
10 const phrase = $w("#searchInput").value;
11 wixSearch.search(phrase)
12 .documentType("Stores/Products")
13 .find()
14 .then((results) => {
15 if (results.documents.length > 0) {
16 $w('#repeater').data = results.documents;
17 } else {
18 console.log("No matching results");
19 }
20 })
21 .catch((error) => {
22 console.log(error);
23 });
24 }
25});
26
27$w("#repeater").onItemReady(($item, itemData) => {
28 $item("#title").text = itemData.title;
29 $item("#description").text = itemData.description;
30 $item("#image").src = itemData.image;
31 $item("#button").link = itemData.url;
32});
33