Configurar variables de entorno
Configure las variables de entorno ALIBABA_CLOUD_ACCESS_KEY_ID y ALIBABA_CLOUD_ACCESS_KEY_SECRET.
El par de AccessKey de una cuenta de Alibaba Cloud permite acceder a todas las operaciones de la API. Se recomienda utilizar un usuario de Resource Access Management (RAM) para invocar operaciones de la API o realizar tareas rutinarias de operación y mantenimiento. Para obtener información sobre cómo usar un usuario de RAM, consulte Create a RAM user.
Para obtener información sobre cómo crear un par de AccessKey, consulte Create an AccessKey pair.
Si utiliza el par de AccessKey de un usuario de RAM, asegúrese de que se hayan otorgado los permisos necesarios al rol AliyunServiceRoleForOpenSearch mediante su cuenta de Alibaba Cloud. Para obtener más información, consulte AliyunServiceRoleForOpenSearch y Access authorization rules.
Se recomienda no incluir su par de AccessKey en materiales fácilmente accesibles para otros, como el código del proyecto. De lo contrario, su par de AccessKey podría filtrarse y los recursos de su cuenta quedarían expuestos.
-
Linux y macOS
Ejecute los siguientes comandos. Reemplace
<access_key_id>y<access_key_secret>con el AccessKey ID y el AccessKey secret del usuario de RAM que esté utilizando.export ALIBABA_CLOUD_ACCESS_KEY_ID=<access_key_id> export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<access_key_secret> -
Windows
Cree un archivo de variables de entorno, agregue las variables de entorno ALIBABA_CLOUD_ACCESS_KEY_ID y ALIBABA_CLOUD_ACCESS_KEY_SECRET al archivo y, a continuación, establezca las variables de entorno con su AccessKey ID y su AccessKey secret.
Reinicie Windows para que el par de AccessKey surta efecto.
Código de ejemplo del SDK V4.0.0 para búsqueda de documentos
Manejo de errores de búsqueda: Compruebe los campos code y message para determinar si ocurrió un error, en lugar del campo status.
Para obtener más información sobre los códigos de error, consulte Error codes.
Demostración del SDK:
Reemplace los parámetros del siguiente código de ejemplo con sus valores reales.
En la consola de OpenSearch, en la página de instance details, busque el application name en el título de la página. En la sección endpoint, localice el host, que corresponde al public endpoint o al VPC endpoint.
appName: El nombre de su aplicación.
host: El endpoint del servicio. Puede ser un endpoint público o de VPC.
package com.aliyun.opensearch;
import com.aliyun.opensearch.sdk.dependencies.com.google.common.collect.Lists;
import com.aliyun.opensearch.sdk.dependencies.org.json.JSONObject;
import com.aliyun.opensearch.sdk.generated.OpenSearch;
import com.aliyun.opensearch.sdk.generated.commons.OpenSearchClientException;
import com.aliyun.opensearch.sdk.generated.commons.OpenSearchException;
import com.aliyun.opensearch.sdk.generated.search.*;
import com.aliyun.opensearch.sdk.generated.search.general.SearchResult;
import com.aliyun.opensearch.search.SearchParamsBuilder;
import com.aliyun.opensearch.search.SearchResultDebug;
import java.nio.charset.Charset;
public class testSearch {
private static String appName = "your_opensearch_app_name";
private static String host = "your_app_api_endpoint";
public static void main(String[] args) {
// User credentials.
// Read the AccessKey ID and AccessKey Secret from environment variables. You must configure the environment variables before you run this sample code.
String accesskey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
String secret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
// View the file and default encoding formats.
System.out.println(
String.format("file.encoding: %s", System.getProperty("file.encoding"))
);
System.out.println(
String.format("defaultCharset: %s", Charset.defaultCharset().name())
);
// Create and initialize an OpenSearch object.
OpenSearch openSearch = new OpenSearch(accesskey, secret, host);
// Create an OpenSearchClient object and pass the OpenSearch object to its constructor.
OpenSearchClient serviceClient = new OpenSearchClient(openSearch);
// Create a SearcherClient object and pass the OpenSearchClient object to its constructor.
SearcherClient searcherClient = new SearcherClient(serviceClient);
// Define a Config object to set config clause parameters, such as paging, return format, and application name.
Config config = new Config(Lists.newArrayList(appName));
config.setStart(0);
config.setHits(5);
// Set the return format to FULLJSON. Supported formats: XML, JSON, and FULLJSON.
config.setSearchFormat(SearchFormat.FULLJSON);
// Specify the fields to be returned in the search results.
config.setFetchFields(
Lists.newArrayList(
"id",
"name",
"phone",
"int_arr",
"literal_arr",
"float_arr",
"cate_id"
)
);
// Note: The rerank_size parameter in the config clause is set in the Rank object.
// Set kvpairs clause parameters. In this example, the uniq plug-in is added for the distinct clause to resolve inaccuracies in total and viewtotal values when reserved is set to false.
//config.setKvpairs("duniqfield:cate_id");
// Create a parameters object.
SearchParams searchParams = new SearchParams(config);
// Set the query clause. To query multiple indexes, you must combine them in the setQuery method, for example, by using the OR operator. If you call the setQuery method multiple times, each call overrides the previous one.
searchParams.setQuery("name:'opensearch' OR title:'opensearch'");
// Set the distinct clause.
Distinct dist = new Distinct();
dist.setKey("cate_id"); // Set the dist_key parameter.
dist.setDistCount(1); // Set the dist_count parameter.
dist.setDistTimes(1); // Set the dist_times parameter.
dist.setReserved(false); // Set the reserved parameter.
dist.setUpdateTotalHit(false); // Set the update_total_hit parameter.
dist.setDistFilter("cate_id<=3"); // Set a filter condition.
dist.setGrade("1.2"); // Set the grade parameter.
// The duniqfield parameter is added in the kvpairs clause of the config clause.
// Add the Distinct object.
searchParams.addToDistincts(dist);
// Set the aggregation clause.
Aggregate agg = new Aggregate();
agg.setGroupKey("cate_id"); // Set the group_key parameter.
agg.setAggFun("count()"); // Set the agg_fun parameter.
agg.setAggFilter("cate_id=1"); // Set the agg_filter parameter.
agg.setRange("0~10"); // Enable range statistics.
agg.setAggSamplerThresHold("5"); // Set the sampling threshold.
agg.setAggSamplerStep("5"); // Set the sampling step.
agg.setMaxGroup("5"); // Set the maximum number of groups to be returned.
// Add the Aggregate object.
//searchParams.addToAggregates(agg);
// The following code provides an example of how to specify multiple aggregation fields.
Set<Aggregate> Aggregates = new HashSet();
Aggregate aggregate1 = new Aggregate();
aggregate1.setGroupKey("cate_id");
aggregate1.setAggFun("count()");
Aggregates.add(aggregate1);
Aggregate aggregate2 = new Aggregate();
aggregate2.setGroupKey("cate_id_1");
aggregate2.setAggFun("count()");
Aggregates.add(aggregate2);
searchParams.setAggregates(Aggregates);
// Set the biz parameter.
Map hashMap = new HashMap();
hashMap.put("biz", "type:web");
searchParams.setCustomParam(hashMap);
// Set filter conditions for the query.
// searchParams.setFilter("id > \"0\""); // You can also use ParamsBuilder to add a filter condition.
// Set sort conditions.
Sort sorter = new Sort();
sorter.addToSortFields(new SortField("id", Order.DECREASE)); // Sort the results by the id field in descending order.
sorter.addToSortFields(new SortField("RANK", Order.INCREASE)); // If the values of the id field are the same, sort the results by the RANK field in ascending order.
// The following code provides an example of how to use tag_match for sorting. You must set the Kvpairs parameter in the config clause.
//sorter.addToSortFields(new SortField("tag_match(\"kk1\",int_array,10,\"max\",\"false\",\"false\")", Order.INCREASE));
//config.setKvpairs("kk1:10");
// Add the Sort object.
searchParams.setSort(sorter);
// Set expressions for rough and fine sorting. In this example, the default expressions are used.
Rank rank = new Rank();
rank.setFirstRankName("default");
rank.setSecondRankName("default");
rank.setReRankSize(5); // Set the number of documents for fine sorting.
// Add the Rank object.
searchParams.setRank(rank);
// Set re-search parameters.
// strategy:threshold,params:total_hits#10: strategy:threshold specifies the re-search policy. This is the only supported policy.
// The parameter for the threshold is `total_hits`. A re-search is triggered if the number of total hits in the initial search is less than this value.
Map<String, String> reSearchParams = new HashMap<String, String>();
reSearchParams.put("re_search", "strategy:threshold,params:total_hits#10");
// Set from_request_id to associate the request with search suggestions.
reSearchParams.put("from_request_id", "159851481919726888064081");
//searchParams.setCustomParam(reSearchParams);
// Set the summary information for search results. We recommend that you use the SearchParamsBuilder object to add summary information.
Summary summ = new Summary("name");
summ.setSummary_field("name"); // The field for which the summary is generated. The field must be of the TEXT type and allow tokenization.
summ.setSummary_len("50"); // The length of the snippet.
summ.setSummary_element("em"); // The tag that is used to highlight keywords.
summ.setSummary_ellipsis("..."); // The ellipsis.
summ.setSummary_snippet("1"); // The number of snippets.
// Add the Summary object.
//searchParams.addToSummaries(summ);
// Add the raw_query parameter. The value of this parameter must be the same as the value of the query parameter.
searchParams.setRawQuery("opensearch");
// SearchParamsBuilder is a utility class that simplifies parameter construction.
SearchParamsBuilder paramsBuilder = SearchParamsBuilder.create(
searchParams
);
// Use the SearchParamsBuilder object to add a summary for the search results.
paramsBuilder.addSummary("name", 50, "em", "...", 1);
// Set a filter condition.
paramsBuilder.addFilter("id>=0", "AND");
// Set the disable parameter to disable the re-search feature.
//searchParams.putToCustomParam("disable", "re_search");
try {
// Execute the query and return the results. You must check the code and message to handle exceptions. For more information about the error messages that correspond to the error codes, see the relevant documentation.
SearchResult searchResult = searcherClient.execute(paramsBuilder);
String result = searchResult.getResult();
JSONObject obj = new JSONObject(result);
// Print the search results.
System.out.println(obj.toString());
// The following code shows how to retrieve the request URL for debugging.
SearchResultDebug searchdebugrst = searcherClient.executeDebug(
searchParams
);
// Print the URL of the last query request.
System.out.println(searchdebugrst.getRequestUrl());
} catch (OpenSearchException e) {
e.printStackTrace();
} catch (OpenSearchClientException e) {
e.printStackTrace();
}
}
}
Para obtener más información sobre las cláusulas de consulta, consulte Clauses.