All Products
Search
Document Center

OpenSearch:Search demo

Last Updated:Mar 18, 2026

This topic describes the environment variables that are required for document searches using the Java SDK and provides sample code.

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 code for document search (SDK V4.0.0)

Search error handling:

Handle exceptions based on the values of the code and message fields, not the status field. For more information about error codes, see Error codes.

Sample 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();
            }
        }
}