search

Searches documents.

There is a limit to how many documents can be returned by a single search query. That limit is by default set at 10000 documents, and you can't get over it even with the from and size pagination options.

When processing a large number of documents (i.e. more than 1000), it is advised to paginate the results using SearchResult.next rather than increasing the size parameter.


Copied to clipboard!
search(index, collection, [query], [options]);
Argument Type Description
index
string
Index name
collection
string
Collection name
query
object
Search query
options
object
Query options

Options

Additional query options

Options Type
(default)
Description
queuable
boolean

(true)
If true, queues the request during downtime, until connected to Kuzzle again
from
number

(0)
Offset of the first document to fetch
size
number

(10)
Maximum number of documents to retrieve per page
scroll
string

("")
When set, gets a forward-only cursor having its ttl set to the given value (ie 30s; cf elasticsearch time limits)

Body properties

Optional:

An empty body matches all documents in the queried collection.

Resolves

Resolves to a SearchResult object.

Usage

Copied to clipboard!
const suv = { category: 'suv' };
const limousine = { category: 'limousine' };

try {
  const requests = [];

  for (let i = 0; i < 5; i++) {
    requests.push(kuzzle.document.create('nyc-open-data', 'yellow-taxi', suv));
  }
  for (let i = 0; i < 10; i++) {
    requests.push(kuzzle.document.create('nyc-open-data', 'yellow-taxi', limousine));
  }
  await Promise.all(requests);

  await kuzzle.index.refresh('nyc-open-data');

  const results = await kuzzle.document.search(
    'nyc-open-data',
    'yellow-taxi',
    {
      query: {
        match: {
          category: 'suv'
        }
      }
    }
  );

  console.log(results);
  /*
    {
      "aggregations": undefined,
      "hits": [
        {
          "_index": "nyc-open-data",
          "_type": "yellow-taxi",
          "_id": "AWgi6A1POQUM6ucJ3q06",
          "_score": 0.046520017,
          "_source": {
            "category": "suv",
            "_kuzzle_info": {
              "author": "-1",
              "createdAt": 1546773859655,
              "updatedAt": null,
              "updater": null,
              "active": true,
              "deletedAt": null
            }
          }
        },
        ...
      ]
    },
    "total": 5,
    "fetched": 5,
    "scroll_id": undefined
  */
  console.log(`Successfully retrieved ${results.total} documents`);
} catch (error) {
  console.error(error.message);
}