All Products
Search
Document Center

OpenSearch:Demo of drop-down suggestion

Last Updated:Jul 24, 2025

Configure environment variables

Configure the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables.

Important
  • The AccessKey pair of an Alibaba Cloud account can be used to access all API operations. We recommend that you use a Resource Access Management (RAM) user to call API operations or perform routine O&M. For information about how to use a RAM user, see Create a RAM user.

  • For information about how to create an AccessKey pair, see Create an AccessKey pair.

  • If you use the AccessKey pair of a RAM user, make sure that the required permissions are granted to the AliyunServiceRoleForOpenSearch role by using your Alibaba Cloud account. For more information, see AliyunServiceRoleForOpenSearch and Access authorization rules.

  • We recommend that you do not include your AccessKey pair in materials that are easily accessible to others, such as the project code. Otherwise, your AccessKey pair may be leaked and resources in your account become insecure.

  • Linux and macOS

    Run the following commands. Replace <access_key_id> and <access_key_secret> with the AccessKey ID and AccessKey secret of the RAM user that you use.

    export ALIBABA_CLOUD_ACCESS_KEY_ID=<access_key_id> 
    export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<access_key_secret>
  • Windows

    1. Create an environment variable file, add the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables to the file, and then set the environment variables to your AccessKey ID and AccessKey secret.

    2. Restart Windows for the AccessKey pair to take effect.

Sample for drop-down list search

In the example, replace the following parameters with your own:

  • host: Replace with the public API endpoint of your instance. Go to Instance list > Click Details of the instance > Endpoint > Public API Endpoint to obtain.

  • suggestionName: Replace with your drop-down model name. Go to Search Algorithm Center > Search Guidance > Drop-down Suggestions to obtain.

  • appName: Replace with the instance name. Go to Instance list to view.

  • query: Replace with the search keyword.

Note
  • When using SDK version 4.0.0 or above, to pass AppName when creating a SuggestionClient object.

  • For details about this API operation, see Drop-down suggestion.

  • For the Java SDK, visit the Download hub.

SDK version 4.0.0 and above

package com.example.opensearch;

import com.aliyun.opensearch.OpenSearchClient;
import com.aliyun.opensearch.SuggestionClient;
import com.aliyun.opensearch.sdk.generated.OpenSearch;
import com.aliyun.opensearch.sdk.generated.commons.OpenSearchClientException;
import com.aliyun.opensearch.sdk.generated.commons.OpenSearchException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.nio.charset.Charset;

public class SuggestDemo {
    static private final String host = "Enter the host of the region where the drop-down suggestion associated application is located";
    OpenSearch openSearch;
    OpenSearchClient openSearchClient;
    static private final byte hits = 8; //Maximum number of drop-down suggestions to return
    static private final String suggestionName = "Enter the drop-down suggestion model name"; //Enter the drop-down suggestion model name
    static private final String appName = "Instance name";
    
    @Before
    public void setUp() {
      	// The AccessKey pair.
        // Obtain the AccessKey ID and AccessKey secret from environment variables. You must configure environment variables before you run this code.
        String accesskey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String secret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        // Initialize the openSearch object
        openSearch = new OpenSearch(accesskey, secret, host);
        openSearchClient = new OpenSearchClient(openSearch);
    }

    @Test
    public void TestEnv() {
        // Obtain the file encoding format and default encoding format.
        System.out.println(String.format("file.encoding: %s", System.getProperty("file.encoding")));
        System.out.println(String.format("defaultCharset: %s", Charset.defaultCharset().name()));

        // Create a drop-down suggestion object
        SuggestionClient suggestionClient = new SuggestionClient(appName,suggestionName,openSearchClient);
        String query = "Enter the search query";
try {
            SuggestParams suggestParams = new SuggestParams();
            suggestParams.setQuery(query); // Set the search query
            suggestParams.setHits(hits); // Set the maximum number of drop-down suggestions to return
            suggestParams.setUserId("12345678"); // Set user_id
            // The "Chinese homophone character" completion retrieval feature is enabled by default. You can add the re_search parameter in the request to adjust it
            // ReSearch.findByValue(1) indicates disabled; ReSearch.findByValue(0) or not passing this parameter indicates enabled
            suggestParams.setReSearch(ReSearch.findByValue(1));
            SearchResult result = suggestionClient.execute(suggestParams); 
            System.out.println(result); // Print the retrieval results
        } catch (OpenSearchException e) {
            e.printStackTrace();
        } catch (OpenSearchClientException e) {
            e.printStackTrace();
        }
    }

    @After
    public void clean() {
        openSearch.clear();
    }
}

SDK versions below 4.0.0

package com.example.opensearch;

import com.aliyun.opensearch.OpenSearchClient;
import com.aliyun.opensearch.SuggestionClient;
import com.aliyun.opensearch.sdk.generated.OpenSearch;
import com.aliyun.opensearch.sdk.generated.commons.OpenSearchClientException;
import com.aliyun.opensearch.sdk.generated.commons.OpenSearchException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.nio.charset.Charset;

public class SuggestDemo {
    static private final String host = "Enter the host of the region where the drop-down suggestion associated application is located";
    OpenSearch openSearch;
    OpenSearchClient openSearchClient;
    static private final byte hits = 8; //Maximum number of drop-down suggestions to return
    static private final String suggestionName = "Enter the drop-down suggestion model name"; //Enter the drop-down suggestion model name

    @Before
    public void setUp() {
      	// The AccessKey pair.
        // Obtain the AccessKey ID and AccessKey secret from environment variables. You must configure environment variables before you run this code.
        String accesskey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String secret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        // Initialize the openSearch object
        openSearch = new OpenSearch(accesskey, secret, host);
        openSearchClient = new OpenSearchClient(openSearch);
    }

    @Test
    public void TestEnv() {
        // View documents and the default encoding format.
        System.out.println(String.format("file.encoding: %s", System.getProperty("file.encoding")));
        System.out.println(String.format("defaultCharset: %s", Charset.defaultCharset().name()));

        // Create a drop-down suggestion object
        SuggestionClient suggestionClient = new SuggestionClient(suggestionName, openSearchClient);
        String query = "Enter the search query";
try {
            SuggestParams suggestParams = new SuggestParams();
            suggestParams.setQuery(query); // Set the search query
            suggestParams.setHits(hits); // Set the maximum number of drop-down suggestions to return
            suggestParams.setUserId("12345678"); // Set user_id
            // The "Chinese homophone character" completion retrieval feature is enabled by default. You can add the re_search parameter in the request to adjust it
            // ReSearch.findByValue(1) indicates disabled; ReSearch.findByValue(0) or not passing this parameter indicates enabled
            suggestParams.setReSearch(ReSearch.findByValue(1));
            SearchResult result = suggestionClient.execute(suggestParams); 
            System.out.println(result); // Print the retrieval results
        } catch (OpenSearchException e) {
            e.printStackTrace();
        } catch (OpenSearchClientException e) {
            e.printStackTrace();
        }
    }

    @After
    public void clean() {
        openSearch.clear();
    }
}