All Products
Search
Document Center

Object Storage Service:Vector index (Python SDK V2)

Last Updated:Mar 20, 2026

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_ID and OSS_ACCESS_KEY_SECRET set as environment variables. For setup instructions, see Configure access credentials

  • An OSS bucket in the target region

How it works

All examples follow the same pattern:

  1. Load credentials from environment variables.

  2. Initialize the client with oss.config.load_default().

  3. Call the target API method on the client.

The four core operations are:

OperationMethodDescription
Enable AISearchopen_meta_query()Activates the AISearch index for a bucket
Get index statusget_meta_query_status()Returns the current state and phase of the index
Run a semantic querydo_meta_query()Searches objects using semantic content and metadata filters
Disable AISearchclose_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:

PlaceholderDescriptionExample
<region-id>Region where the bucket is locatedcn-hangzhou
<bucket-name>Name of your bucketmy-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:

FieldDescription
create_timeWhen the index was created
update_timeWhen the index was last updated
stateCurrent index state
phaseCurrent 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:

ParameterDescriptionExample
max_resultsMaximum number of objects to return1000
queryNatural-language description of the content to find"An aerial view of a snow-covered forest"
orderSort order for results — asc or desc"desc"
media_typesRestrict results to specific media typesmedia_type=['image']
simple_queryJSON-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