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 #
Search( index string, collection string, query json.RawMessage, options types.QueryOptions) (*types.SearchResult, error)| Argument | Type | Description |
|---|---|---|
index | string | Index name |
collection | string | Collection name |
query | json.RawMessage | Search query |
options | types.QueryOptions | A struct containing 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 | 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: #
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 pointer on types.SearchResult struct
Usage #
for i := 0; i < 5; i++ { kuzzle.Document.Create("nyc-open-data", "yellow-taxi", "", json.RawMessage(`{ "category": "suv" }`), nil) } for i := 5; i < 15; i++ { kuzzle.Document.Create("nyc-open-data", "yellow-taxi", "", json.RawMessage(`{ "category": "limousine" }`), nil) } kuzzle.Collection.Refresh("nyc-open-data", "yellow-taxi", nil) options := types.NewQueryOptions() options.SetFrom(0) options.SetSize(2) response, err := kuzzle.Document.Search("nyc-open-data", "yellow-taxi", json.RawMessage(`{ "query": { "match": { "category": "suv" } } }`), options) if err != nil { log.Fatal(err) } else { fmt.Printf("Successfully retrieved %d documents", response.Total) }