This page shows how to call the drop-down suggestion API using the Java SDK.
Prerequisites
Before you begin, ensure that you have:
An OpenSearch instance with a drop-down suggestion model created in Search Algorithm Center > Search Guidance > Drop-down Suggestions
The public API endpoint of your instance. Go to Instance list > Details > Endpoint > Public API Endpoint to get it.
The Java SDK downloaded from the Download hub
ALIBABA_CLOUD_ACCESS_KEY_IDandALIBABA_CLOUD_ACCESS_KEY_SECRETenvironment variables configured (see Configure environment variables)
Configure environment variables
Set your AccessKey credentials as environment variables before running the sample code.
The AccessKey pair of an Alibaba Cloud account has full access to all API operations. Use a Resource Access Management (RAM) user to call API operations or perform routine O&M. For details, see Create a RAM user and Create an AccessKey pair. If you use a RAM user's AccessKey pair, grant the required permissions to the AliyunServiceRoleForOpenSearch role. For more information, see AliyunServiceRoleForOpenSearch and Access authorization rules. Do not include your AccessKey pair in project code or other materials accessible to others.
Linux and macOS
Replace <access_key_id> and <access_key_secret> with your RAM user's AccessKey ID and AccessKey secret, then run:
export ALIBABA_CLOUD_ACCESS_KEY_ID=<access_key_id>
export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<access_key_secret>Windows
Create an environment variable file and add
ALIBABA_CLOUD_ACCESS_KEY_IDandALIBABA_CLOUD_ACCESS_KEY_SECRETwith your AccessKey ID and AccessKey secret as the values.Restart Windows for the changes to take effect.
Call the drop-down suggestion API
The following example initializes an OpenSearchClient, creates a SuggestionClient, and executes a drop-down suggestion query.
For details about the API, see Drop-down suggestion.
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 = "<public-api-endpoint>"; // Public API endpoint of your instance
static private final String appName = "<instance-name>"; // SDK 4.0.0+: pass appName to SuggestionClient
static private final String suggestionName = "<suggestion-model-name>"; // Drop-down suggestion model name
static private final byte hits = 8; // Maximum number of suggestions to return
OpenSearch openSearch;
OpenSearchClient openSearchClient;
@Before
public void setUp() {
// Read AccessKey credentials from environment variables
String accesskey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
String secret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
openSearch = new OpenSearch(accesskey, secret, host);
openSearchClient = new OpenSearchClient(openSearch);
}
@Test
public void TestEnv() {
System.out.println(String.format("file.encoding: %s", System.getProperty("file.encoding")));
System.out.println(String.format("defaultCharset: %s", Charset.defaultCharset().name()));
// SDK 4.0.0 and above: pass appName as the first argument
SuggestionClient suggestionClient = new SuggestionClient(appName, suggestionName, openSearchClient);
// SDK below 4.0.0: omit appName
// SuggestionClient suggestionClient = new SuggestionClient(suggestionName, openSearchClient);
String query = "<search-keyword>";
try {
SuggestParams suggestParams = new SuggestParams();
suggestParams.setQuery(query); // Search keyword
suggestParams.setHits(hits); // Maximum number of suggestions
suggestParams.setUserId("12345678"); // User ID for personalization
// Chinese homophone character completion is enabled by default.
// ReSearch.findByValue(1) disables it; ReSearch.findByValue(0) or omitting enables it.
suggestParams.setReSearch(ReSearch.findByValue(1));
SearchResult result = suggestionClient.execute(suggestParams);
System.out.println(result);
} catch (OpenSearchException e) {
e.printStackTrace();
} catch (OpenSearchClientException e) {
e.printStackTrace();
}
}
@After
public void clean() {
openSearch.clear();
}
}Replace the placeholders in the code with your actual values:
| Placeholder | Description | Where to get it |
|---|---|---|
<public-api-endpoint> | Public API endpoint of your OpenSearch instance | Instance list > Details > Endpoint > Public API Endpoint |
<instance-name> | Name of your OpenSearch instance | Instance list |
<suggestion-model-name> | Name of your drop-down suggestion model | Search Algorithm Center > Search Guidance > Drop-down Suggestions |
<search-keyword> | The query string to get suggestions for | Your application input |
SDK version note: The only difference between SDK 4.0.0+ and earlier versions is the SuggestionClient constructor. SDK 4.0.0 and above requires appName as the first argument. For SDK versions below 4.0.0, use new SuggestionClient(suggestionName, openSearchClient) instead (the commented-out line in the example above).