olivere/elastic

repository·release-branch.v7·Indexed 27 days ago

https://github.com/olivere/elastic

A Go client library for Elasticsearch providing a high-level API for indexing, searching, and cluster management. It supports a wide range of Document, Search, and Indices APIs, as well as comprehensive Query DSL, metrics, bucket, and pipeline aggregations. The library includes a ScrollService for iterator-like scrolling and provides versioned packages (e.g., v7 for Elasticsearch 7.x) to match specific Elasticsearch installations.

Tokens
46.6K
Snippets
50
Records
297
Agent score
92%

What's inside olivere/elastic

  1. Enforce context.Context in PerformRequest and Do

    release-branch.v7
    Starting with Elastic 5.0, all request execution methods require a context.Context. You must update all Do() calls to pass a context, e.g., Do(ctx). This enables automatic request cancellation and other concurrency patterns. If you do not have a specific context to use, you can pass context.TODO() or context.Background().
  2. Configure Basic Auth for the Elastic client

    release-branch.v7

    You can now pass HTTP Basic Auth credentials directly when creating a client using the elastic.SetBasicAuth option. This is useful for environments using Elasticsearch Shield.

    client, err := elastic.NewClient(elastic.SetBasicAuth("user", "secret"))
    if err != nil {
      log.Fatal(err)
    }
  3. Migrate from Elastic 2.0 to 3.0: Pointer Types

    release-branch.v7

    In Elastic 3.0, all types have changed from value types to pointer types. When calling methods that accept queries or filters, you no longer need to pass the address of the variable using the & operator. This simplifies the API and prevents subtle issues.

    Elastic 2.0 (Old):

    q := elastic.NewMatchAllQuery()
    res, err := elastic.Search("one").Query(&q).Do()

    Elastic 3.0 (New):

    q := elastic.NewMatchAllQuery()
    res, err := elastic.Search("one").Query(q).Do()
    // Or simplified:
    res, err := elastic.Search("one").Query(elastic.NewMatchAllQuery()).Do()
    q := elastic.NewMatchAllQuery()
    res, err := elastic.Search("one").Query(q).Do()
  4. Run the Elastic OpenTracing example

    release-branch.v7

    To demonstrate tracing with Elastic and OpenTelemetry/OpenTracing, you must first run a tracer (such as Jaeger) and then execute the tracing example.

    1. Start the tracer using the provided script:
      ./run-tracer.sh
    2. Open the tracer's web UI (typically Jaeger) to visualize traces:
      open http://localhost:16686
    3. Build and run the Go example with specific parameters for index, document type, count, and bulk size:
      go build
      ./tracing -index=test -type=doc -n=100000 -bulk-size=100
    go build
    ./tracing -index=test -type=doc -n=100000 -bulk-size=100
  5. Use the Script type for scripting operations

    release-branch.v7

    Starting with Elastic 3.0, you should use the Script type for all scripting operations (e.g., in aggregations or updates) instead of passing raw strings and parameters directly to the service methods. Use elastic.NewScript() to construct your script.

    Elastic 3.0 usage:

    update, err := client.Update().Index("twitter").Type("tweet").Id("1").
    	Script(elastic.NewScript("ctx._source.retweets += num").Param("num", 1)).
    	Upsert(map[string]interface{}{"retweets": 0}).
    	Do()