Configure environment variables
Configure the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables.
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
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.
Restart Windows for the AccessKey pair to take effect.
Sample code for drop-down suggestions
Before you run the sample code, replace the following parameters with your actual values:
-
host: The public API endpoint of your instance. To obtain the endpoint, go to the instance list page, click Details for the instance, and find the Public API Endpoint in the Endpoint section.
-
suggestionName: The name of your drop-down suggestion model. To obtain the name, go to Search Algorithm Center, choose Search Guidance, and click drop-down suggestion.
-
appName: The name of your instance. You can find the name on the instance list page.
-
query: The search keyword.
-
If you use SDK v4.0.0 or later, you must pass AppName when you create a SuggestionClient object. This parameter is not required for SDK versions earlier than v4.0.0.
-
For more information about this API operation, see drop-down suggestion.
-
You can download the Java SDK from the download hub.
SDK v4.0.0 and later
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 {
// Replace the placeholders with your actual values.
static private final String host = "";
static private final String appName = "";
static private final String suggestionName = "";
static private final byte hits = 8; // The maximum number of suggestions to return.
OpenSearch openSearch;
OpenSearchClient openSearchClient;
@Before
public void setUp() {
// Configure credentials.
// Read credentials from environment variables.
String accesskey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
String secret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
// Initialize the OpenSearch client.
openSearch = new OpenSearch(accesskey, secret, host);
openSearchClient = new OpenSearchClient(openSearch);
}
@Test
public void TestEnv() {
// (Optional) Print the default file encoding.
System.out.println(String.format("file.encoding: %s", System.getProperty("file.encoding")));
System.out.println(String.format("defaultCharset: %s", Charset.defaultCharset().name()));
// Create a client for drop-down suggestions.
SuggestionClient suggestionClient = new SuggestionClient(appName,suggestionName,openSearchClient);
String query = "";
try {
SuggestParams suggestParams = new SuggestParams();
suggestParams.setQuery(query); // Set the search query.
suggestParams.setHits(hits); // Set the maximum number of suggestions.
suggestParams.setUserId("12345678"); // Set the user ID for personalized results.
// By default, the completion feature for Chinese homophones is enabled.
// You can add the re_search parameter to adjust this behavior.
// ReSearch.findByValue(1) disables this feature. ReSearch.findByValue(0) or omitting the parameter enables it.
suggestParams.setReSearch(ReSearch.findByValue(1));
SearchResult result = suggestionClient.execute(suggestParams);
System.out.println(result); // Print the result.
} catch (OpenSearchException e) {
e.printStackTrace();
} catch (OpenSearchClientException e) {
e.printStackTrace();
}
}
@After
public void clean() {
openSearch.clear();
}
}
SDK before v4.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 {
// Replace the placeholders with your actual values.
static private final String host = "";
static private final String suggestionName = "";
static private final byte hits = 8; // The maximum number of suggestions to return.
OpenSearch openSearch;
OpenSearchClient openSearchClient;
@Before
public void setUp() {
// Configure credentials.
// Read credentials from environment variables.
String accesskey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
String secret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
// Initialize the OpenSearch client.
openSearch = new OpenSearch(accesskey, secret, host);
openSearchClient = new OpenSearchClient(openSearch);
}
@Test
public void TestEnv() {
// (Optional) Print the default file encoding.
System.out.println(String.format("file.encoding: %s", System.getProperty("file.encoding")));
System.out.println(String.format("defaultCharset: %s", Charset.defaultCharset().name()));
// Create a client for drop-down suggestions.
SuggestionClient suggestionClient = new SuggestionClient(suggestionName, openSearchClient);
String query = "";
try {
SuggestParams suggestParams = new SuggestParams();
suggestParams.setQuery(query); // Set the search query.
suggestParams.setHits(hits); // Set the maximum number of suggestions.
suggestParams.setUserId("12345678"); // Set the user ID for personalized results.
// By default, the completion feature for Chinese homophones is enabled.
// You can add the re_search parameter to adjust this behavior.
// ReSearch.findByValue(1) disables this feature. ReSearch.findByValue(0) or omitting the parameter enables it.
suggestParams.setReSearch(ReSearch.findByValue(1));
SearchResult result = suggestionClient.execute(suggestParams);
System.out.println(result); // Print the result.
} catch (OpenSearchException e) {
e.printStackTrace();
} catch (OpenSearchClientException e) {
e.printStackTrace();
}
}
@After
public void clean() {
openSearch.clear();
}
}