### MatchPhraseQuery Example Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/types.md Demonstrates how to construct a MatchPhraseQuery to find documents containing the exact phrase 'opensearch guide'. ```go q := opensearch.NewMatchPhraseQuery("title", "opensearch guide") ``` -------------------------------- ### Build and Run Tracing Example Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/recipes/tracing/otel/README.md Compile and execute the Go tracing example. This command demonstrates how to pass parameters for index, type, number of documents, and bulk size. ```sh go build ./tracing -index=test -type=doc -n=100000 -bulk-size=100 ``` -------------------------------- ### Example Usage of Script Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/types.md Demonstrates creating a script with a source and a parameter. ```go script := opensearch.NewScript("doc['price'].value * params.multiplier"). Param("multiplier", 1.2) ``` -------------------------------- ### MatchQuery Example Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/types.md Demonstrates how to construct and configure a MatchQuery with specific operator and fuzziness settings. ```go q := opensearch.NewMatchQuery("title", "opensearch"). Operator("and"). Fuzziness("AUTO") ``` -------------------------------- ### Example: Update with Script Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/api-reference/index-management.md Shows how to perform an update using a Painless script. This is useful for more complex logic, like incrementing a value. ```go // Update with script res, err := client.Update(). Index("users"). Id("1"). Script(opensearch.NewScript("ctx._source.age += params.increment"). Param("increment", 1)). Do(context.Background()) ``` -------------------------------- ### Example: Creating a Bulk Delete Request Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/api-reference/bulk.md Demonstrates how to create a new BulkDeleteRequest and add it to a bulk operation. ```go req := opensearch.NewBulkDeleteRequest(). Index("users"). Id("1") bulk := client.Bulk().Add(req) ``` -------------------------------- ### Example: Update with RetryOnConflict Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/api-reference/index-management.md Demonstrates how to use 'RetryOnConflict' to handle potential version conflicts during an update. This is crucial for concurrent updates. ```go res, err := client.Update(). Index("users"). Id("1"). RetryOnConflict(3). Doc(update). Do(context.Background()) ``` -------------------------------- ### Build a Search Query Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/INDEX.md Construct a search query using the query builder pattern. This example demonstrates creating a match query and an aggregation. ```go ss := opensearch.NewSearchSource(). Query(opensearch.NewMatchQuery("title", "opensearch")), Aggregation("status", opensearch.NewTermsAggregation().Field("status")), Size(20) results, err := client.Search("articles").SearchSource(ss).Do(ctx) ``` -------------------------------- ### Start Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/api-reference/client.md Starts the client's background goroutines for health checking and node sniffing. This is automatically called by `NewClient` but can be called explicitly if sniffing or health checks were initially disabled or after the client was stopped. ```APIDOC ## Start ### Description Starts the client's background goroutines for health checking and node sniffing. Automatically called by `NewClient`. Should only be called explicitly if the client was created with sniffing or health checks disabled, or after the client was stopped. ### Method Signature ```go func (c *Client) Start() ``` ### Example ```go client, _ := opensearch.NewSimpleClient() client.Start() // Start background processes defer client.Stop() ``` ``` -------------------------------- ### Manage OpenSearch Indices Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/README.md Provides examples for creating a new index with specific settings, checking if an index exists, and deleting an index. ```go // Create index res, err := client.CreateIndex("users"). BodyJson(map[string]interface{}{ "settings": map[string]interface{}{ "number_of_shards": 5, "number_of_replicas": 1, }, }). Do(context.Background()) // Check if exists exists, err := client.IndexExists("users").Do(context.Background()) // Delete index res, err := client.DeleteIndex("users").Do(context.Background()) ``` -------------------------------- ### Configuration and Options Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/README.md A complete configuration guide for the OpenSearch Go client, covering client initialization options, connection settings, retry strategies, and advanced HTTP configuration. ```APIDOC ## Configuration and Options ### Description This section outlines the configuration options available for the OpenSearch Go client. It covers how to initialize the client with various settings, including connection details, authentication, and advanced HTTP configurations. ### Configuration Details - **Client initialization options**: - Connection options (SetURL, SetBasicAuth, SetHttpClient, SetScheme) - Health check options - Node sniffing options - Retry and backoff options - Compression options - Logging and debugging - Request customization - Plugin requirements - **Configuration struct** (config.Config) - config.Parse() for URL parsing - Environment variables - Advanced HTTP configuration (TLS, AWS Signature) - Common configuration patterns ``` -------------------------------- ### Basic Document Indexing Example Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/api-reference/index-management.md Demonstrates how to add a document to an index using the IndexService. It specifies the index, document ID, and the document content as a JSON object. The version of the created document is printed upon success. ```go user := map[string]interface{}{ "name": "John Doe", "email": "john@example.com", "age": 30, "active": true, } res, err := client.Index(). Index("users"). Id("1"). BodyJson(user). Do(context.Background()) if err != nil { log.Fatal(err) } fmt.Printf("Version: %d\n", res.Version) ``` -------------------------------- ### Bulk Create Request Example Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/api-reference/bulk.md Use this to create a document. It will fail if a document with the same ID already exists. Requires the OpenSearch client and a BulkableRequest. ```go req := opensearch.NewBulkCreateRequest(). Index("users"). Id("1"). Doc(user) bulk := client.Bulk().Add(req) ``` -------------------------------- ### MultiMatchQuery Example Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/types.md Demonstrates constructing a MultiMatchQuery to search for 'opensearch' across 'title', 'body', and 'tags' fields, specifying the 'best_fields' match type. ```go q := opensearch.NewMultiMatchQuery("opensearch", "title", "body", "tags"). Type("best_fields") ``` -------------------------------- ### Start OpenSearch Client Goroutines Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/api-reference/client.md Starts the client's background goroutines for health checking and node sniffing. This is automatically called by NewClient. Use explicitly if sniffing or health checks were disabled during creation or after stopping the client. ```go func (c *Client) Start() ``` ```go client, _ := opensearch.NewSimpleClient() client.Start() // Start background processes defer client.Stop() ``` -------------------------------- ### Example: Performing a Bulk Operation and Processing Response Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/api-reference/bulk.md Shows how to add multiple index and delete requests to a bulk operation, execute it, and then process the response, checking for errors and individual item results. ```go res, err := client.Bulk(). Add(opensearch.NewBulkIndexRequest().Index("users").Id("1").Doc(user1)). Add(opensearch.NewBulkIndexRequest().Index("users").Id("2").Doc(user2)). Add(opensearch.NewBulkDeleteRequest().Index("users").Id("3")), Do(context.Background()) if err != nil { log.Fatal(err) } fmt.Printf("Bulk operation took %dms\n", res.Took) fmt.Printf("Had errors: %v\n", res.Errors) for i, item := range res.Items { // Each item is a map with operation name as key for opName, result := range item { if result.Error != nil { fmt.Printf("Item %d operation '%s' failed: %s\n", i, opName, result.Error.Reason) } else { fmt.Printf("Item %d operation '%s' succeeded (status %d)\n", i, opName, result.Status) } } } ``` -------------------------------- ### Example: Partial Update with Doc Merge Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/api-reference/index-management.md Demonstrates a partial update using a document fragment. This is suitable for simple field modifications. ```go // Partial update with doc merge res, err := client.Update(). Index("users"). Id("1"). Doc(map[string]interface{}{ "age": 31, "updated_at": time.Now(), }). Do(context.Background()) ``` -------------------------------- ### QueryStringQuery Example Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/types.md Demonstrates constructing a QueryStringQuery using Lucene syntax to search for 'opensearch' or 'elastic' in the 'title' field. ```go q := opensearch.NewQueryStringQuery("(opensearch OR elastic) AND guide"). DefaultField("title") ``` -------------------------------- ### Write Unit Tests in Go Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/CONTRIBUTING.md Example of a unit test using the OpenSearch client and test helper. Ensure to use context.TODO() for API calls in tests. ```go package opensearch import ( "context" "testing" ) func TestMyFeature(t *testing.T) { client := setupTestClientAndCreateIndex(t) res, err := client.MyFeature().Do(context.TODO()) if err != nil { t.Fatal(err) } if res == nil { t.Errorf("expected response, got nil") } } ``` -------------------------------- ### Bulk Index Request Example Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/api-reference/bulk.md Use this to add or update a document. It creates the document if it doesn't exist or overwrites it if it does. Requires the OpenSearch client and a BulkableRequest. ```go req := opensearch.NewBulkIndexRequest(). Index("users"). Id("1"). Doc(map[string]interface{}{ "name": "John Doe", "email": "john@example.com", "age": 30, }) bulk := client.Bulk().Add(req) ``` -------------------------------- ### Configure Custom HTTP Transport with TLS Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/configuration.md This example demonstrates setting up a custom HTTP transport with TLS configuration, including loading a CA certificate and specifying TLS version. Use this when you need to connect to an OpenSearch instance with custom TLS settings. ```go import ( "crypto/tls" "crypto/x509" "os" ) // Load CA certificate caCert, _ := os.ReadFile("ca-cert.pem") caCertPool := x509.NewCertPool() caCertPool.AppendCertsFromPEM(caCert) // Configure TLS tlsConfig := &tls.Config{ RootCAs: caCertPool, MinVersion: tls.VersionTLS12, } transport := &http.Transport{ TLSClientConfig: tlsConfig, MaxIdleConns: 100, MaxConnsPerHost: 10, } httpClient := &http.Client{ Transport: transport, Timeout: 30 * time.Second, } client, err := opensearch.NewClient( opensearch.SetHttpClient(httpClient), opensearch.SetURL("https://opensearch.example.com:9200"), ) ``` -------------------------------- ### Basic Bulk Operations Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/api-reference/bulk.md This example demonstrates how to perform basic bulk indexing operations. It adds 100 documents to the 'users' index and then executes the bulk request. ```APIDOC ## Basic Bulk Operations This example demonstrates how to perform basic bulk indexing operations. It adds 100 documents to the 'users' index and then executes the bulk request. ```go bulk := client.Bulk() for i := 1; i <= 100; i++ { user := map[string]interface{}{ "name": fmt.Sprintf("User %d", i), "email": fmt.Sprintf("user%d@example.com", i), } bulk.Add(opensearch.NewBulkIndexRequest(). Index("users"). Id(fmt.Sprintf("%d", i)). Doc(user)) } res, err := bulk.Do(context.Background()) if err != nil { log.Fatal(err) } if res.Errors { for _, item := range res.Items { for _, result := range item { if result.Error != nil { fmt.Printf("Error: %s\n", result.Error.Reason) } } } } ``` ``` -------------------------------- ### Validate Required Plugins During Initialization Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/configuration.md Ensure that specific OpenSearch plugins are installed on all nodes before the client is fully initialized. The client initialization will fail if any of the specified plugins are missing. ```go client, err := opensearch.NewClient( opensearch.SetRequiredPlugins("analysis-phonetic", "mapper-murmur3"), ) if err != nil { log.Fatal("Required plugins not found") } ``` -------------------------------- ### Run Jaeger Tracer Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/recipes/tracing/otel/README.md Start the Jaeger tracer using the provided script. This is a prerequisite for observing traces. ```sh ./run-tracer.sh ``` -------------------------------- ### Example: Upsert Operation Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/api-reference/index-management.md Illustrates an upsert operation, where the document is updated if it exists, or inserted with the provided 'Upsert' document if it does not. ```go // Upsert (update or insert) res, err := client.Update(). Index("users"). Id("1"). Doc(map[string]interface{}{"name": "John"}). Upsert(map[string]interface{}{"name": "John", "age": 30}). Do(context.Background()) ``` -------------------------------- ### Configure Serverless/Lambda Client Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/configuration.md Configure the client for serverless environments like AWS Lambda, using environment variables for URL and credentials. No explicit `Start()` or `Stop()` calls are needed. ```go client, err := opensearch.NewSimpleClient( opensearch.SetURL(os.Getenv("OPENSEARCH_URL")), opensearch.SetBasicAuth( os.Getenv("OPENSEARCH_USER"), os.Getenv("OPENSEARCH_PASS"), ), ) // No need to call Start() or Stop() ``` -------------------------------- ### Get and Put Index Mapping in Go Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/api-reference/index-management.md Use these methods to retrieve the current mapping of an index or to define/update the mapping for an index. Ensure the client is initialized before use. ```go type IndicesGetMappingService struct { client *Client indices []string types []string } type IndicesPutMappingService struct { client *Client index string typ string body interface{} } ``` ```go // Get mapping mapping, err := client.GetMapping().Index("users"). Do(context.Background()) // Put mapping res, err := client.PutMapping(). Index("users"). BodyJson(map[string]interface{}{ "properties": map[string]interface{}{ "name": map[string]interface{}{ "type": "text", "analyzer": "standard", }, "email": map[string]interface{}{ "type": "keyword", }, "age": map[string]interface{}{ "type": "integer", }, "address": map[string]interface{}{ "type": "object", "properties": map[string]interface{}{ "street": map[string]interface{}{"type": "text"}, "city": map[string]interface{}{"type": "keyword"}, }, }, }, }). Do(context.Background()) ``` -------------------------------- ### Bulk Processing with BulkProcessor Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/README.md Details the setup and usage of the BulkProcessor for efficient bulk indexing operations. It supports automatic batching, configurable workers, and flush intervals. ```go // Automatic batching with BulkProcessor processor, err := client.BulkProcessor(). NumWorkers(4). BulkActions(1000). FlushInterval(30 * time.Second). Do(context.Background()) for _, doc := range documents { processor.Add(opensearch.NewBulkIndexRequest(). Index("users"). Doc(doc)) } processor.Flush() processor.Close() ``` -------------------------------- ### Bulk Update Request Examples Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/api-reference/bulk.md Use this to partially update a document. You can either merge a document or use a script for arbitrary logic. Requires the OpenSearch client and a BulkableRequest. ```go // Update with doc merge req := opensearch.NewBulkUpdateRequest(). Index("users"). Id("1"). Doc(map[string]interface{}{"age": 31}) // Update with script req := opensearch.NewBulkUpdateRequest(). Index("users"). Id("1"). Script(opensearch.NewScript("ctx._source.age += params.inc").Param("inc", 1)) bulk := client.Bulk().Add(req) ``` -------------------------------- ### Configure Pagination for SearchSource Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/api-reference/search.md Control the offset and number of results returned using the From and Size methods. From sets the starting point, and Size determines how many documents to retrieve. ```go func (s *SearchSource) From(from int) *SearchSource ``` ```go func (s *SearchSource) Size(size int) *SearchSource ``` ```go ss := opensearch.NewSearchSource(). From(20). Size(10) // Returns results 20-29 ``` -------------------------------- ### Get and Put Index Settings in Go Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/api-reference/index-management.md These methods allow you to retrieve the current settings for an index or update settings such as the number of replicas or refresh interval. The client must be properly configured. ```go type IndicesPutSettingsService struct { client *Client indices []string body interface{} preserveExisting *bool } type IndicesGetSettingsService struct { client *Client indices []string } ``` ```go // Get settings settings, err := client.IndexGetSettings("users"). Do(context.Background()) // Update settings res, err := client.IndexPutSettings(). Index("users"). BodyJson(map[string]interface{}{ "settings": map[string]interface{}{ "number_of_replicas": 2, "refresh_interval": "30s", "index.max_result_window": 50000, }, }). Do(context.Background()) ``` -------------------------------- ### Go: Retrieve a Document by ID Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/api-reference/index-management.md Example of retrieving a single document using the GetService. It demonstrates setting the index and ID, executing the request, and handling potential 'document not found' errors. ```go res, err := client.Get(). Index("users"). Id("1"). Do(context.Background()) if err != nil { if opensearch.IsNotFound(err) { fmt.Println("Document not found") } log.Fatal(err) } var user map[string]interface{} er = json.Unmarshal(res.Source, &user) fmt.Printf("Found user: %v\n", user) ``` -------------------------------- ### Go: Retrieve Specific Fields and Control Source Inclusion Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/api-reference/index-management.md Example demonstrating how to retrieve only specific fields ('name', 'email') and control source inclusion using FetchSourceContext. This is useful for targeted data retrieval. ```go res, err := client.Get(). Index("users"). Id("1"). FetchSourceContext(opensearch.NewFetchSourceContext(true). Include("name", "email") ). Do(context.Background()) ``` -------------------------------- ### Get Node Information and Statistics Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/api-reference/client.md Creates services to retrieve information and statistics about cluster nodes. Use these to inspect individual node configurations and performance metrics. ```go func (c *Client) NodesInfo() *NodesInfoService func (c *Client) NodesStats() *NodesStatsService ``` ```go info, err := client.NodesInfo().Do(context.Background()) stats, err := client.NodesStats().Do(context.Background()) ``` -------------------------------- ### Get Index Statistics Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/api-reference/client.md Creates a service to retrieve statistics for indices, such as document count and storage size. Use this to monitor index health and resource usage. ```go stats, err := client.IndexStats("users").Do(context.Background()) ``` -------------------------------- ### Configure GET Request with Body Method Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/configuration.md Specify the HTTP method (GET, POST, or HEAD) to use when sending a GET request that includes a body. This is a non-standard behavior often used to work around proxy limitations. ```go client, err := opensearch.NewClient( opensearch.SetSendGetBodyAs("POST"), // Some proxies don't allow GET with body ) ``` -------------------------------- ### Bulk with Mixed Operations Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/api-reference/bulk.md This example shows how to perform a bulk request that includes a mix of operations: indexing new documents, creating documents if they don't exist, updating existing documents, and deleting documents. ```APIDOC ## Bulk with Mixed Operations This example shows how to perform a bulk request that includes a mix of operations: indexing new documents, creating documents if they don't exist, updating existing documents, and deleting documents. ```go bulk := client.Bulk().Index("users") // Index new documents bulk.Add(opensearch.NewBulkIndexRequest().Id("1").Doc(user1)) bulk.Add(opensearch.NewBulkIndexRequest().Id("2").Doc(user2)) // Create if not exists bulk.Add(opensearch.NewBulkCreateRequest().Id("3").Doc(user3)) // Update existing bulk.Add(opensearch.NewBulkUpdateRequest().Id("1").Doc(map[string]interface{}{"age": 31})) // Delete bulk.Add(opensearch.NewBulkDeleteRequest().Id("99")) res, err := bulk.Do(context.Background()) ``` ``` -------------------------------- ### Get Cluster Statistics Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/api-reference/client.md Creates a service to get aggregate statistics about the cluster. This is useful for monitoring and performance analysis. ```go func (c *Client) ClusterStats() *ClusterStatsService ``` ```go stats, err := client.ClusterStats().Do(context.Background()) ``` -------------------------------- ### Get Search Shards Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/api-reference/client.md Retrieve information about which shards a search request will execute on. Specify the index names for which to get shard information. ```go shards, err := client.SearchShards("users").Do(context.Background()) ``` -------------------------------- ### Configure and Use BulkProcessorService Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/api-reference/bulk.md Demonstrates how to configure and use the BulkProcessorService for batching bulk operations. It shows setting various parameters, adding requests, and managing the processor's lifecycle. ```go processor, err := client.BulkProcessor(). Name("myprocessor"). NumWorkers(4). BulkActions(500). BulkSize(5 * 1024 * 1024). FlushInterval(30 * time.Second). BeforeFunc(func(id int64, req *opensearch.BulkRequest) error { fmt.Printf("Executing bulk request %d\n", id) return nil }). AfterFunc(func(id int64, req *opensearch.BulkRequest, res *opensearch.BulkResponse, err error) error { if err != nil { fmt.Printf("Bulk request %d failed: %v\n", id, err) } return nil }). Do(context.Background()) if err != nil { log.Fatal(err) } // Add requests processor.Add(opensearch.NewBulkIndexRequest().Index("users").Doc(user1)) processor.Add(opensearch.NewBulkIndexRequest().Index("users").Doc(user2)) // Manually flush processor.Flush() // Close and wait for pending requests processor.Close() ``` -------------------------------- ### SetSendGetBodyAs Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/configuration.md Configures the HTTP method used when sending a GET request that includes a body. This is a non-standard behavior, with "GET" as the default. ```APIDOC ## SetSendGetBodyAs ### Description HTTP method to use when sending a GET request with a body (non-standard). ### Method `SetSendGetBodyAs` ### Parameters #### Parameters - **method** (string) - "GET", "POST", or "HEAD" ### Request Example ```go client, err := opensearch.NewClient( opensearch.SetSendGetBodyAs("POST"), // Some proxies don't allow GET with body ) ``` ``` -------------------------------- ### Bulk with Conditional Routing Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/api-reference/bulk.md This example shows how to use conditional routing in bulk operations, which is useful for sharding data based on specific criteria like a user ID. Each document is routed to its corresponding shard. ```Go bulk := client.Bulk() for _, doc := range documents { req := opensearch.NewBulkIndexRequest(). Index("users"). Id(doc.ID). Routing(doc.UserID). // Route by user for sharding Doc(doc) bulk.Add(req) } res, err := bulk.Do(context.Background()) ``` -------------------------------- ### Configure Development Client Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/configuration.md Use `NewSimpleClient` for development and testing environments, typically pointing to a local OpenSearch instance. ```go client, err := opensearch.NewSimpleClient( opensearch.SetURL("http://localhost:9200"), ) ``` -------------------------------- ### Constructor for Highlight Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/types.md Creates a new Highlight instance. ```go func NewHighlight() *Highlight ``` -------------------------------- ### FieldCaps Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/api-reference/client.md Creates a service to get capabilities of fields across indices. ```APIDOC ## FieldCaps ### Description Creates a service to get capabilities of fields across indices. ### Method Not applicable (SDK method) ### Signature ```go func (c *Client) FieldCaps(indices ...string) *FieldCapsService ``` ### Parameters #### Path Parameters - **indices** (string) - Required - Index names ### Request Example ```go caps, err := client.FieldCaps("users", "products").Do(context.Background()) ``` ### Response #### Success Response Returns a `*FieldCapsService` object that can be used to further configure and execute the field capabilities request. #### Response Example (Response details depend on the `Do` method execution) ``` -------------------------------- ### Go: Delete a Document by ID with Refresh Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/api-reference/index-management.md Example of deleting a document by its ID. It shows how to set the index, ID, and specify 'true' for refresh to ensure the deletion is immediately visible. It also handles cases where the document might already be gone. ```go res, err := client.Delete(). Index("users"). Id("1"). Refresh("true"). Do(context.Background()) if err != nil { if opensearch.IsNotFound(err) { fmt.Println("Document already gone") } log.Fatal(err) } fmt.Printf("Result: %s\n", res.Result) // "deleted" or "not_found" ``` -------------------------------- ### Basic Service Pattern Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/README.md Demonstrates the fluent service pattern for building and executing search requests. Services are builders that collect configuration before execution. ```go // Services are builders that collect configuration service := client.Search("myindex").Query(q).Size(10) // Do() executes the request result, err := service.Do(context.Background()) ``` -------------------------------- ### Get Service Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/api-reference/client.md Creates a new GetService for retrieving a single document by ID. ```APIDOC ## Get ### Description Creates a new `GetService` for retrieving a single document by ID. ### Method Signature ```go func (c *Client) Get() *GetService ``` ### Example ```go res, err := client.Get().Index("users").Id("1").Do(context.Background()) if err != nil { if opensearch.IsNotFound(err) { fmt.Println("Document not found") } } ``` ``` -------------------------------- ### ClusterStats Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/api-reference/client.md Creates a service to get aggregate statistics about the cluster, such as disk usage and indexing rates. ```APIDOC ## ClusterStats ### Description Creates a service to get aggregate statistics about the cluster. ### Method This is a Go client method, not an HTTP method. ### Endpoint N/A (Client Method) ### Parameters N/A ### Request Example ```go stats, err := client.ClusterStats().Do(context.Background()) ``` ### Response #### Success Response - **Stats** (object) - An object containing aggregate statistics for the cluster. #### Response Example ```json { "cluster_name": "my-cluster", "nodes": { "count": { "total": 5, "successful": 5, "failed": 0 }, "versions": [ "..." ], "os": { "ref": { "name": "Linux", "version": "..." } }, "jvm": { "versions": [ { "version": "...", "bundled_jdk": true, "using_compressed_ordinary_object_pointers": true } ] }, "fs": { "total": "...", "free": "...", "available": "..." }, "process": { "refresh_interval_in_millis": 1000 } }, "indices": { "count": 10, "primaries": { "docs": { "count": 1000000, "deleted": 1000 }, "store": { "size_in_bytes": 1073741824 }, "indexing": { "index_total": 50000, "index_time_in_millis": 5000, "delete_total": 100, "delete_time_in_millis": 100, "throttle_total": 0, "throttle_time_in_millis": 0 }, "search": { "query_total": 100000, "query_time_in_millis": 10000, "fetch_total": 50000, "fetch_time_in_millis": 5000 }, "merge": { "total_operations": 1000, "total_time_in_millis": 1000 }, "refresh": { "total_operations": 500, "total_time_in_millis": 500 }, "flush": { "total_operations": 50, "total_time_in_millis": 50 }, "get": { "total": 1000000, "current": 0, "exists_total": 900000, "missing_total": 100000, "current_fetch": 0, "fetch_total": 50000 }, "index": { "total": 50000, "current": 0, "failed": 0 }, "bulk": { "total_operations": 0, "total_size_in_bytes": 0, "total_time_in_millis": 0, "avg_size_in_bytes": 0 }, "warmer": { "total_operations": 0, "total_time_in_millis": 0 } }, "search": { "open_contexts": 0, "query_total": 100000, "query_time_in_millis": 10000, "fetch_total": 50000, "fetch_time_in_millis": 5000, "scroll_total": 0, "scroll_time_in_millis": 0, "scroll_current": 0, "suggest_total": 0, "suggest_time_in_millis": 0 } } } ``` ``` -------------------------------- ### Constructor for Script Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/types.md Creates a new Script instance with the provided source code. ```go func NewScript(source string) *Script ``` -------------------------------- ### Get OpenSearch Cluster Version Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/api-reference/client.md Creates a service to retrieve the OpenSearch cluster version information. ```go func (c *Client) OpensearchVersion() *OpensearchVersionService ``` ```go vs := client.OpensearchVersion() info, err := vs.Do(context.Background()) // info contains version details ``` -------------------------------- ### Initialize OpenSearch Client from Configuration Object Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/api-reference/client.md Use NewClientFromConfig to initialize a client when connection details are already parsed into a config.Config object, often from external sources like configuration files or environment variables. ```go cfg, err := config.Parse("http://user:pass@localhost:9200/myindex?sniff=true") if err != nil { log.Fatal(err) } client, err := opensearch.NewClientFromConfig(cfg) if err != nil { log.Fatal(err) } ``` -------------------------------- ### Import OpenSearch Package with Configuration Utilities Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/INDEX.md Import both the main OpenSearch Go client package and its configuration utilities. This is useful when you need to manage client configuration separately. ```go import ( "github.com/disaster37/opensearch/v3" "github.com/disaster37/opensearch/v3/config" ) ``` -------------------------------- ### SetRequiredPlugins Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/configuration.md Validates that specified plugins are installed on all nodes. Initialization fails if any of the required plugins are missing. ```APIDOC ## SetRequiredPlugins ### Description Validates that specified plugins are installed on all nodes. Fails during initialization if missing. ### Method `SetRequiredPlugins` ### Parameters #### Parameters - **plugins** (...string) - Plugin names that must be available ### Request Example ```go client, err := opensearch.NewClient( opensearch.SetRequiredPlugins("analysis-phonetic", "mapper-murmur3"), ) if err != nil { log.Fatal("Required plugins not found") } ``` ``` -------------------------------- ### Construct and Execute Bulk Index/Update Requests Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/types.md Demonstrates how to construct and chain methods for bulk index and update requests. Use the Index, Id, and Doc methods to specify the target index, document ID, and document content. ```go req := opensearch.NewBulkIndexRequest(). Index("users"). Id("1"). Doc(map[string]interface{}{"name": "John"}) req := opensearch.NewBulkUpdateRequest(). Index("users"). Id("1"). Doc(map[string]interface{}{"age": 30}) ``` -------------------------------- ### Configure OpenSearch Client Options Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/INDEX.md Set various client options for connection, health checks, node discovery, retries, and features. Ensure necessary imports are included. ```go // Connection opensearch.SetURL(urls...) opensearch.SetBasicAuth(username, password) opensearch.SetHttpClient(httpClient) opensearch.SetScheme("https") // Health checks opensearch.SetHealthcheck(true) opensearch.SetHealthcheckInterval(60 * time.Second) opensearch.SetHealthcheckTimeout(1 * time.Second) opensearch.SetHealthcheckTimeoutStartup(5 * time.Second) // Node discovery opensearch.SetSniff(true) opensearch.SetSnifferInterval(15 * time.Minute) opensearch.SetSnifferTimeout(2 * time.Second) opensearch.SetSnifferTimeoutStartup(5 * time.Second) opensearch.SetSnifferCallback(callback) // Retries opensearch.SetRetrier(retrier) opensearch.SetRetryStatusCodes(429, 503) // Features opensearch.SetGzip(true) opensearch.SetSendGetBodyAs("POST") opensearch.SetLogger(logger) opensearch.SetDebugLog(writer) opensearch.SetHeaders(headers) opensearch.SetRequiredPlugins(names...) opensearch.SetMaxResponseBodySize(size) opensearch.SetDecoder(decoder) ``` -------------------------------- ### Offset-based Pagination Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/README.md Demonstrates basic offset-based pagination using the From and Size parameters for retrieving paginated results. ```go // Offset-based results, _ := client.Search("users").From(20).Size(10).Do(ctx) ``` -------------------------------- ### Debug a Specific Test with Dagger Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/CONTRIBUTING.md Start a Delve debugger for a specific test. Connect your IDE to the specified port. ```bash dagger call --src . debug-test --run TestMyFeature up ``` -------------------------------- ### ClusterHealth Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/api-reference/client.md Creates a service to get the health status of the cluster. This method returns a ClusterHealthService object that can be used to perform the health check. ```APIDOC ## ClusterHealth ### Description Creates a service to get the health status of the cluster. ### Method This is a Go client method, not an HTTP method. ### Endpoint N/A (Client Method) ### Parameters N/A ### Request Example ```go health, err := client.ClusterHealth().Do(context.Background()) fmt.Printf("Cluster status: %s\n", health.Status) // "green", "yellow", or "red" ``` ### Response #### Success Response - **Status** (string) - The health status of the cluster ('green', 'yellow', or 'red'). #### Response Example ```json { "status": "green" } ``` ``` -------------------------------- ### Get Field Capabilities Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/api-reference/client.md Obtain the capabilities of fields across multiple indices. Pass the names of the indices to query for field capabilities. ```go caps, err := client.FieldCaps("users", "products").Do(context.Background()) ``` -------------------------------- ### Create Long-Lived and Short-Lived OpenSearch Clients Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/README.md Demonstrates how to create a long-lived client with authentication and a short-lived client for specific use cases like Lambda functions. ```go import "github.com/disaster37/opensearch/v3" // Long-lived client (recommended) client, err := opensearch.NewClient( opensearch.SetURL("https://opensearch.example.com:9200"), opensearch.SetBasicAuth("admin", "password"), ) if err != nil { log.Fatal(err) } defer client.Stop() // Short-lived client (e.g., Lambda) client, err := opensearch.NewSimpleClient( opensearch.SetURL("https://opensearch.example.com:9200"), ) ``` -------------------------------- ### Get Cluster Health Status Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/api-reference/client.md Creates a service to retrieve the health status of the cluster. Use this to check if the cluster is green, yellow, or red. ```go func (c *Client) ClusterHealth() *ClusterHealthService ``` ```go health, err := client.ClusterHealth().Do(context.Background()) fmt.Printf("Cluster status: %s\n", health.Status) // "green", "yellow", or "red" ``` -------------------------------- ### Get Document by ID Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/api-reference/client.md Creates a new GetService for retrieving a single document by ID. This is useful for fetching a specific document when its ID is known. ```go res, err := client.Get().Index("users").Id("1").Do(context.Background()) if err != nil { if opensearch.IsNotFound(err) { fmt.Println("Document not found") } } ``` -------------------------------- ### Client Initialization and Public Methods Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/README.md Documentation for client initialization, lifecycle, and all public methods, including connection options, health checks, and various API groups like Document, Search, Index Management, Cluster Management, Template, Script, and low-level request execution. ```APIDOC ## Client Initialization and Public Methods ### Description This section details the initialization and lifecycle management of the OpenSearch Go client. It covers various connection options such as URL, authentication, and HTTP client configuration. It also includes information on health checks, node sniffing, and the execution of low-level requests. ### Public Methods Covered - Client initialization and lifecycle - Connection options (URL, auth, HTTP client) - Health checks and node sniffing - Document APIs (Index, Get, Update, Delete, Bulk) - Search APIs (Search, MultiSearch, Count, Scroll) - Index management (Create, Delete, Open, Close, Settings) - Cluster management - Template and script management - Low-level request execution ``` -------------------------------- ### Ping Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/api-reference/client.md Initiates a `PingService` to test connectivity with the OpenSearch cluster. This service sends a simple GET request to the cluster's root endpoint. ```APIDOC ## Ping ### Description Creates a new `PingService` to test connectivity to the OpenSearch cluster. The ping request is a simple GET request to the cluster root endpoint. ### Method Signature ```go func (c *Client) Ping() *PingService ``` ### Returns - `*PingService`: A service object used to perform the ping request. ### Example ```go ping := client.Ping() code, _, err := ping.Do(context.Background()) if err != nil { log.Fatal(err) } fmt.Printf("Ping returned status code: %d\n", code) ``` ``` -------------------------------- ### Ping OpenSearch Cluster Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/api-reference/client.md Creates a new PingService to test connectivity to the OpenSearch cluster by sending a GET request to the cluster root endpoint. ```go func (c *Client) Ping() *PingService ``` ```go ping := client.Ping() code, _, err := ping.Do(context.Background()) if err != nil { log.Fatal(err) } fmt.Printf("Ping returned status code: %d\n", code) ``` -------------------------------- ### Create OpenSearch Client with Default Settings Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/api-reference/client.md Use NewClient to create a long-lived client with default settings for persistent connections and background tasks like health checks and node discovery. Ensure to call Stop() when the client is no longer needed. ```go client, err := opensearch.NewClient() if err != nil { log.Fatal(err) } deffer client.Stop() ``` -------------------------------- ### IndexService Advanced Parameters Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/api-reference/index-management.md Shows how to use advanced parameters like OpType, Refresh, Pipeline, and WaitForActiveShards to control the indexing operation. OpType can be 'index' or 'create', Refresh controls searchability, Pipeline routes through ingest, and WaitForActiveShards ensures shard availability. ```go res, err := client.Index(). Index("users"). Id("1"). OpType("create"). // Fail if exists Refresh("true"). // Make immediately searchable Pipeline("my-pipeline"). BodyJson(user). Do(context.Background()) ``` -------------------------------- ### Get OpenSearch Field Mappings Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/api-reference/client.md Use this service to retrieve mapping information for specific fields within an index. Specify the index and the fields of interest. ```go mapping, err := client.GetFieldMapping().Index("users").Fields("name", "email").Do(context.Background()) ``` -------------------------------- ### Query Building with SearchSource Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/README.md Shows how to build a search query body separately using SearchSource and then execute it via the SearchService. This separates query construction from execution. ```go // SearchSource builds the query body ss := opensearch.NewSearchSource(). Query(opensearch.NewMatchQuery("title", "opensearch")), Aggregation("statuses", opensearch.NewTermsAggregation().Field("status"))), Size(20) // SearchService wraps it and executes results, err := client.Search("articles").SearchSource(ss).Do(ctx) ``` -------------------------------- ### Create a New SearchSource Builder Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/api-reference/search.md Instantiate a SearchSource builder to construct complex search requests. This builder supports queries, aggregations, sorting, and pagination. ```go func NewSearchSource() *SearchSource ``` ```go ss := opensearch.NewSearchSource() ss.Query(opensearch.NewMatchAllQuery()).Size(10) ``` -------------------------------- ### Implement Custom Retry Logic Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/errors.md Example of a custom retry function that retries on errors or 5xx status codes. It uses an exponential backoff strategy. ```go func MyRetrier(ctx context.Context, retry int, req *http.Request, res *http.Response, err error) (time.Duration, error) { if err != nil { return time.Second * time.Duration(retry), err // Retry } if res != nil && res.StatusCode >= 500 { return time.Second * time.Duration(retry), nil // Retry on 5xx } return 0, nil // Don't retry } client, _ := opensearch.NewClient( opensearch.SetRetrier(opensearch.NewBackoffRetrier(backoff.NewExponentialBackOff())), ) ``` -------------------------------- ### NewClientFromConfig Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/api-reference/client.md Initializes a client from a structured config.Config object. This is useful when you have parsed OpenSearch connection details from an external source (e.g., a configuration file or environment variable). ```APIDOC ## NewClientFromConfig ### Description Initializes a client from a structured `config.Config` object. This is useful when you have parsed OpenSearch connection details from an external source (e.g., a configuration file or environment variable). ### Signature ```go func NewClientFromConfig(cfg *config.Config) (*Client, error) ``` ### Parameters #### Path Parameters - **cfg** (*config.Config) - Description: Configuration object parsed from OpenSearch URL ### Returns - `(*Client, error)` ### Example ```go cfg, err := config.Parse("http://user:pass@localhost:9200/myindex?sniff=true") if err != nil { log.Fatal(err) } client, err := opensearch.NewClientFromConfig(cfg) if err != nil { log.Fatal(err) } ``` ``` -------------------------------- ### Get and Update Index Settings Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/api-reference/client.md Use IndexGetSettings to retrieve index settings and IndexPutSettings to modify them. The BodyJson method accepts a map for settings like number_of_replicas. ```go settings, err := client.IndexGetSettings("users").Do(context.Background()) ``` ```go res, err := client.IndexPutSettings(). Index("users"). BodyJson(map[string]interface{}{ "settings": map[string]interface{}{ "number_of_replicas": 2, }, }). Do(context.Background()) ``` -------------------------------- ### Enable Debugging Features Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/api-reference/search.md Use Explain, Version, and SeqNoAndPrimaryTerm to enable advanced debugging and metadata features in search results. Explain provides scoring details, Version includes the document version, and SeqNoAndPrimaryTerm provides sequence numbers for optimistic locking. ```go ss := opensearch.NewSearchSource(). Query(opensearch.NewMatchAllQuery()). Explain(true). Version(true) ``` -------------------------------- ### Run Full CI Pipeline with Dagger Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/CONTRIBUTING.md Execute the complete CI pipeline, including build, lint, format, test, and code coverage upload. ```bash dagger call --src . ci ``` -------------------------------- ### Get and Put OpenSearch Index Mappings Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/api-reference/client.md Use these services to retrieve or modify the field mappings for an index. Mappings define the data types and properties of fields. ```go // Get mapping mapping, err := client.GetMapping().Index("users").Do(context.Background()) // Put mapping res, err := client.PutMapping(). Index("users"). BodyJson(map[string]interface{}{ "properties": map[string]interface{}{ "name": map[string]interface{}{ "type": "text"}, }, }). Do(context.Background()) ``` -------------------------------- ### Constructor for FetchSourceContext Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/types.md Creates a new FetchSourceContext instance. ```go func NewFetchSourceContext(enable bool) *FetchSourceContext ``` -------------------------------- ### Search Documents with Filters and Sorting Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/README.md Illustrates how to search for documents in an index, applying filters, specifying the number of results, and sorting the output. ```go results, err := client.Search("users"). Query(opensearch.NewMatchQuery("name", "john")). Size(10). Sort("age", true). Do(context.Background()) for _, hit := range results.Hits.Hits { var user map[string]interface{} json.Unmarshal(hit.Source, &user) fmt.Println(user) } ``` -------------------------------- ### Index Management Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/README.md Comprehensive guide to index lifecycle management, including document CRUD operations, index creation/deletion, mapping and settings management, and maintenance operations. ```APIDOC ## Index Management ### Description This section details the management of index lifecycles within OpenSearch. It covers all aspects from creating and deleting indices to managing their mappings, settings, and performing maintenance tasks. ### Operations - Document CRUD operations (Index, Get, Update, Delete) - Index creation, deletion, opening, closing - Mapping management - Settings management - Maintenance operations (refresh, flush, merge) - Statistics and monitoring ``` -------------------------------- ### Create a Simple OpenSearch Client for Single Requests Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/api-reference/client.md Use NewSimpleClient for short-lived clients, suitable for serverless environments or per-request scenarios. This client disables background tasks and has a retry count of 1, optimizing for immediate use without persistent connections. ```go client, err := opensearch.NewSimpleClient( opensearch.SetURL("https://opensearch.example.com:9200"), ) if err != nil { log.Fatal(err) } // Use immediately; no need to call Start() or Stop() ``` -------------------------------- ### Get Cluster State Source: https://github.com/disaster37/opensearch/blob/release-branch.v3/_autodocs/api-reference/client.md Creates a service to retrieve the complete cluster state, including nodes, indices, and allocations. Use this for a comprehensive overview of the cluster's configuration. ```go func (c *Client) ClusterState() *ClusterStateService ``` ```go state, err := client.ClusterState().Do(context.Background()) ```