All Products
Search
Document Center

OpenSearch:Data push demo

Last Updated:Apr 29, 2025

This topic describes the data push demo that shows how to push data. The demo dynamically encapsulates document data into Map objects and calls the add() method to add these Map objects to the cache. Then, the demo calls the pushDocuments() method to submit the document data in these Map objects at a time.

Sample code

package main

import (
	"fmt"
	util "github.com/alibabacloud-go/tea-utils/v2/service"
	"github.com/alibabacloud-go/tea/tea"
	ha3engine "github.com/aliyun/alibabacloud-ha3-go-sdk/client"
)

func main() {
	// Create a client instance for sending requests.
	config := &ha3engine.Config{
		// The API endpoint of the instance. You can view the API endpoint in the API Endpoint section of the Instance Details page.
		Endpoint: tea.String("ha-cn-i7*****605.public.ha.aliyuncs.com"),
		// The username. You can view the username in the API Endpoint section of the Instance Details page.
		AccessUserName: tea.String("username"),
		// The password. You can modify the password in the API Endpoint section of the Instance Details page.
		AccessPassWord: tea.String("password"),
	}

	// Initialize a client for sending requests.
	client, _clientErr := ha3engine.NewClient(config)

	// If the request takes an extended period of time to complete, you can configure this parameter to increase the amount of wait time for the request. Unit: milliseconds.
	runtime := &util.RuntimeOptions{
		ConnectTimeout: tea.Int(5000),
		ReadTimeout:    tea.Int(10000),
		Autoretry:      tea.Bool(false),
		IgnoreSSL:      tea.Bool(false),
		MaxIdleConns:   tea.Int(50),
	}
	client.RuntimeOptions = runtime

	// If an error occurs when the system creates the client, _clientErr and an error message are returned.
	if _clientErr != nil {
		fmt.Println(_clientErr)
		return
	}

	docPush(client)
}

func docPush(client *ha3engine.Client) {
	pushDocumentsRequestModel := &ha3engine.PushDocumentsRequestModel{}
	// The table name of the document whose data is to be pushed.
	tableName := "<table_name>"
	// The primary key field of the document whose data is to be pushed.
	keyField := "<field_pk>"

	array := []map[string]interface{}{}
	filed := map[string]interface{}{
		"fields": map[string]interface{}{
			"id": 1,
			"title":  "test",
		},
		"cmd": tea.String("add"),
	}
	array = append(array, filed)
	pushDocumentsRequestModel.SetBody(array)

	// Call the method for sending a request.
	response, _requestErr := client.PushDocuments(tea.String(tableName), tea.String(keyField), pushDocumentsRequestModel)
	// By default, whether the primary key field exists is checked when data is pushed. To disable the check, set the request header X-Opensearch-Validate-Data to false.
	//headers := map[string]*string{}
	//headers["X-Opensearch-Validate-Data"] = tea.String("false")
	//pushDocumentsRequestModel.SetHeaders(headers)
	
	// If an error occurs when the system sends the request, _requestErr and an error message are returned.
	if _requestErr != nil {
		fmt.Println(_requestErr)
		return
	}

	// Display the response if no error occurs.
	fmt.Println(response)
}