search

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());
    });
  });
import io.kuzzle.sdk.core.Kuzzle;
import io.kuzzle.sdk.core.Options; 

Kuzzle kuzzle = new Kuzzle("localhost");

JSONObject body = new JSONObject()
  .put("query", new JSONObject()
    .put("bool", new JSONObject()
      .put("must", new JSONArray()
        .put(
          new JSONObject().put("terms",
            new JSONObject().put("status",
              new JSONArray()
                .put("idle")
                .put("wantToHire")
                .put("toHire")
                .put("riding")
            )
          )
        )
        .put(
          new JSONObject().put("term",
            new JSONObject()
              .put("type", new JSONArray().put("cab"))
          )
        )
        .put(
          new JSONObject().put("geo_distance",
            new JSONObject()
              .put("distance", "10km")
              .put("pos",
                new JSONObject()
                  .put("lat", "48.8566140")
                  .put("lon", "2.352222")
              )
          )
        )
      )
    )
  )
  .put("sort", new JSONArray()
    .put("status")
    .put(new JSONObject()
      .put("_geo_distance", new JSONObject()
        .put("pos", new JSONObject()
          .put("lat", "48.8566140")
          .put("lon", "2.352222")
        )
        .put('order'; "asc")
      )
    )
    .put(new JSONObject()
      .put("date", "desc")
    )
  )
  .put("aggregations", new JSONObject()
    .put("aggs_name", new JSONObject()
      .put("terms", new JSONObject()
        .put("field", "field_name")
      )
    )
  );

Options options = new Options();
options.setFrom((long) 0);
options.setSize((long) 20);

kuzzle
  .collection("collection", "index")
  .search(body, options, new ResponseListener<SearchResult>() {
    @Override
    public void onSuccess(SearchResult searchResult) {
      for (Document doc : searchResult.getDocuments()) {
        // Get documents
      }

      searchResult.getTotal(); // return total of documents returned

      searchResult.getAggregations(): // return a JSONObject representing the aggregations response
    }

    @Override
    public void onError(JSONObject error) {
      // Handle error
    }
  });
<?php

use \Kuzzle\Kuzzle;
use \Kuzzle\Document;
use \Kuzzle\Util\SearchResult;

$body = [
  'query' => [
    'bool' => [
      'must' => [
        [
          'terms' => ['status' => ['idle', 'wantToHire', 'toHire', 'riding']]
        ],
        ['term' => '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
];

$kuzzle = new Kuzzle('localhost');
$dataCollection = $kuzzle->collection('collection', 'index');

try {
  $searchResult = $dataCollection->search($body, $options);

  // $searchResult instanceof SearchResult
  $searchResult->getTotal();

  foreach($searchResult->getDocuments() as $document) {
    // $document instanceof Document
  }

  // return an array representing the aggregations response
  $searchResult->getAggregations();
}
catch (ErrorException $e) {

}

Executes a search on the data collection.

Processing large data sets

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

Pagination of results can be done by using the from and size but the cost becomes prohibitive when the deep pagination is reached. In fact, Elasticsearch, the database Kuzzle is relying on, prevents going beyond 10,000 results 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
queuablebooleanMark this request as (not) queuabletrue
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

Callback response

Resolves to an instance of SearchResult.