All Products
Search
Document Center

OpenSearch:Primary key-based query

Last Updated:Feb 27, 2024

This topic describes the document search demo that shows you how to access an OpenSearch Vector Search Edition instance to query data by using an asynchronous client in the SDK for Java.

package com.aliyun.ha3engine;

import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

import com.aliyun.ha3engine.async.AsyncClient;
import com.aliyun.ha3engine.async.models.MultiQueryRequest;
import com.aliyun.ha3engine.async.models.QueryRequest;
import com.aliyun.ha3engine.async.models.SearchResponse;
import com.aliyun.sdk.ha3engine.async.core.AsyncConfigInfoProvider;
import com.aliyun.tea.TeaException;

import darabonba.core.client.ClientOverrideConfiguration;

/**
 * @author alibaba
 */
public class SearchDoc {

    public static void main(String[] args) throws Exception {
        try {
        	// The username and password of the instance. You can view the username and password in the API Endpoint section of the Instance Details page.
            AsyncConfigInfoProvider provider = AsyncConfigInfoProvider.create("username", "password");

            // Initialize the asynchronous client.
            AsyncClient client = AsyncClient.builder()
                    .credentialsProvider(provider)
                    .overrideConfiguration(
                            ClientOverrideConfiguration.create()
                                    // The public endpoint of the instance. You can view the public endpoint in the Network Information section of the Instance Details page.
                                    .setEndpointOverride("ha-cn-***********.public.ha.aliyuncs.com")
                        			.setProtocol("http")
                    ).build();
            FetchRequest fetchRequest = FetchRequest.builder().tableName("table_name").ids(Arrays.asList("1", "2")).build();
            CompletableFuture<SearchResponse> searchResponseCompletableFuture = client.fetch(fetchRequest);
            String responseBody = searchResponseCompletableFuture.get().getBody();

            System.out.println("result:" + responseBody);

        } catch (ExecutionException | InterruptedException e) {
            System.out.println(e.getMessage());
        } catch (TeaException e) {
            System.out.println(e.getMessage());
            Map<String, Object> abc = e.getData();
            System.out.println(com.aliyun.teautil.Common.toJSONString(abc));
        }
    }
}