All Products
Document Center

Demo code for implementing the query analysis features

Last Updated: Sep 09, 2021

Demo code for implementing the query analysis features by using OpenSearch SDK for Java V3.1.3

package com.aliyun.opensearch;

import com.aliyun.opensearch.sdk.generated.OpenSearch;
import com.aliyun.opensearch.sdk.generated.commons.OpenSearchClientException;
import com.aliyun.opensearch.sdk.generated.commons.OpenSearchException;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

public class testQueryProcessor {

    private static String appName = "The name of the OpenSearch application for which you want to implement the query analysis features";
    private static String accesskey = "The AccessKey ID of your Alibaba Cloud account";
    private static String secret = "The AccessKey secret of your Alibaba Cloud account";
    private static String host = "The endpoint of the OpenSearch API in your region";

    public static void main(String[] args) {

        // View documents 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 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 return results.
        Config config = new Config(Lists.newArrayList(appName));
        // Specify the data format of return results. OpenSearch SDK for Java V3.1.2 supports XML and JSON but not FULLJSON. The FULLJSON format is supported by OpenSearch SDK for Java V3.1.3.

        // 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:'kfc'");// If the query keyword is kfc, the return results are the documents that are related to KFC.

        List<String> qpname = new ArrayList<String>();
        qpname.add("qptest");  // Set a name for the query analyzer.

        // 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);

        // Run the query and return the results.
        SearchResult searchResult;
        try {
            searchResult = searcherClient.execute(paramsBuilder);
            String result = searchResult.getResult();

            JSONObject obj = new JSONObject(result);

            // Determine whether an error has occurred. If no error has occurred, the search results are returned. If an error has occurred, the following code is run:
                JSONArray jsonarr = JSONArray.parseArray(obj.get("errors").toString());
                System.out.println("error code:"+jsonarr.getJSONObject(0).get("code"));
                System.out.println("error message:"+jsonarr.getJSONObject(0).get("message"));
                // Display the search results.
                // You may need the information about request addresses for debugging.
                SearchResultDebug srd = searcherClient.executeDebug(searchParams);

        } catch (OpenSearchException e) {
        } catch (OpenSearchClientException e) {