CorvidReference

WixSearchResult

The results of a site search, containing the retrieved documents.

When you execute a search with the find() function, it returns a Promise that resolves to a WixSearchResult object. This object contains the documents that match the search and functions for paging through the search results.

Table of Contents

PROPERTIES

?
Store values associated with an object.
currentPageReturns the index of the current results page number.
documentsReturns the documents that match the search.
lengthReturns the number of documents in the current results page.
pageSizeReturns the search page size.
totalCountReturns the total number of documents that match the search.
totalPagesReturns the total number of pages the search produced.

FUNCTIONS

?
Perform actions on an object.
hasNext( )Indicates if the search has more results.
hasPrev( )Indicates if the search has previous results.
next( )Retrieves the next page of search results.
prev( )Retrieves the previous page of search results.

OBJECTS

?
Objects used when setting, getting, or calling the properties and methods listed above.
DocumentA document returned by a site search.

currentPage

Returns the index of the current results page number.

Description

The currentPage is a zero-based index of the current page of results.

The page size is defined by the limit() function, can be retrieved using the pageSize property, and navigating through pages is done with the prev() and next() functions.

The currentPage property returns undefined if the search returned no results.

Syntax

get currentPage(): number
TYPE
?
The kind of data the property stores.
number

Examples

Get the current page index of a search result

let resultPage = results.currentPage; // 0

Perform a search and get the current page index of the search result

import wixSearch from 'wix-search';

// ...

wixSearch.search(phrase)
  .find()
  .then( (results) => {
    let resultPage = results.currentPage; // 0
  } ) ;

documents

Returns the documents that match the search.

Description

The current page of documents retrieved by the search.

The page size is defined by the limit() function, can be retrieved using the pageSize property, and navigating through pages is done with the prev() and next() functions.

When no documents match the search, the documents array is empty.

Syntax

get documents(): Array<Document>
TYPE
?
The kind of data the property stores.
Array<Document>

Examples

Get the documents of a search result

let documents = results.documents;

/**
  * [
  *   {
  *     "_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"
  *   },
  *   {
  *     "_id":             "d9ffb3e6-be22-4047-b926-c9da6a1847e4",
  *     "url":             "/page-url",
  *     "documentType"     "Site/Pages",
  *     "image":           "wix:image://v1/45...bba2~mv2.jpg#originWidth=750&originHeight=750",
  *     "title":           "Page title",
  *     "description":     "Page text"
  *   }
  * ]
  */

Perform a search and get the documents of the result

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;
        } else {
          console.log("No matching results");
        }
      })
      .catch((error) => {
        console.log(error);
      });
  }
});

/**
  * [
  *   {
  *     "_id":             "aqmg7",
  *     "url":             "/page-url",
  *     "documentType"     "Site/Pages",
  *     "image":           "wix:image://v1/45...bba2~mv2.jpg#originWidth=750&originHeight=750",
  *     "title":           "Page title",
  *     "description":     "Page text"
  *   },
  *   {
  *     "_id":             "d9ffb3e6-be22-4047-b926-c9da6a1847e4",
  *     "url":             "/post/post-title",
  *     "documentType"     "Blog/Posts",
  *     "image":           "wix:image://v1/20...8hui~mv2.jpg#originWidth=250&originHeight=250",
  *     "title":           "Post title",
  *     "description":     "Post plain content"
  *   }
  *   {
  *     "_id":             "ea77f230-558f-57b6-cdd1-0ba565e8f827",
  *     "url":             "/bookings-checkout/service-title",
  *     "documentType"     "Bookings/Services",
  *     "image":           "wix:image://v1/11..._s_2.jpg#originWidth=3534&originHeight=2366",
  *     "title":           "Service name",
  *     "description":     "Service description"
  *   },
  *   {
  *     "_id":             "5df750deccb7040017b7c622",
  *     "url":             "/forum/category/post-title",
  *     "documentType"     "Forum/Posts",
  *     "image":           "wix:image://v1/a276dd~mv2.png#originWidth=466&originHeight=363",
  *     "title":           "Post title",
  *     "description":     "Post plain content"
  *   },
  *   {
  *     "_id":             "18176e85-ef8b-447c-b66f-f7b7ca360a5d",
  *     "url":             "/product-page/product-title",
  *     "documentType"     "Stores/Products",
  *     "image":           "wix:image://v1/67...4df7.jpg#originWidth=1920&originHeight=2891",
  *     "title":           "Product name",
  *     "description":     "Product description"
  *   }
  * ]
  */

length

Returns the number of documents in the current results page.

Description

The page size is defined by the limit() function, can be retrieved using the pageSize property, and navigating through pages is done with the prev() and next() functions.

Syntax

get length(): number
TYPE
?
The kind of data the property stores.
number

Examples

Get the length of a search result

let resultLength = results.length; // 20

Perform a search and get the length of the result

import wixSearch from 'wix-search';

// ...

wixSearch.search(phrase)
  .find()
  .then( (results) => {
    let resultLength = results.length; // 20
  } ) ;

pageSize

Returns the search page size.

Description

The page size is defined by the limit() function, can be retrieved using the pageSize property, and navigating through pages is done with the prev() and next() functions.

Syntax

get pageSize(): number
TYPE
?
The kind of data the property stores.
number

Examples

Get the page size of a search result

let resultPageSize = results.pageSize; // 25

Perform a search and get the page size of the search result

import wixSearch from 'wix-search';

// ...

wixSearch.search(phrase)
  .find()
  .then( (results) => {
    let resultPageSize = results.pageSize; // 25
  } ) ;

totalCount

Returns the total number of documents that match the search.

Description

The totalCount returns the total number of documents that match the search, not just the number of documents in the current page.

Syntax

get totalCount(): number
TYPE
?
The kind of data the property stores.
number

Examples

Get the total count of a search result

let resultCount = results.totalCount; // 50

Perform a search and get the total count of the result

import wixSearch from 'wix-search';

// ...

wixSearch.search(phrase)
  .find()
  .then( (results) => {
    let resultCount = results.totalCount; // 50
  } ) ;

totalPages

Returns the total number of pages the search produced.

Description

The page size is defined by the limit() function, can be retrieved using the pageSize property, and navigating through pages is done with the prev() and next() functions.

Syntax

get totalPages(): number
TYPE
?
The kind of data the property stores.
number

Examples

Get the total number of pages in a search result

let resultPages = results.totalPages; // 2

Perform a search and get the total number of pages in the search result

import wixSearch from 'wix-search';

// ...

wixSearch.search(phrase)
  .find()
  .then( (results) => {
    let resultPages = results.totalPages; // 2
  } ) ;

hasNext( )

Indicates if the search has more results.

Syntax

function hasNext(): boolean
RETURN VALUE
?
Value that a function evaluates to when it is finished running.
boolean
true if there are more results.

Examples

Get whether the search result object has more results

let hasNext = results.hasNext(); // true

Iterate through all pages of search results

This example demonstrates how to get all search results, bypassing the maximum limit of 1000.
async function retrieveAllDocuments(phrase){
  let allDocuments = [];
    
  let results = await wixSearch.search(phrase)
    .limit(1000)
    .find();
    
  allDocuments.push(results.documents);
    
  while(results.hasNext()) {
    results = await results.next();
    allDocuments.push(results.documents);
  }
    
  return allDocuments;
}

hasPrev( )

Indicates if the search has previous results.

Syntax

function hasPrev(): boolean
RETURN VALUE
?
Value that a function evaluates to when it is finished running.
boolean
true if there are previous results.

Examples

Get whether the search result object has previous results

let hasPrev = results.hasPrev(); // false

next( )

Retrieves the next page of search results.

Description

The next() function retrieves the next page of search results.

The page size is defined by the limit() function, can be retrieved using the pageSize property, and navigating through pages is done with the prev() and next() functions.

If items are added or removed between calls to next(), the values returned by WixSearchResult may change.

Syntax

function next(): Promise<WixSearchResult>
RETURN VALUE
?
Value that a function evaluates to when it is finished running.

Fulfilled - A search result object with the next page of search results. Rejected - The errors that caused the rejection.

Examples

Get the next page of a search result

oldResults.next()
  .then( (results) => {
    let newResults = results;
    let documents = newResults.documents;
    let firstDocument = documents[0];
    let totalCount = newResults.totalCount;
    let pageSize = newResults.pageSize;
    let currentPage = newResults.currentPage;
    let totalPages = newResults.totalPages;
    let hasNext = newResults.hasNext();
    let hasPrev = newResults.hasPrev();
    let length = newResults.length;
  } )
  .catch( (error) => {
    console.log(error);
  } );

Iterate through all pages of search results

This example demonstrates how to get all search results, bypassing the maximum limit of 1000.
async function retrieveAllDocuments(phrase){
  let allDocuments = [];
    
  let results = await wixSearch.search(phrase)
    .limit(1000)
    .find();
    
  allDocuments.push(results.documents);
    
  while(results.hasNext()) {
    results = await results.next();
    allDocuments.push(results.documents);
  }
    
  return allDocuments;
}

prev( )

Retrieves the previous page of search results.

Description

The prev() function retrieves the previous page of search results.

The page size is defined by the limit() function, can be retrieved using the pageSize property, and navigating through pages is done with the prev() and next() functions.

If items are added or removed between calls to prev() the values returned may change.

Syntax

function prev(): Promise<WixSearchResult>
RETURN VALUE
?
Value that a function evaluates to when it is finished running.

Fulfilled - A search result object with the previous page of search results. Rejected - The errors that caused the rejection.

Examples

Get the previous page of a search result

oldResults.prev()
  .then( (results) => {
    let newResults = results;
    let documents = newResults.documents;
    let firstDocument = documents[0];
    let totalCount = newResults.totalCount;
    let pageSize = newResults.pageSize;
    let currentPage = newResults.currentPage;
    let totalPages = newResults.totalPages;
    let hasNext = newResults.hasNext();
    let hasPrev = newResults.hasPrev();
    let length = newResults.length;
  } )
  .catch( (error) => {
    console.log(error);
  } );

Document

A document returned by a site search.

Syntax

type Document = {
  _id: string
  image: string
  documentType: string
  url: string
  description: string
  title: string
}
MEMBERS
?
The properties of an object.
_id
string
Unique document identifier.
image
string
Document image in the following format: wix:image://v1/<uri>/<filename>#originWidth=<width>&originHeight=<height>[&watermark=<watermark_manifest_string>]
documentType
string

Document type. One of the following:

  • Site/Pages
  • Blog/Posts
  • Bookings/Services
  • Forum/Posts
  • Stores/Products
url
string
The relative page URL. For regular site pages, the URL defined in SEO settings. Note that the url for the home page is an empty string. For Wix app pages, the URL stored in the database collection.
description
string
For regular site pages, all text on the page. For Wix app pages, the description or content stored in the database collection.
title
string
For regular site pages, the SEO page title. For Wix app pages, the title or name stored in the database collection.

Examples

Perform a search and get the documents of the result

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;
        } else {
          console.log("No matching results");
        }
      })
      .catch((error) => {
        console.log(error);
      });
  }
});

/**
  * [
  *   {
  *     "_id":             "aqmg7",
  *     "url":             "/page-url",
  *     "documentType"     "Site/Pages",
  *     "image":           "wix:image://v1/45...bba2~mv2.jpg#originWidth=750&originHeight=750",
  *     "title":           "Page title",
  *     "description":     "Page text"
  *   },
  *   {
  *     "_id":             "d9ffb3e6-be22-4047-b926-c9da6a1847e4",
  *     "url":             "/post/post-title",
  *     "documentType"     "Blog/Posts",
  *     "image":           "wix:image://v1/20...8hui~mv2.jpg#originWidth=250&originHeight=250",
  *     "title":           "Post title",
  *     "description":     "Post plain content"
  *   }
  *   {
  *     "_id":             "ea77f230-558f-57b6-cdd1-0ba565e8f827",
  *     "url":             "/bookings-checkout/service-title",
  *     "documentType"     "Bookings/Services",
  *     "image":           "wix:image://v1/11..._s_2.jpg#originWidth=3534&originHeight=2366",
  *     "title":           "Service name",
  *     "description":     "Service description"
  *   },
  *   {
  *     "_id":             "5df750deccb7040017b7c622",
  *     "url":             "/forum/category/post-title",
  *     "documentType"     "Forum/Posts",
  *     "image":           "wix:image://v1/a276dd~mv2.png#originWidth=466&originHeight=363",
  *     "title":           "Post title",
  *     "description":     "Post plain content"
  *   },
  *   {
  *     "_id":             "18176e85-ef8b-447c-b66f-f7b7ca360a5d",
  *     "url":             "/product-page/product-title",
  *     "documentType"     "Stores/Products",
  *     "image":           "wix:image://v1/67...4df7.jpg#originWidth=1920&originHeight=2891",
  *     "title":           "Product name",
  *     "description":     "Product description"
  *   }
  * ]
  */