Using the API
The examples in this section use the typed API together with the esdsl builders: the recommended way to use the Go client. The typed API gives you strongly typed request and response structs generated from the elasticsearch-specification, and esdsl provides fluent, chainable builders for queries, aggregations, mappings, and sort options.
If you need raw-JSON control or an endpoint not yet covered by the typed API, see the low-level API. The two APIs share the same transport, so you can mix them in the same application. See the migration guide for how to adopt the typed API one endpoint at a time.
The same search operation, expressed with the typed API and esdsl:
import "github.com/elastic/go-elasticsearch/v9/typedapi/esdsl"
res, err := es.Search().
Index("my-index").
Query(esdsl.NewMatchQuery("title", "golang")).
Do(context.Background())
if err != nil {
log.Fatal(err)
}
for _, hit := range res.Hits.Hits {
fmt.Println(hit.Source_)
}
esdslbuilders produce typed query structs with a fluent, chainable syntax. No deeply nested struct literals.- The response is already typed and decoded. No
io.Readerto parse, nodefer res.Body.Close().
- CRUD operations: index, get, update, and delete documents
- Searching: build and run search queries
- Aggregations: run aggregations on your data
- Bulk indexing: efficiently ingest large volumes of documents
- ES|QL: use the Elasticsearch Query Language
- Dense vectors and kNN search: work with vector embeddings and similarity search