Convert Figma logo to code with AI

elastic logogo-elasticsearch

The official Go client for Elasticsearch

5,626
609
5,626
61

Top Related Projects

7,374

Deprecated: Use the official Elasticsearch client for Go at https://github.com/elastic/go-elasticsearch

9,971

A modern text/numeric/geo-spatial/vector indexing library for go

Official Python client for Elasticsearch

Quick Overview

The elastic/go-elasticsearch repository is the official Go client for Elasticsearch. It provides a robust and efficient way to interact with Elasticsearch clusters, allowing developers to perform various operations such as indexing, searching, and managing documents and indices using Go programming language.

Pros

  • Native Go implementation with excellent performance
  • Comprehensive API coverage for Elasticsearch operations
  • Well-documented with extensive examples and guides
  • Supports multiple Elasticsearch versions

Cons

  • Steep learning curve for developers new to Elasticsearch
  • Complex API structure might be overwhelming for simple use cases
  • Requires careful error handling and connection management
  • Limited built-in high-level abstractions compared to some other clients

Code Examples

  1. Creating a client and performing a simple search:
cfg := elasticsearch.Config{
    Addresses: []string{
        "http://localhost:9200",
    },
}
es, _ := elasticsearch.NewClient(cfg)

res, _ := es.Search(
    es.Search.WithIndex("my-index"),
    es.Search.WithBody(strings.NewReader(`{"query":{"match":{"title":"elasticsearch"}}}`)),
)
fmt.Println(res)
  1. Indexing a document:
doc := `{"title":"Test document","content":"This is a test"}`
res, _ := es.Index(
    "my-index",
    strings.NewReader(doc),
    es.Index.WithDocumentID("1"),
    es.Index.WithRefresh("true"),
)
fmt.Println(res)
  1. Performing an aggregation:
query := `{
  "aggs": {
    "avg_price": {
      "avg": {
        "field": "price"
      }
    }
  }
}`
res, _ := es.Search(
    es.Search.WithIndex("products"),
    es.Search.WithBody(strings.NewReader(query)),
)
fmt.Println(res)

Getting Started

To start using the Elasticsearch Go client:

  1. Install the package:

    go get github.com/elastic/go-elasticsearch/v8
    
  2. Import the package in your Go code:

    import "github.com/elastic/go-elasticsearch/v8"
    
  3. Create a client:

    cfg := elasticsearch.Config{
        Addresses: []string{"http://localhost:9200"},
    }
    es, err := elasticsearch.NewClient(cfg)
    if err != nil {
        log.Fatalf("Error creating client: %s", err)
    }
    
  4. Start using the client to interact with your Elasticsearch cluster.

Competitor Comparisons

7,374

Deprecated: Use the official Elasticsearch client for Go at https://github.com/elastic/go-elasticsearch

Pros of elastic

  • More mature and feature-rich, with a longer development history
  • Extensive documentation and community support
  • Provides higher-level abstractions and helper functions for common tasks

Cons of elastic

  • Larger codebase and dependencies, potentially increasing build times
  • May have a steeper learning curve for beginners
  • Not officially maintained by Elastic, which could lead to compatibility issues

Code Comparison

elastic:

client, err := elastic.NewClient()
result, err := client.Index().
    Index("tweets").
    Type("doc").
    Id("1").
    BodyJson(tweet).
    Do(context.Background())

go-elasticsearch:

es, err := elasticsearch.NewDefaultClient()
res, err := es.Index(
    "tweets",
    strings.NewReader(`{"message":"Hello World"}`),
    es.Index.WithDocumentID("1"),
)

The elastic library provides a more fluent API with method chaining, while go-elasticsearch uses a more low-level approach with separate function calls. The elastic library abstracts away some of the complexity, making it easier to use for common operations. However, go-elasticsearch offers more fine-grained control over requests and is closer to the Elasticsearch REST API.

Both libraries have their strengths, and the choice between them depends on specific project requirements, familiarity with Elasticsearch, and desired level of abstraction.

9,971

A modern text/numeric/geo-spatial/vector indexing library for go

Pros of Bleve

  • Pure Go implementation, making it easier to embed and deploy in Go applications
  • Supports a wider range of storage backends, including in-memory and custom options
  • More flexible and customizable for specific use cases

Cons of Bleve

  • Generally slower performance compared to Elasticsearch for large-scale operations
  • Less mature ecosystem and smaller community support
  • Fewer advanced features out-of-the-box, such as machine learning capabilities

Code Comparison

Bleve:

index, _ := bleve.New("example.bleve", mapping)
doc := struct{
    ID   string
    Name string
}{"1", "John Doe"}
index.Index("doc1", doc)

go-elasticsearch:

es, _ := elasticsearch.NewDefaultClient()
doc := map[string]interface{}{
    "name": "John Doe",
}
_, _ = es.Index("test-index", strings.NewReader(`{"name":"John Doe"}`), es.Index.WithDocumentID("1"))

Both libraries provide ways to create indexes and add documents, but Bleve's API is more Go-idiomatic, while go-elasticsearch follows Elasticsearch's REST API structure more closely. Bleve allows for direct indexing of Go structs, whereas go-elasticsearch typically works with JSON data.

Official Python client for Elasticsearch

Pros of elasticsearch-py

  • Written in Python, which is widely used in data science and analytics
  • More mature and feature-rich, with a longer development history
  • Extensive documentation and community support

Cons of elasticsearch-py

  • Generally slower performance compared to Go implementations
  • Python's Global Interpreter Lock (GIL) can limit concurrency in multi-threaded applications

Code Comparison

elasticsearch-py:

from elasticsearch import Elasticsearch

es = Elasticsearch(["http://localhost:9200"])
result = es.search(index="my-index", body={"query": {"match_all": {}}})

go-elasticsearch:

es, _ := elasticsearch.NewDefaultClient()
res, _ := es.Search(
    es.Search.WithIndex("my-index"),
    es.Search.WithBody(strings.NewReader(`{"query":{"match_all":{}}}`)),
)

Summary

Both elasticsearch-py and go-elasticsearch are official Elasticsearch clients, each with its strengths. elasticsearch-py is more established and benefits from Python's ecosystem, making it ideal for data analysis tasks. go-elasticsearch, while newer, offers better performance and concurrency, making it suitable for high-throughput applications. The choice between them often depends on the specific project requirements and the development team's expertise.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

go-elasticsearch

The official Go client for Elasticsearch.

Download the latest version of Elasticsearch or sign-up for a free trial of Elastic Cloud.

Go Reference Go Report Card codecov.io Build Unit Integration API

Compatibility

Go

Starting from version 8.12.0, this library follow the Go language policy. Each major Go release is supported until there are two newer major releases. For example, Go 1.5 was supported until the Go 1.7 release, and Go 1.6 was supported until the Go 1.8 release.

Elasticsearch

Language clients are forward compatible; meaning that clients support communicating with greater or equal minor versions of Elasticsearch. Elasticsearch language clients are only backwards compatible with default distributions and without guarantees made.

When using Go modules, include the version in the import path, and specify either an explicit version or a branch:

require github.com/elastic/go-elasticsearch/v8 v8.0.0
require github.com/elastic/go-elasticsearch/v7 7.17

It's possible to use multiple versions of the client in a single project:

// go.mod
github.com/elastic/go-elasticsearch/v7 v7.17.0
github.com/elastic/go-elasticsearch/v8 v8.0.0

// main.go
import (
  elasticsearch7 "github.com/elastic/go-elasticsearch/v7"
  elasticsearch8 "github.com/elastic/go-elasticsearch/v8"
)
// ...
es7, _ := elasticsearch7.NewDefaultClient()
es8, _ := elasticsearch8.NewDefaultClient()

The main branch of the client is compatible with the current master branch of Elasticsearch.

Installation

Refer to the Installation section of the getting started documentation.

Connecting

Refer to the Connecting section of the getting started documentation.

Operations

Helpers

The esutil package provides convenience helpers for working with the client. At the moment, it provides the esutil.JSONReader() and the esutil.BulkIndexer helpers.

Examples

The _examples folder contains a number of recipes and comprehensive examples to get you started with the client, including configuration and customization of the client, using a custom certificate authority (CA) for security (TLS), mocking the transport for unit tests, embedding the client in a custom type, building queries, performing requests individually and in bulk, and parsing the responses.

License

This software is licensed under the Apache 2 license. See NOTICE.