SDK
SDK Javascript v6.x
1

You are currently looking at the documentation of a previous version of Kuzzle. We strongly recommend that you use the latest version. You can also use the version selector in the top menu.

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.


search(index, collection, [query], [options]);
ArgumentTypeDescription
index
string
Index name
collection
string
Collection name
query
object
Search query
options
object
Query options

Options #

Additional query options

OptionsType
(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 #

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);
}