All Products
Search
Document Center

OpenSearch:Demo code for implementing iterative scroll queries

Last Updated:Aug 31, 2023

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 iterative scroll queries by using OpenSearch SDK for Java V4.0.0

Iterative scroll queries free you from specifying the scroll ID for each scroll query. Scroll queries do not support the aggregate, distinct, or rank clause, and support sorting only based on a single field.

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.generated.OpenSearch;
import com.aliyun.opensearch.sdk.generated.search.*;
import com.aliyun.opensearch.search.DeepPageingIterator;
import java.nio.charset.Charset;

public class testScrollIterator {

    // Scroll queries do not support the aggregate, distinct, or rank clause, and support sorting only based on a single field.
    private static String appName = "Name of the OpenSearch application that you want to manage";
    private static String tableName = "Name of the table to which data is to be uploaded";
    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(start); // The start parameter that you specify in the config clause does not take effect for scroll queries. The default value 0 is used.
        config.setHits(5);// Specify the number of documents to be displayed on each page. In this example, the number is set to 5.

        // Specify the data format of returned results. Supported formats are 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 specified by the setReRankSize method of the Rank class.

        // 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. If you specify each index field in a separate setQuery call, the last clause overwrites the preceding clauses.
        searchParams.setQuery("name:'opensearch'");

        // Specify filter conditions.
        searchParams.setFilter("cate_id<=3"); // You can also set a filter condition by using the SearchParamsBuilder class.

       	// Specify a 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.
 
         // Add the Sort object as a query parameter.
        searchParams.setSort(sorter);     
      
        // Create a DeepPaging object to implement iterative scroll queries.
        DeepPaging deep =new DeepPaging();

        // Add the DeepPaging object as a query parameter.
        searchParams.setDeepPaging(deep);

        // Use the DeepPageingIterator class to implement iterative scroll queries. This way, you do not need to specify scroll IDs.
        DeepPageingIterator pagesIterator = new DeepPageingIterator(searcherClient, searchParams);

        // Set an iteration interval, in milliseconds. The default interval is 100 milliseconds. This ensures that the paging speed is appropriate and does not place much pressure on the OpenSearch service. You can set an iteration interval based on your needs.
        pagesIterator.setPagingIntervals(80); // The iteration interval, in milliseconds.

        // Determine whether an error has occurred based on the error code and message instead of the status information. For more information about errors, see the "Error codes" topic. 
        try{

            System.out.println("test");
            while (pagesIterator.hasNext()) {
                System.out.println("Debugging information:" + pagesIterator.next());
            }

        }catch(Exception ex){
            System.out.println("Error message:" + ex.getMessage());
        }

    }
}