Elasticsearch client for Go (Golang): a powerful tool for working with search
Elasticsearch is a distributed search and analytics system based on Apache Lucene that enables full-text search, aggregations, and real-time analytics. To interact with Elasticsearch from the Go (Golang) language, there is an official client — go-elasticsearch. This is a library developed and maintained by the Elastic team that provides a complete set of tools for working with the Elasticsearch REST API.
Features and advantages of go-elasticsearch
The go-elasticsearch library offers Go developers the following capabilities:
- Full support for the Elasticsearch API — all API versions, including indexing, search, aggregations, cluster management, and settings.
- Typed helper functions — simplify creating queries and processing responses.
- Support for bulk operations — mass insertion, updating, and deletion of documents.
- Automatic connection management — connection pool, retries on errors, and load balancing.
- Compatibility with Go modules — easy installation and updates via
go get. - Support for contexts — request cancellation and timeouts.
- Extensibility — the ability to create custom transports and middleware.
Installation and configuration
To install the library, run the command:
go get github.com/elastic/go-elasticsearch/v8After installation, you can create a client with basic settings:
package main
import ( "log" "github.com/elastic/go-elasticsearch/v8")
func main() { cfg := elasticsearch.Config{ Addresses: []string{ "http://localhost:9200", }, } es, err := elasticsearch.NewClient(cfg) if err != nil { log.Fatalf("Error creating client: %s", err) } log.Println("Elasticsearch client successfully created")}To connect to a cluster with authentication, specify the username and password:
cfg := elasticsearch.Config{ Addresses: []string{"https://your-cluster-url:9200"}, Username: "elastic", Password: "your_password",}Basic data operations
Creating an index
res, err := es.Indices.Create("my_index")if err != nil { log.Fatalf("Error creating index: %s", err)}defer res.Body.Close()log.Println(res.String())Indexing a document
To add a document, use the Index method:
package main
import ( "bytes" "encoding/json" "log" "github.com/elastic/go-elasticsearch/v8")
type Book struct { Title string `json:"title"` Author string `json:"author"` Year int `json:"year"`}
func main() { es, _ := elasticsearch.NewClient(elasticsearch.Config{ Addresses: []string{"http://localhost:9200"}, })
book := Book{ Title: "War and Peace", Author: "Leo Tolstoy", Year: 1869, }
data, _ := json.Marshal(book) res, err := es.Index("books", bytes.NewReader(data)) if err != nil { log.Fatalf("Error indexing: %s", err) } defer res.Body.Close() log.Println(res.String())}Searching documents
Search is performed using the Search method with a request body in JSON format:
query := `{ "query": { "match": { "author": "Tolstoy" } }}`
res, err := es.Search( es.Search.WithIndex("books"), es.Search.WithBody(bytes.NewReader([]byte(query))), es.Search.WithPretty(),)if err