SDK
SDK Javascript v5.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 #

Executes a search on the collection.

There is a small delay between the time a document is created and its availability in our search layer (usually a couple of seconds). That means that a document that was just created might not be returned by this function at first.

Processing large data sets #

When processing a large number of documents (i.e. more than 1000), using search is not always the best option.

Pagination of results can be done by using the from and size but the cost becomes prohibitive when deep pagination is reached. In fact, Elasticsearch, Kuzzle's embedded database, limits results to 10,000 records by default.

Instead, the recommended way to process a large number of documents is to use Collection.scroll or, easier, SearchResult.fetchNext.

See SearchResult.fetchNext for an example of how to process every document of a collection.


search(body, [options], callback) #

ArgumentsTypeDescription
bodyJSON objectSearch request body, using ElasticSearch Query DSL format.
If given an empty object, matches all documents in the collection
optionsJSON objectOptional parameters
callbackfunctionCallback handling the response

Options #

OptionTypeDescriptionDefault
fromnumberProvide the starting offset of the request (used to paginate results)0
queuablebooleanMake this request queuable or nottrue
scrollstringStart a scroll session, with a time to live equals to this parameter's value following the Elastisearch time formatundefined
sizenumberProvide the maximum number of results of the request (used to paginate results)10
To get more information about scroll sessions, please refer to the [API reference documentation](/core/1/api/controllers/document/search).

Callback Response #

Returns an instance of SearchResult.

Usage #

const
  body = {
    query: {
      bool: {
        must: [
          {
            terms: {status: ['idle', 'wantToHire', 'toHire', 'riding']}
          },
          {
            term: {type: 'cab'}
          },
          {
            geo_distance: {
              distance: '10km',
              pos: {lat: '48.8566140', lon: '2.352222'}
            }
          }
        ]
      }
    },
    sort: [
      'status',
      {
        _geo_distance : {
          pos: {lat: '48.8566140', lon: '2.352222'},
          order : "asc"
        }
      },
      {date: "desc"}
    ],
    aggregations: {
      aggs_name: {
        terms: {
          field: "field_name"
        }
      }
    }
  },
  options = {
    from: 0,
    size: 20
  };
// Using callbacks (NodeJS or Web Browser)
kuzzle
  .collection('collection', 'index')
  .search(body, options, function (err, searchResult) {
    searchResult.getDocuments().forEach(function(document) {
      console.log(document.toString());
    });
  });
// Using promises (NodeJS only)
kuzzle
  .collection('collection', 'index')
  .searchPromise(body, options)
  .then(searchResult => {
    searchResult.getDocuments().forEach(document => {
      console.log(document.toString());
    });
  });