Tous les produits
Search
Centre de documentation

OpenSearch:Search demo

Dernière mise à jour :Aug 10, 2026

Découvrez comment configurer les variables d'environnement et utiliser l'exemple de code du SDK Java pour rechercher des documents.

Configuration des variables d'environnement

Définissez les variables d'environnement ALIBABA_CLOUD_ACCESS_KEY_ID et ALIBABA_CLOUD_ACCESS_KEY_SECRET.

Important
  • La paire AccessKey d'un compte Alibaba Cloud permet d'accéder à toutes les opérations API. Nous vous recommandons d'utiliser un utilisateur Resource Access Management (RAM) pour appeler les opérations API ou effectuer la maintenance courante. Pour savoir comment utiliser un utilisateur RAM, consultez la rubrique Création d'un utilisateur RAM.

  • Pour savoir comment créer une paire AccessKey, consultez la rubrique Création d'une paire AccessKey.

  • Si vous utilisez la paire AccessKey d'un utilisateur RAM, assurez-vous que le rôle AliyunServiceRoleForOpenSearch dispose des autorisations requises via votre compte Alibaba Cloud. Pour plus d'informations, consultez les rubriques AliyunServiceRoleForOpenSearch et Règles d'autorisation d'accès.

  • Nous vous déconseillons d'inclure votre paire AccessKey dans des supports facilement accessibles à des tiers, tels que le code du projet. Dans le cas contraire, votre paire AccessKey pourrait être divulguée et compromettre la sécurité des ressources de votre compte.

  • Linux et macOS

    Exécutez les commandes suivantes. Remplacez <access_key_id> et <access_key_secret> par l'ID AccessKey et la clé secrète AccessKey de l'utilisateur RAM utilisé.

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

    1. Créez un fichier de variables d'environnement, ajoutez-y les variables ALIBABA_CLOUD_ACCESS_KEY_ID et ALIBABA_CLOUD_ACCESS_KEY_SECRET, puis définissez-les avec votre ID AccessKey et votre clé secrète AccessKey.

    2. Redémarrez Windows pour que la paire AccessKey prenne effet.

Exemple de code pour la recherche de documents (SDK V4.0.0)

Gestion des erreurs de recherche :

Traitez les exceptions en fonction des champs code et message, et non du champ status. Pour plus de détails sur les codes d'erreur, consultez la rubrique Codes d'erreur.

Exemple de code :

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 = "Replace with your OpenSearch application name";
    private static String host = "Replace with the API endpoint of your application";
    public static void main(String[] args) {
            // User identity information.
    				// 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 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 and construct an OpenSearch object.
            OpenSearch openSearch = new OpenSearch(accesskey, secret, host);
            // Create an OpenSearchClient object and use the OpenSearch object as a constructor parameter.
            OpenSearchClient serviceClient = new OpenSearchClient(openSearch);
            // Create a SearcherClient object and use the OpenSearchClient object as a constructor parameter.
            SearcherClient searcherClient = new SearcherClient(serviceClient);
            // Define a Config object to set parameters for the config clause, such as paging, data 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 include XML, JSON, and FULLJSON.
            config.setSearchFormat(SearchFormat.FULLJSON);
            // Specify the fields to return 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 class object.
            // Set the parameters for the kvpairs clause. In this example, the uniq plugin is added to the distinct clause to resolve the issue of inaccurate total and viewtotal values when reserved is set to false.
            //config.setKvpairs("duniqfield:cate_id");
            // Create a parameter object.
            SearchParams searchParams = new SearchParams(config);
            // Set the query clause. To combine multiple indexes for a query, merge them in setQuery. Otherwise, subsequent setQuery calls will overwrite previous ones.
            searchParams.setQuery("name:'opensearch'");
            // Set the aggregation and distinct clause.
            Distinct dist = new Distinct();
            dist.setKey("cate_id"); // Set dist_key.
            dist.setDistCount(1); // Set dist_count.
            dist.setDistTimes(1); // Set dist_times.
            dist.setReserved(false); // Set reserved.
            dist.setUpdateTotalHit(false); // Set update_total_hit.
            dist.setDistFilter("cate_id<=3"); // Set the filter condition.
            dist.setGrade("1.2"); // Set the grade.
            // The duniqfield parameter is added by specifying the kvpairs clause in the config clause.
            // Add the Distinct object parameter.
            searchParams.addToDistincts(dist);
            // Set the aggregation clause.
            Aggregate agg = new Aggregate();
            agg.setGroupKey("cate_id"); // Set group_key.
            agg.setAggFun("count()"); // Set agg_fun.
            agg.setAggFilter("cate_id=1"); // Set agg_filter.
            agg.setRange("0~10"); // Set segmented statistics.
            agg.setAggSamplerThresHold("5"); // Set the sampling threshold.
            agg.setAggSamplerStep("5"); // Set the sampling step size.
            agg.setMaxGroup("5"); // Set the maximum number of groups to return.
            // Add the Aggregate object parameter.
            //searchParams.addToAggregates(agg);
            // Example of setting 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 the query filter condition.
            // searchParams.setFilter("id > \"0\""); // You can also use the following ParamsBuilder to add a filter condition.
            // Set the sort condition.
            Sort sorter = new Sort();
            sorter.addToSortFields(new SortField("id", Order.DECREASE)); // Sort by the id field in descending order.
            sorter.addToSortFields(new SortField("RANK", Order.INCREASE)); // If the IDs are the same, sort by RANK in ascending order.
            // Example of using tag_match in sort: (The Kvpairs must be set 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 parameter.
            searchParams.setSort(sorter);
            // Set the coarse and fine sort expressions. In this example, they are set to the default values.
            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 parameter.
            searchParams.setRank(rank);
            // Set the re_search parameter for re-searching.
            // strategy:threshold,params:total_hits#10 => Here, strategy:threshold indicates the policy. Only one policy is currently supported.
            // The parameter for threshold is total_hits. This means that a re-search is triggered if the total_hits of the first request 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 for the associated search request.
            reSearchParams.put("from_request_id", "159851481919726888064081");
            //searchParams.setCustomParam(reSearchParams);
            // Set the search result summary. In this example, the SearchParamsBuilder object is used to add the summary, which is more convenient.
            Summary summ = new Summary("name");
            summ.setSummary_field("name");// The field to which the summary applies. This field must be a text field that can be tokenized.
            summ.setSummary_len("50");// The length of the snippet.
            summ.setSummary_element("em"); // The HTML tag for highlight.
            summ.setSummary_ellipsis("...");// The ellipsis for the snippet.
            summ.setSummary_snippet("1");// The number of snippets.
            // Add the Summary object parameter.
            //searchParams.addToSummaries(summ);
            
            // Add the raw_query parameter. The value must be the same as the search query.
            searchParams.setRawQuery("opensearch");
            // A utility class for SearchParams that provides more convenient operations.
            SearchParamsBuilder paramsBuilder = SearchParamsBuilder.create(searchParams);
            // Use the SearchParamsBuilder object to add a search result summary.
            paramsBuilder.addSummary("name", 50, "em", "...", 1);
            // Set the query 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 result. Check for exceptions based on the code and message. For information about error codes, see the Error codes document.
                SearchResult searchResult = searcherClient.execute(paramsBuilder);
                String result = searchResult.getResult();
                JSONObject obj = new JSONObject(result);
                // Print the query result.
                System.out.println(obj.toString());
                // Some users may need to debug the request URL information.
                SearchResultDebug searchdebugrst = searcherClient.executeDebug(searchParams);
                // Print the information of the last query request string.
                System.out.println(searchdebugrst.getRequestUrl());
            } catch (OpenSearchException e) {
                e.printStackTrace();
            } catch (OpenSearchClientException e) {
                e.printStackTrace();
            }
        }
}