All Products
Search
Document Center

OpenSearch:Query data

Last Updated:Nov 01, 2023

You can query data in a table on the Query Test page of the OpenSearch console. Alternatively, you can query the data by using an SDK.

1. Configure the public access whitelist

If you access an OpenSearch Vector Search Edition instance from the virtual private cloud (VPC) in which the instance resides by using the same vSwitch, skip this step.

For more information about how to access an OpenSearch Vector Search Edition instance from an on-premises environment or the Internet, see Configure the public access whitelist.

2. Query data in a table

Use an SDK

The following sample code provides an example on how to use SDK for Python to query data in a table.

Add dependencies:

pip install alibabacloud-ha3engine-vector

Demo code for querying data:

from alibabacloud_ha3engine_vector.client import Client
from alibabacloud_ha3engine_vector.models import Config
from alibabacloud_ha3engine_vector.models import QueryRequest

config = Config(
        #  The API endpoint of the instance.
        endpoint="<API endpoint>",
        #  The instance ID. You can view the instance ID in the upper-left corner of the Instance Details page. Example: ha-cn-i7*****605.
        instance_id="<Instance ID>",
        #  The username. You can view the username in the API Endpoint section of the Instance Details page.
        access_user_name="<Username>",
        #  The password. You can view and change the password in the API Endpoint section of the Instance Details page.
        access_pass_word="<Password>")
client = Client(config)

request = QueryRequest(table_name="<Table name>",
                    	vector=[0.1, 0.2, 0.3],
                        include_vector=True,
                        top_k=10)
result = client.query(request)
print(result.body)

For more information about SDKs for other programming languages, see Query data.

Note
  • If you use SDK for Python, you must remove the http:// prefix when you specify the endpoint parameter.

Sample response:

{
    "totalCount": 1,
    "result": [
        {
            "id": "1",
            "vector": [
                0.0019676426891237499,
               ...
            ],
            "score": 0.0
        }
    ],
    "totalTime": 2.688
}

Use the OpenSearch console

  1. Log on to the OpenSearch Vector Search Edition console. In the left-side navigation pane, click Instances. On the Instances page, find the instance in which you want to run a query test and click the instance ID. On the instance details page, click Query Test in the left-side navigation pane.image.png

  1. On the Query Test page, select an index table and click Search.

image.png

Example:

{
  "vector": [0.0019676427,0.005902928,0.021644069,0.21644068,0.12199384,0.043288138,0.007870571,0.0,0.08460863,0.041320495,0.043288138,0.035417568,0.011805856,0.055093993,0.12592913,0.017708784,0.021644069,0.0019676427,0.0,0.0,0.0019676427,0.078705706,0.1987319,0.041320495,0.039352853,0.0039352854,0.007870571,0.0039352854,0.0039352854,0.017708784,0.035417568,0.06886749,0.0019676427,0.0019676427,0.013773498,0.049191065,0.2125054,0.22824654,0.123961486,0.0039352854,0.0,0.0,0.021644069,0.14560555,0.078705706,0.1987319,0.22824654,0.005902928,0.064932205,0.0019676427,0.0019676427,0.021644069,0.027546996,0.035417568,0.22824654,0.22824654,0.1337997,0.023611711,0.009838213,0.007870571,0.0039352854,0.0039352854,0.017708784,0.20069954,0.033449925,0.005902928,0.019676426,0.035417568,0.015741142,0.029514639,0.13183205,0.123961486,0.029514639,0.0,0.027546996,0.22824654,0.15741141,0.0,0.0039352854,0.043288138,0.18889369,0.072802775,0.055093993,0.17315255,0.08460863,0.0019676427,0.007870571,0.035417568,0.22824654,0.10034977,0.009838213,0.021644069,0.062964566,0.027546996,0.015741142,0.04525578,0.086576276,0.033449925,0.023611711,0.017708784,0.0,0.0,0.03738521,0.072802775,0.16724962,0.035417568,0.031482283,0.20463483,0.043288138,0.011805856,0.0039352854,0.051158708,0.023611711,0.11412327,0.13183205,0.16134669,0.049191065,0.023611711,0.0039352854,0.0039352854,0.049191065,0.035417568,0.015741142,0.0039352854,0.03738521,0.08264099,0.094446845,0.021644069],
  "topK": 10,
  "includeVector": true
}

Sample response:

[
  {
    "vector": [
      0.0019676426891237497,
      0.0059029278345406055,
   	...
    ],
    "id": "0",
    "score": 0
  }
]

Relevant query syntax: