$npx -y skills add cinience/alicloud-skills --skill aliyun-opensearch-searchUse when working with OpenSearch vector search edition via the Python SDK (ha3engine) to push documents and run HA/SQL searches. Ideal for RAG and vector retrieval pipelines in Claude Code/Codex.
| 1 | Category: provider |
| 2 | |
| 3 | # OpenSearch Vector Search Edition |
| 4 | |
| 5 | Use the ha3engine SDK to push documents and execute HA/SQL searches. This skill focuses on API/SDK usage only (no console steps). |
| 6 | |
| 7 | ## Prerequisites |
| 8 | |
| 9 | - Install SDK (recommended in a venv to avoid PEP 668 limits): |
| 10 | |
| 11 | ```bash |
| 12 | python3 -m venv .venv |
| 13 | . .venv/bin/activate |
| 14 | python -m pip install alibabacloud-ha3engine |
| 15 | ``` |
| 16 | - Provide connection config via environment variables: |
| 17 | - `OPENSEARCH_ENDPOINT` (API domain) |
| 18 | - `OPENSEARCH_INSTANCE_ID` |
| 19 | - `OPENSEARCH_USERNAME` |
| 20 | - `OPENSEARCH_PASSWORD` |
| 21 | - `OPENSEARCH_DATASOURCE` (data source name) |
| 22 | - `OPENSEARCH_PK_FIELD` (primary key field name) |
| 23 | |
| 24 | ## Quickstart (push + search) |
| 25 | |
| 26 | ```python |
| 27 | import os |
| 28 | from alibabacloud_ha3engine import models, client |
| 29 | from Tea.exceptions import TeaException, RetryError |
| 30 | |
| 31 | cfg = models.Config( |
| 32 | endpoint=os.getenv("OPENSEARCH_ENDPOINT"), |
| 33 | instance_id=os.getenv("OPENSEARCH_INSTANCE_ID"), |
| 34 | protocol="http", |
| 35 | access_user_name=os.getenv("OPENSEARCH_USERNAME"), |
| 36 | access_pass_word=os.getenv("OPENSEARCH_PASSWORD"), |
| 37 | ) |
| 38 | ha3 = client.Client(cfg) |
| 39 | |
| 40 | def push_docs(): |
| 41 | data_source = os.getenv("OPENSEARCH_DATASOURCE") |
| 42 | pk_field = os.getenv("OPENSEARCH_PK_FIELD", "id") |
| 43 | |
| 44 | documents = [ |
| 45 | {"fields": {"id": 1, "title": "hello", "content": "world"}, "cmd": "add"}, |
| 46 | {"fields": {"id": 2, "title": "faq", "content": "vector search"}, "cmd": "add"}, |
| 47 | ] |
| 48 | req = models.PushDocumentsRequestModel({}, documents) |
| 49 | return ha3.push_documents(data_source, pk_field, req) |
| 50 | |
| 51 | |
| 52 | def search_ha(): |
| 53 | # HA query example. Replace cluster/table names as needed. |
| 54 | query_str = ( |
| 55 | "config=hit:5,format:json,qrs_chain:search" |
| 56 | "&&query=title:hello" |
| 57 | "&&cluster=general" |
| 58 | ) |
| 59 | ha_query = models.SearchQuery(query=query_str) |
| 60 | req = models.SearchRequestModel({}, ha_query) |
| 61 | return ha3.search(req) |
| 62 | |
| 63 | try: |
| 64 | print(push_docs().body) |
| 65 | print(search_ha()) |
| 66 | except (TeaException, RetryError) as e: |
| 67 | print(e) |
| 68 | ``` |
| 69 | |
| 70 | ## Script quickstart |
| 71 | |
| 72 | ```bash |
| 73 | python skills/ai/search/aliyun-opensearch-search/scripts/quickstart.py |
| 74 | ``` |
| 75 | |
| 76 | Environment variables: |
| 77 | |
| 78 | - `OPENSEARCH_ENDPOINT` |
| 79 | - `OPENSEARCH_INSTANCE_ID` |
| 80 | - `OPENSEARCH_USERNAME` |
| 81 | - `OPENSEARCH_PASSWORD` |
| 82 | - `OPENSEARCH_DATASOURCE` |
| 83 | - `OPENSEARCH_PK_FIELD` (optional, default `id`) |
| 84 | - `OPENSEARCH_CLUSTER` (optional, default `general`) |
| 85 | |
| 86 | Optional args: `--cluster`, `--hit`, `--query`. |
| 87 | |
| 88 | ## SQL-style search |
| 89 | |
| 90 | ```python |
| 91 | from alibabacloud_ha3engine import models |
| 92 | |
| 93 | sql = "select * from <indexTableName>&&kvpair=trace:INFO;formatType:json" |
| 94 | sql_query = models.SearchQuery(sql=sql) |
| 95 | req = models.SearchRequestModel({}, sql_query) |
| 96 | resp = ha3.search(req) |
| 97 | print(resp) |
| 98 | ``` |
| 99 | |
| 100 | ## Notes for Claude Code/Codex |
| 101 | |
| 102 | - Use `push_documents` for add/delete updates. |
| 103 | - Large query strings (>30KB) should use the RESTful search API. |
| 104 | - HA queries are fast and flexible for vector + keyword retrieval; SQL is helpful for structured data. |
| 105 | |
| 106 | ## Error handling |
| 107 | |
| 108 | - Auth errors: verify username/password and instance access. |
| 109 | - 4xx on push: check schema fields and `pk_field` alignment. |
| 110 | - 5xx: retry with backoff. |
| 111 | |
| 112 | ## Validation |
| 113 | |
| 114 | ```bash |
| 115 | mkdir -p output/aliyun-opensearch-search |
| 116 | for f in skills/ai/search/aliyun-opensearch-search/scripts/*.py; do |
| 117 | python3 -m py_compile "$f" |
| 118 | done |
| 119 | echo "py_compile_ok" > output/aliyun-opensearch-search/validate.txt |
| 120 | ``` |
| 121 | |
| 122 | Pass criteria: command exits 0 and `output/aliyun-opensearch-search/validate.txt` is generated. |
| 123 | |
| 124 | ## Output And Evidence |
| 125 | |
| 126 | - Save artifacts, command outputs, and API response summaries under `output/aliyun-opensearch-search/`. |
| 127 | - Include key parameters (region/resource id/time range) in evidence files for reproducibility. |
| 128 | |
| 129 | ## Workflow |
| 130 | |
| 131 | 1) Confirm user intent, region, identifiers, and whether the operation is read-only or mutating. |
| 132 | 2) Run one minimal read-only query first to verify connectivity and permissions. |
| 133 | 3) Execute the target operation with explicit parameters and bounded scope. |
| 134 | 4) Verify results and save output/evidence files. |
| 135 | |
| 136 | ## References |
| 137 | |
| 138 | - SDK package: `alibabacloud-ha3engine` |
| 139 | - Demos: data push and HA/SQL search demos in OpenSearch docs |
| 140 | |
| 141 | - Source list: `references/sources.md` |