olivere/elastic
repository·release-branch.v7·Indexed 27 days ago
https://github.com/olivere/elasticA 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.
What's inside olivere/elastic
- Partial fields were removed in Elastic 3.0. Use source filtering to specify which fields should be returned in the search results.
Install Elastic v7 using Go modules
release-branch.v7If you are using Elasticsearch 7.x, use Go modules to manage the dependency. Import the
v7package in your Go files.import "github.com/olivere/elastic/v7"Get started with Elastic Client
release-branch.v7To use Elastic, you must first create aClient. By default, the client attempts to connect to Elasticsearch athttp://127.0.0.1:9200. You should typically create a single client instance for your entire application.Enforce context.Context in PerformRequest and Do
release-branch.v7Starting with Elastic 5.0, all request execution methods require acontext.Context. You must update allDo()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 passcontext.TODO()orcontext.Background().Configure Basic Auth for the Elastic client
release-branch.v7You can now pass HTTP Basic Auth credentials directly when creating a client using the
elastic.SetBasicAuthoption. This is useful for environments using Elasticsearch Shield.client, err := elastic.NewClient(elastic.SetBasicAuth("user", "secret")) if err != nil { log.Fatal(err) }Migrate from Filtered Query to Boolean Query
release-branch.v7In Elastic 3.0, theFiltered Queryhas been removed. To achieve the same functionality, you should migrate your code to use aBoolean Queryinstead.Migrate from 5.0 to 6.0: Use single type indices
release-branch.v7Starting with Elasticsearch 6.0, the engine defaults to single type indices. You can no longer use multiple types when performing operations such as adding an index with a mapping.Set up a tracer with Jaeger
release-branch.v7To trace your application using OpenTelemetry, you can run a local Jaeger instance. Use the provided script to start the tracer service:
./run-tracer.shOnce the service is running, you can access the Jaeger web UI to view traces at:
open http://localhost:16686Migrate from Elastic 2.0 to 3.0: Pointer Types
release-branch.v7In 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()Run the Elastic OpenTracing example
release-branch.v7To demonstrate tracing with Elastic and OpenTelemetry/OpenTracing, you must first run a tracer (such as Jaeger) and then execute the tracing example.
- Start the tracer using the provided script:
./run-tracer.sh - Open the tracer's web UI (typically Jaeger) to visualize traces:
open http://localhost:16686 - 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- Start the tracer using the provided script:
Replace LimitFilter with terminate_after
release-branch.v7TheLimitFilterwas removed in Elastic 3.0. To limit the number of documents processed or returned, use theterminate_afterparameter in your search request.Use the Script type for scripting operations
release-branch.v7Starting with Elastic 3.0, you should use the
Scripttype for all scripting operations (e.g., in aggregations or updates) instead of passing raw strings and parameters directly to the service methods. Useelastic.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()