SDK
SDK Javascript v7.x

# 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.

When using a cursor with the scroll option, Elasticsearch has to duplicate the transaction log to keep the same result during the entire scroll session.
It can lead to memory leaks if a scroll duration too great is provided, or if too many scroll sessions are open simultaneously.

Available since Kuzzle 2.2.0

You can restrict the scroll session maximum duration under the services.storage.maxScrollDuration configuration key.


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)
verb
string
(HTTP only) Forces the verb of the route

# verb

When instantiated with a HTTP protocol object, the SDK uses the POST API by default for this API route. You can set the verb option to GET to force the SDK to use the GET API instead.

# 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.collection.refresh('nyc-open-data', 'yellow-taxi');
  const results = await kuzzle.document.search(
    'nyc-open-data',
    'yellow-taxi',
    {
      query: {
        match: {
          category: 'suv'
        }
      }
    }
  );
  console.log(results);
  /*
    {
      "aggregations": undefined,
      "hits": [
        {
          "_id": "AWgi6A1POQUM6ucJ3q06",
          "_score": 0.046520017,
          "_source": {
            "category": "suv",
            "_kuzzle_info": {
              "author": "-1",
              "createdAt": 1546773859655,
              "updatedAt": null,
              "updater": null
            }
          }
        },
        ...
      ]
    },
    "total": 5,
    "fetched": 5,
    "scroll_id": undefined
  */
  console.log(`Successfully retrieved ${results.total} documents`);
} catch (error) {
  console.error(error.message);
}