SearchAsync #
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 SearchResults.Next rather than increasing the size parameter.
Arguments #
public async Task<SearchResults> SearchAsync(
string index,
string collection,
JObject query,
SearchOptions options = null);
Argument | Type | Description |
---|---|---|
index | string | Index name |
collection | string | Collection name |
query | JObject | JObject representing the search query |
options | SearchOptions | An instance of SearchOptions class |
query #
A JObject 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 SearchResults instance.
Exceptions #
Throws a KuzzleException
if there is an error. See how to handle errors.
Usage #
try {
for (int i = 0; i < 5; i++) {
await kuzzle.Document.CreateAsync(
"nyc-open-data",
"yellow-taxi",
JObject.Parse(@"{
""category"": ""suv""
}"));
}
for (int i = 5; i < 15; i++) {
await kuzzle.Document.CreateAsync(
"nyc-open-data",
"yellow-taxi",
JObject.Parse(@"{
""category"": ""limousine""
}"));
}
await kuzzle.Collection.RefreshAsync("nyc-open-data", "yellow-taxi");
SearchOptions options = new SearchOptions();
options.From = 0;
options.Size = 2;
SearchResults result = await kuzzle.Document.SearchAsync(
"nyc-open-data",
"yellow-taxi",
JObject.Parse(@"{
""query"": {
""match"": {
""category"": ""suv""
}
}
}"),
options);
Console.WriteLine($"Successfully retrieved {result.Total} documents");
} catch (KuzzleException e) {
Console.Error.WriteLine(e);
}
Edit this page on Github(opens new window)