CorvidReference

wix-search

The wix-search module contains functionality for searching a site.

The wix-search API allows you to create a custom site search experience for your site visitors. For example, you can use a text input element to get a search phrase from site visitors, and use a repeater to display the results of a search.

To enable Search API functionality for your site, you must add the Wix Site Search application to your site. After installing the app you can delete the Search Bar, but make sure to leave the Search Results page on your site. Note that you cannot use the Search API to control the Wix Search Bar or Search Results page.

The Search API searches supported site documents and returns matching search results in Document form. The following documents are currently supported:

  • Site pages: All regular site pages with SEO indexing enabled. Note that Wix site pages are indexed by default. Dynamic pages, custom router pages, and some Wix app pages are not currently included in searches.
  • Wix app pages: Items in the following database collections: Blog/Posts, Bookings/Services, Forum/Posts, and Stores/Products. Each item represents a Wix app page on your site.

    To use the search module, import wixSearch from the wix-search module:

    import wixSearch from 'wix-search';

Table of Contents

FUNCTIONS

?
Perform actions on an object.
search( )Creates a search.

search( )

Creates a search.

Description

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

The returned object contains the 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 and control the results a search returns.

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

  • documentType: none (searches all document types)
  • language: For multilingual sites, the current language. For single-language sites, the site language.
  • skip: 0
  • limit: 25

find() returns results in the form of a WixSearchResult, which contains documents that match the search, information about the search itself, 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.

Syntax

function search([phrase: string]): WixSearchBuilder
PARAMETERS
?
phrase(Optional)
string
The phrase to run the search on.
RETURN VALUE
?
Value that a function evaluates to when it is finished running.
A search builder object.

Examples

Build a search

import wixSearch from 'wix-search';

// ...

let search = wixSearch.search(phrase);

Build and perform a search

import wixSearch from 'wix-search';

// ...

$w("#searchInput").onKeyPress((keyPress) => {
  if (keyPress.key === "Enter") {
    const phrase = $w("#searchInput").value;
    wixSearch.search(phrase)
      .find()
      .then((results) => {
        if (results.documents.length > 0) {
          let documents = results.documents;
          let firstDocument = documents[0];
          let totalCount = results.totalCount;
          let pageSize = results.pageSize;
          let currentPage = results.currentPage;
          let totalPages = results.totalPages;
          let hasNext = results.hasNext();
          let hasPrev = results.hasPrev();
          let length = results.length;
        } else {
          console.log("No matching results");
        }
      })
      .catch((error) => {
        console.log(error);
      });
  }
});

Build and perform a search and get the first document in the search results

import wixSearch from 'wix-search';

// ...

$w("#searchInput").onKeyPress((keyPress) => {
  if (keyPress.key === "Enter") {
    const phrase = $w("#searchInput").value;
    wixSearch.search(phrase)
      .find()
      .then((results) => {
        if (results.documents.length > 0) {
          let firstDocument = results.documents[0];
        } else {
          console.log("No matching results");
        }
      })
      .catch((error) => {
        console.log(error);
      });
  }
});

/**   Example firstDocument:
  *
  *  {
  *     "_id":             "18176e85-ef8b-447c-b66f-f7b7ca360a5d",
  *     "url":             "/product-page/product-title",
  *     "documentType"     "Stores/Product",
  *     "image":           "wix:image://v1/67...4df7.jpg#originWidth=1920&originHeight=2891",
  *     "title":           "Product title",
  *     "description":     "Product description"
  *   }
  */

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.
import wixSearch from 'wix-search';

// ...

// Before search runs, set the repeater data as empty
$w('#repeater').data = [];

$w("#searchInput").onKeyPress((keyPress) => {
  if (keyPress.key === "Enter") {
    const phrase = $w("#searchInput").value;
    wixSearch.search(phrase)
      .documentType("Stores/Products")
      .find()
      .then((results) => {
        if (results.documents.length > 0) {
          $w('#repeater').data = results.documents;
        } else {
          console.log("No matching results");
        }
      })
      .catch((error) => {
        console.log(error);
      });
  }
});

$w("#repeater").onItemReady(($item, itemData) => {
  $item("#title").text = itemData.title;
  $item("#description").text = itemData.description;
  $item("#image").src = itemData.image;
  $item("#button").link = itemData.url;
});