SDK
SDK Golang v1.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 #

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 #

Copied to clipboard!
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: #

An empty body matches all documents in the queried collection.

Return #

Returns a pointer on types.SearchResult struct

Usage #

Copied to clipboard!
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.Index.Refresh("nyc-open-data", 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)
}