All Products
Search
Document Center

OpenSearch:Demo code for implementing the search feature

Last Updated:Apr 03, 2024

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.

Demo code for implementing the search feature by using OpenSearch SDK for Java V4.0.0

Note: Determine whether an error has occurred based on the error code and message instead of the status information.

For more information about errors, see Error codes.

Demo code provided by OpenSearch SDK for Java

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 = "Name of the OpenSearch application that you want to manage";
    private static String host = "Endpoint of the OpenSearch API in your region";
    public static void main(String[] args) {
      			// Specify your AccessKey pair.
    				// Obtain the AccessKey ID and AccessKey secret from the environment variables. You must configure the environment variables before you run the sample code.
      			String accesskey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
            String secret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
            // Obtain the file encoding format and 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 an OpenSearch object.
            OpenSearch openSearch = new OpenSearch(accesskey, secret, host);
            // Use the OpenSearch object as a parameter to create an OpenSearchClient object.
            OpenSearchClient serviceClient = new OpenSearchClient(openSearch);
            // Use the OpenSearchClient object as a parameter to create a SearcherClient object.
            SearcherClient searcherClient = new SearcherClient(serviceClient);
            // Create a Config object and use the config clause to configure parameters such as the application name, paging-related parameters, and data format of returned results.
            Config config = new Config(Lists.newArrayList(appName));
            config.setStart(0);
            config.setHits(5);
            // Specify the data format of return results. Supported formats include XML, JSON, and FULLJSON. In this example, the data format is set to FULLJSON.
            config.setSearchFormat(SearchFormat.FULLJSON);
            // Specify the fields to be returned in 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 class is set by using the setReRankSize method of the Rank class.
            // Specify the kvpairs clause. In this example, the uniq plug-in is added to the distinct clause. This ensures that the values of the total and viewtotal parameters are accurate if the setReserved parameter is set to false.
            //config.setKvpairs("duniqfield:cate_id");
            // Create a SearchParams object.
            SearchParams searchParams = new SearchParams(config);
            // Specify the query clause. You can specify multiple keywords to perform a query based on multiple index fields. In this case, you must specify the index fields in one setQuery call. You can connect two index fields with OR. The following code provides an example. If you specify each index field in a separate setQuery call, the last clause overwrites the preceding clauses.
            searchParams.setQuery("name:'opensearch' OR title:'opensearch'");
            // Create a Distinct object.
            Distinct dist = new Distinct();
            dist.setKey("cate_id"); // Specify a field to be used for distinct extraction.
            dist.setDistCount(1); // Specify the number of documents that are extracted each time.
            dist.setDistTimes(1); // Specify the number of times of extraction.
            dist.setReserved(false); // Specify whether to retain the remaining documents after extraction.
            dist.setUpdateTotalHit(false); // Specify whether to subtract the number of discarded documents from the value of the totalHits parameter if the setreserved parameter is set to false.
            dist.setDistFilter("cate_id<=3"); // Specify a filter condition to be used to select documents to be extracted.
            dist.setGrade("1.2"); // Specify a threshold for distinct extraction.
            // The duniqfield parameter is specified in the form of a kvpairs clause in the config clause.
            // Add the Distinct object as a query parameter.
            searchParams.addToDistincts(dist);
            // Create an Aggregate object.
            Aggregate agg = new Aggregate();
            agg.setGroupKey("cate_id"); // Specify a field to be aggregated.
            agg.setAggFun("count()"); // Specify an aggregation function.
            agg.setAggFilter("cate_id=1"); // Specify an aggregation filter.
            agg.setRange("0~10"); // Specify an aggregation range.
            agg.setAggSamplerThresHold("5"); // Set a threshold for sampled aggregation.
            agg.setAggSamplerStep("5"); // Specify a threshold for sampled aggregation.
            agg.setMaxGroup("5"); // Specify the maximum number of groups that can be returned.
            // Add the Aggregate object as a query parameter.
            //searchParams.addToAggregates(agg);
            // Specify multiple statistics 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);
            // Specify the biz parameter.
            Map hashMap = new HashMap();
            hashMap.put("biz","type:web");
            searchParams.setCustomParam(hashMap);
            // Specify a filter condition.
            // searchParams.setFilter("id > \"0\""); // You can also set a filter condition by using the SearchParamsBuilder class.
            // Specify the sorting condition.
            Sort sorter = new Sort();
            sorter.addToSortFields(new SortField("id", Order.DECREASE)); // Specify a field based on which documents are to be sorted, and a sorting method. In this example, documents are sorted based on the id field in descending order.
            sorter.addToSortFields(new SortField("RANK", Order.INCREASE)); // If multiple documents have the same value of the id field, sort the documents based on the RANK field in ascending order.
            // In the example, documents are sorted based on the tag_match field. The key-value pairs must be set for 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 as a query parameter.
            searchParams.setSort(sorter);
            // Specify a rough sort expression and a fine sort expression. In this example, the default expressions are used.
            Rank rank = new Rank();
            rank.setFirstRankName("default");
            rank.setSecondRankName("default");
            rank.setReRankSize(5);// Specify the number of documents to be sorted based on the fine sort expression.
            // Add the Aggregate object as a query parameter.
            searchParams.setRank(rank);
            // Set the re_search parameter.
            //strategy:threshold,params:total_hits#10 => "strategy:threshold" indicates the query strategy. Only one strategy is supported. 
            // Use the total_hits parameter to set a threshold. If the value of the total_hits parameter for the first run of a query is less than the specified threshold, the query is run again. 
            Map<String, String> reSearchParams = new HashMap<String, String>();
            reSearchParams.put("re_search", "strategy:threshold,params:total_hits#10");
            // Specify the from_request_id parameter for queries that are associated with drop-down suggestions.
            reSearchParams.put("from_request_id", "159851481919726888064081");
            //searchParams.setCustomParam(reSearchParams);
            // Create a Summary object. In this example, the search result summary is added as a query parameter by using a SearchParamsBuilder object.
            Summary summ = new Summary("name");
            summ.setSummary_field("name");// Specify the fields for which you want to configure the search result summary. The fields must be of the text type so that the fields can be analyzed. 
            summ.setSummary_len("50");// Specify the maximum length of the search result summary.
            summ.setSummary_element("em"); // Specify the name of the HTML tag that is used to highlight search queries in red.
            summ.setSummary_ellipsis("...");// Specify the connector that is used to connect segments.
            summ.setSummary_snippet("1");// Specify the number of segments.
            // Add the Summary object as a query parameter.
            //searchParams.addToSummaries(summ);
            
            // Add the raw_query parameter. The value of the parameter must be the same as that of the search query.
            searchParams.setRawQuery("opensearch");
            // Create a SearchParamsBuilder object. As the utility class of SearchParams, the SearchParamsBuilder class allows you to configure query-related parameters with ease.
            SearchParamsBuilder paramsBuilder = SearchParamsBuilder.create(searchParams);
            // Use the SearchParamsBuilder object to add the created search result summary as a query parameter.
            paramsBuilder.addSummary("name", 50, "em", "...", 1);
            // Specify a filter condition.
            paramsBuilder.addFilter("id>=0", "AND");

            // Specify the disable parameter to disable the re-search feature:
            //searchParams.putToCustomParam("disable", "re_search");
            try {
                // Run the query and return the results. Determine whether an error has occurred based on the error code and message. For more information about errors, see the "Error codes" topic. 
                SearchResult searchResult = searcherClient.execute(paramsBuilder);
                String result = searchResult.getResult();
                JSONObject obj = new JSONObject(result);
                // Display the search results.
                System.out.println(obj.toString());
                // You may need the information about request addresses for debugging.
                SearchResultDebug searchdebugrst = searcherClient.executeDebug(searchParams);
                // Display the request string of the last query.
                System.out.println(searchdebugrst.getRequestUrl());
            } catch (OpenSearchException e) {
                e.printStackTrace();
            } catch (OpenSearchClientException e) {
                e.printStackTrace();
            }
        }
}
Note

For more informations,see Clauses.