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.
Arguments #
std::shared_ptr<kuzzleio::SearchResult> search(
const std::string& index,
const std::string& collection,
const std::string& query);
std::shared_ptr<kuzzleio::SearchResult> search(
const std::string& index,
const std::string& collection,
const std::string& query,
const kuzzleio::query_options& options);
Argument | Type | Description |
---|---|---|
index | const std::string& | Index name |
collection | const std::string& | Collection name |
query | const std::string& | JSON string representing the search query |
options | kuzzleio::query_options* | Query options |
options #
Additional query options
Option | Type (default) | Description |
---|---|---|
queuable | bool ( true ) | If true, queues the request during downtime, until connected to Kuzzle again |
from | int ( 0 ) | Offset of the first document to fetch |
size | int ( 10 ) | Maximum number of documents to retrieve per page |
scroll | const std::string& ( "" ) | When set, gets a forward-only cursor having its ttl set to the given value (ie 30s ; cf elasticsearch time limits) |
query #
A JSON string representing the query. Query can have the following root properties:
query
: the search query itself, using the ElasticSearch Query DSL syntax.aggregations
: control how the search results should be aggregatedsort
: contains a list of fields, used to sort search results, in order of importance.
An empty body matches all documents in the queried collection.
Return #
Returns a kuzzleio::SearchResult instance.
Exceptions #
Throws a kuzzleio::KuzzleException
if there is an error. See how to handle errors.
Usage #
try {
for (size_t i = 0; i < 5; i++) {
kuzzle->document->create("nyc-open-data", "yellow-taxi", "", R"({
"category": "suv"
})");
}
for (size_t i = 5; i < 15; i++) {
kuzzle->document->create("nyc-open-data", "yellow-taxi", "", R"({
"category": "limousine"
})");
}
kuzzle->index->refresh("nyc-open-data");
kuzzleio::query_options options;
options.from = 0;
options.size = 2;
std::shared_ptr<kuzzleio::SearchResult> results = kuzzle->document->search(
"nyc-open-data",
"yellow-taxi",
R"({
"query": {
"match": {
"category": "suv"
}
}
})",
options);
std::cout << "Successfully retrieved " << results->total() << " documents" << std::endl;
} catch (kuzzleio::KuzzleException& e) {
std::cerr << e.what() << std::endl;
}