All Products
Search
Document Center

OpenSearch Management and Control SDK for Python

Last Updated: Sep 09, 2021

Download link

OpenSearch Management and Control SDK for Python: https://pypi.org/project/aliyun-python-sdk-opensearch

Source code of the SDK: https://github.com/aliyun/aliyun-openapi-python-sdk/tree/master/aliyun-python-sdk-opensearch

Procedure

To use OpenSearch Management and Control SDK for Python, perform the following steps:

Step 1: Create an Alibaba Cloud account

For more information, see Sign up with Alibaba Cloud. We recommend that you complete real-name verification at your earliest opportunity. Otherwise, you cannot use some Alibaba Cloud services.

Step 2: Obtain an Alibaba Cloud AccessKey pair

To use OpenSearch Management and Control SDK for Python, you must apply for an Alibaba Cloud AccessKey pair. Go to the Security Management page. Select an AccessKey pair for the SDK. If no AccessKey pair is available, create an AccessKey pair and make sure that it is in the Enable state. For more information about how to create an AccessKey pair, see Preparations.

Step 3: Install Alibaba Cloud SDK for Python

To install Alibaba Cloud SDK for Python, perform the following steps:

  1. Install the core library of Alibaba Cloud SDK for Python.

    • If you are using Python 2.X, run the following command:

pip install aliyun-python-sdk-core
  • If you are using Python 3.X, run the following command:

pip install aliyun-python-sdk-core-v3
  1. Install OpenSearch Management and Control SDK for Python.

To install OpenSearch Management and Control SDK for Python, run the following command:

pip install aliyun-python-sdk-opensearch

Step 4: Use OpenSearch Management and Control SDK for Python

This example shows how to use OpenSearch Management and Control SDK for Python to create and view a sort script.

  1. Create a client. When you create a client, you must provide the region ID, AccessKey ID, and AccessKey secret.

  2. Configure the endpoint.

  3. Create a request and configure parameters.

  4. Send the request and handle the response or exception.

#!/bin/env python
import sys
import json
import time
import base64

from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkopensearch.request.v20171225 import CreateSortScriptRequest
from aliyunsdkopensearch.request.v20171225 import SaveSortScriptFileRequest
from aliyunsdkopensearch.request.v20171225 import GetSortScriptRequest

# Create an AcsClient object.
client = AcsClient(
   "<your-access-key-id>", 
   "<your-access-key-secret>",
   "<your-region-id>"
)

# Configure the endpoint.
# Specify the region ID, service name, and endpoint.
client.add_endpoint("cn-hangzhou", "opensearch", "opensearch.cn-hangzhou.aliyuncs.com")

# Create a sort script and design the structure of creation requests.
content = {}
content["scriptName"] = "script1"
content["type"] = "cava_script"
content["scope"] = "second_rank"
contentStr = json.dumps(content)
createRequest = CreateSortScriptRequest.CreateSortScriptRequest()
createRequest.set_content_type("application/json")
createRequest.set_content(contentStr)
createRequest.set_appVersionId("123456")
createRequest.set_appGroupIdentity("appName")
try:
    response = client.do_action_with_exception(createRequest)
    print response
except ClientException as e:
    print e
except ServerException as e:
    print e

# Upload content of the sort script.
# Replace "your code" with the content of the sort script.
code = 'your code'
content = {}
content["version"] = int(time.time()*1000)
content["content"] = base64.b64encode(code)
contentStr = json.dumps(content)

# Design the structure of requests for updating the script code.
saveFileRequest = SaveSortScriptFileRequest.SaveSortScriptFileRequest()
saveFileRequest.set_content_type("application/json")
saveFileRequest.set_content(contentStr)
saveFileRequest.set_appVersionId("123456")
saveFileRequest.set_appGroupIdentity("appName")
try:
    response = client.do_action_with_exception(createRequest)
    print response
except ClientException as e:
    print e
except ServerException as e:
    print e

# View the sort script.
getRequest = GetSortScriptRequest.GetSortScriptRequest()
getRequest.set_content_type("application/json")
getRequest.set_appVersionId("123456")
getRequest.set_appGroupIdentity("appName")
getRequest.set_scriptName("script1")
try:
    response = client.do_action_with_exception(createRequest)
    print response
except ClientException as e:
    print e
except ServerException as e:
    print e