OSS AISearch lets you search objects by semantic content, OSS metadata, multimedia metadata, ETags, tags, and custom metadata — no matter how many objects are in the bucket. This topic shows how to use Python SDK V2 to enable, query, and disable AISearch.
Note: All examples use the China (Hangzhou) region (cn-hangzhou) with the public endpoint. To access OSS from another Alibaba Cloud service in the same region, switch to the internal endpoint. For region-to-endpoint mappings, see Regions and endpoints.Prerequisites
Before you begin, ensure that you have:
Python SDK V2 installed (
alibabacloud_oss_v2)OSS_ACCESS_KEY_IDandOSS_ACCESS_KEY_SECRETset as environment variables. For setup instructions, see Configure access credentialsAn OSS bucket in the target region
How it works
All examples follow the same pattern:
Load credentials from environment variables.
Initialize the client with
oss.config.load_default().Call the target API method on the client.
The four core operations are:
| Operation | Method | Description |
|---|---|---|
| Enable AISearch | open_meta_query() | Activates the AISearch index for a bucket |
| Get index status | get_meta_query_status() | Returns the current state and phase of the index |
| Run a semantic query | do_meta_query() | Searches objects using semantic content and metadata filters |
| Disable AISearch | close_meta_query() | Deactivates the AISearch index for a bucket |
Enable AISearch
Call open_meta_query() with mode='semantic' to enable the AISearch index on a bucket.
import alibabacloud_oss_v2 as oss
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
cfg.region = "<region-id>" # Example: cn-hangzhou
client = oss.Client(cfg)
result = client.open_meta_query(oss.OpenMetaQueryRequest(
bucket="<bucket-name>",
mode='semantic', # Set to "semantic" to enable AISearch
))
print(f"status code: {result.status_code}, request ID: {result.request_id}")Replace the following placeholders:
| Placeholder | Description | Example |
|---|---|---|
<region-id> | Region where the bucket is located | cn-hangzhou |
<bucket-name> | Name of your bucket | my-bucket |
Get index status
Call get_meta_query_status() to check the current state of the AISearch index.
import alibabacloud_oss_v2 as oss
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
cfg.region = "<region-id>"
client = oss.Client(cfg)
result = client.get_meta_query_status(oss.GetMetaQueryStatusRequest(
bucket="<bucket-name>",
))
print(f"status code: {result.status_code}")
print(f"request ID: {result.request_id}")
print(f"created at: {result.meta_query_status.create_time}")
print(f"updated at: {result.meta_query_status.update_time}")
print(f"state: {result.meta_query_status.state}")
print(f"phase: {result.meta_query_status.phase}")The response includes:
| Field | Description |
|---|---|
create_time | When the index was created |
update_time | When the index was last updated |
state | Current index state |
phase | Current indexing phase |
Query objects
Call do_meta_query() with mode='semantic' to search objects by semantic content and metadata filters.
The following parameters control the query:
| Parameter | Description | Example |
|---|---|---|
max_results | Maximum number of objects to return | 1000 |
query | Natural-language description of the content to find | "An aerial view of a snow-covered forest" |
order | Sort order for results — asc or desc | "desc" |
media_types | Restrict results to specific media types | media_type=['image'] |
simple_query | JSON-encoded metadata filter expression | '{"Operation":"gt","Field":"Size","Value":"30"}' |
import alibabacloud_oss_v2 as oss
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
cfg.region = "<region-id>"
client = oss.Client(cfg)
result = client.do_meta_query(oss.DoMetaQueryRequest(
bucket="<bucket-name>",
mode='semantic',
meta_query=oss.MetaQuery(
max_results=1000,
query='An aerial view of a snow-covered forest',
order='desc',
media_types=oss.MetaQueryMediaTypes(
media_type=['image']
),
simple_query='{"Operation":"gt", "Field": "Size", "Value": "30"}',
),
))
print(vars(result))Disable AISearch
Call close_meta_query() to disable the AISearch index for a bucket.
import alibabacloud_oss_v2 as oss
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
cfg.region = "<region-id>"
client = oss.Client(cfg)
result = client.close_meta_query(oss.CloseMetaQueryRequest(
bucket="<bucket-name>",
))
print(f"status code: {result.status_code}, request ID: {result.request_id}")What's next
AISearch overview — learn about supported search conditions and how the index is built
Data indexing API reference — full API documentation for the meta query operations
Complete sample on GitHub — end-to-end runnable example