|
| 1 | +[[endpoints]] |
| 2 | +==== Endpoints |
| 3 | + |
| 4 | +All the available endpoints are generated in separate packages and assembled in the client. The `core` namespace is duplicated at the root of the client for convenient access. |
| 5 | + |
| 6 | +Each endpoint follows a factory pattern which returns a pointer to a new instance each time. This leads to a builder pattern allowing to directly chain the options before running your query. |
| 7 | + |
| 8 | +[source,go] |
| 9 | +----- |
| 10 | +res, err := es.Search().Index("index_name").AllowPartialSearchResults(true).Do(context.Background()) |
| 11 | +----- |
| 12 | + |
| 13 | +If parameters are needed for the specific endpoint you are using, those will be present as arguments in the same order as the API: |
| 14 | + |
| 15 | +[source,go] |
| 16 | +------------------------------------ |
| 17 | +es.Create("index_name", "doc_id").Do(context.Background()) |
| 18 | +------------------------------------ |
| 19 | + |
| 20 | +Otherwise, you can find them within the builder: |
| 21 | + |
| 22 | +[source,go] |
| 23 | +------------------------------------ |
| 24 | +es.Search().Index("index_name").Do(context.Background()) |
| 25 | +------------------------------------ |
| 26 | + |
| 27 | +Alternatively each endpoint can be instantiated directly from its package: |
| 28 | + |
| 29 | +[source,go] |
| 30 | +------------------------------------ |
| 31 | +transport, err := elastictransport.New(elastictransport.Config{}) |
| 32 | +res, err = search.New(transport).Do(context.Background()) |
| 33 | +------------------------------------ |
| 34 | + |
| 35 | +The `Do` method takes an optional `context`, runs the request through the transport and returns the results as well as an error. |
| 36 | + |
| 37 | +For body-empty endpoints such as `core.Exists`, an additional method `IsSuccess` is available. As the `Do` method, it takes an optional `context`, drains and closes the body if needed, and returns a boolean alongside an error |
| 38 | + |
| 39 | +[source,go] |
| 40 | +----- |
| 41 | +if exists, err := es.Core.Exists("index_name", "doc_id").IsSuccess(context.Background()); exists { |
| 42 | + // The document exists! |
| 43 | +} else if err != nil { |
| 44 | + // An error occurred. |
| 45 | +} |
| 46 | +----- |
0 commit comments