This topic describes the document search demo that shows you how to access an instance of OpenSearch Vector Search Edition to query data by using the SDK. You can query data in the following methods.
Execute an SQL statement
import java.util.HashMap;
import java.util.Map;
import com.aliyun.ha3engine.Client;
import com.aliyun.ha3engine.models.*;
import com.aliyun.tea.TeaException;
/**
* @author alibaba
*/
public class SearchDoc {
public static void main(String[] args) throws Exception {
Config config = new Config();
// The username. You can view the username on the API Endpoint tab of the Instance Details page.
config.setAccessUserName("<userName>");
// The password. You can change the password on the API Endpoint tab of the Instance Details page.
config.setAccessPassWord("<password>");
// The name of the OpenSearch instance. You can view the instance name in the upper-left corner of the Instance Details page. Example: ha-cn-i7*****605.
config.setInstanceId("<instanceID>");
// To access the instance by using an internal endpoint, uncomment the following line and specify the endpoint.
//config.setEndpoint("ha-cn-******.ha.aliyuncs.com");
// To access the instance over the Internet, uncomment the following line and specify the HTTP proxy.
// config.setHttpProxy("http://Public IP address:Port number");
Client client = new Client(config);
try {
SearchRequestModel searchRequestModel = new SearchRequestModel();
SearchQuery searchQuery = new SearchQuery();
SQLQuery sqlQuery = new SQLQuery();
sqlQuery.setQuery("select * from table_odps");
Map<String, String> sqlQueryKvpairs = new HashMap<>();
sqlQueryKvpairs.put("trace", "INFO");
sqlQueryKvpairs.put("formatType", "full_json");
sqlQuery.setKvpairs(sqlQueryKvpairs);
searchQuery.setSql(client.buildSQLSearchQuery(sqlQuery));
searchRequestModel.setQuery(searchQuery);
SearchResponseModel searchResponseModel = client.Search(searchRequestModel);
System.out.println("result:" + searchResponseModel.getBody());
} catch (TeaException e) {
System.out.println(e.getMessage());
Map<String, Object> abc = e.getData();
System.out.println(com.aliyun.teautil.Common.toJSONString(abc));
}
}
}Use an SQL query string
import java.util.Map;
import com.aliyun.ha3engine.Client;
import com.aliyun.ha3engine.models.Config;
import com.aliyun.ha3engine.models.SearchQuery;
import com.aliyun.ha3engine.models.SearchRequestModel;
import com.aliyun.ha3engine.models.SearchResponseModel;
import com.aliyun.tea.TeaException;
/**
* @author alibaba
*/
public class SearchDoc {
public static void main(String[] args) throws Exception {
Config config = new Config();
// The username. You can view the username on the API Endpoint tab of the Instance Details page.
config.setAccessUserName("<userName>");
// The password. You can change the password on the API Endpoint tab of the Instance Details page.
config.setAccessPassWord("<password>");
// The name of the OpenSearch instance. You can view the instance name in the upper-left corner of the Instance Details page. Example: ha-cn-i7*****605.
config.setInstanceId("<instanceID>");
// To access the instance by using an internal endpoint, uncomment the following line and specify the endpoint.
//config.setEndpoint("ha-cn-******.ha.aliyuncs.com");
// To access the instance over the Internet, uncomment the following line and specify the HTTP proxy.
// config.setHttpProxy("http://Public IP address:Port number");
Client client = new Client(config);
try {
SearchRequestModel searchRequestModel = new SearchRequestModel();
SearchQuery searchQuery = new SearchQuery();
searchQuery.setSql("query=select * from table_odps&&kvpair=trace:INFO;formatType:full_json");
searchRequestModel.setQuery(searchQuery);
SearchResponseModel searchResponseModel = client.Search(searchRequestModel);
System.out.println("result:" + searchResponseModel.getBody());
} catch (TeaException e) {
System.out.println(e.getMessage());
Map<String, Object> abc = e.getData();
System.out.println(com.aliyun.teautil.Common.toJSONString(abc));
}
}
}Perform a contructed HA query
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import com.aliyun.ha3engine.Client;
import com.aliyun.ha3engine.models.*;
import com.aliyun.tea.TeaException;
/**
* @author alibaba
*/
public class SearchDoc {
public static void main(String[] args) throws Exception {
Config config = new Config();
// The username. You can view the username on the API Endpoint tab of the Instance Details page.
config.setAccessUserName("<userName>");
// The password. You can change the password on the API Endpoint tab of the Instance Details page.
config.setAccessPassWord("<password>");
// The name of the OpenSearch instance. You can view the instance name in the upper-left corner of the Instance Details page. Example: ha-cn-i7*****605.
config.setInstanceId("<instanceID>");
// To access the instance by using an internal endpoint, uncomment the following line and specify the endpoint.
//config.setEndpoint("ha-cn-******.ha.aliyuncs.com");
// To access the instance over the Internet, uncomment the following line and specify the HTTP proxy.
// config.setHttpProxy("http://Public IP address:Port number");
Client client = new Client(config);
try {
HaQuery haQuery = new HaQuery();
// Specify a query clause that contains the index and query content.
haQuery.setQuery("id:8148508889615505646");
// Specify the name of the cluster to be requested.
haQuery.setCluster("general");
// Specify an aggregate clause.
ArrayList<HaQueryAggregateClause> aggregateClauses = new ArrayList<>();
HaQueryAggregateClause agg = new HaQueryAggregateClause();
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"); // Specify a threshold value for sampled aggregation.
agg.setAggSamplerStep("5"); // Specify a step size for sampling.
agg.setMaxGroup("5"); // Specify the maximum number of groups that can be returned.
// Add one or more Aggregate objects.
aggregateClauses.add(agg);
haQuery.setAggregate(aggregateClauses);
// Specify a distinct clause.
ArrayList<HaQueryDistinctClause> DistinctClauses = new ArrayList<>();
HaQueryDistinctClause dist = new HaQueryDistinctClause();
dist.setDistKey("cate_id"); // Specify the name of a field to be extracted.
dist.setDistCount(1); // Specify the number of documents that are extracted each time.
dist.setDistTimes(1); // Specify the number of extraction times.
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 reserved 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 value for distinct extraction.
// Add one or more Distinct objects. The value of the dist parameter must be unique.
DistinctClauses.add(dist);
haQuery.setDistinct(DistinctClauses);
// Specify a sorting condition.
ArrayList<HaQuerySortClause> SortList = new ArrayList<>();
HaQuerySortClause sorter = new HaQuerySortClause();
// Specify the field that you want to sort.
sorter.setSortKey("id");
// Specify a sorting order. The plus sign (+) indicates ascending order. The minus sign (-) indicates descending order.
sorter.setSortOrder("+");
// Specify one or more sorting conditions.
SortList.add(sorter);
haQuery.setSort(SortList);
// Create a Config object and use the config clause to configure parameters for paging and for the data format of return results.
HaQueryconfigClause Queryconfig = new HaQueryconfigClause();
Queryconfig.setStart("1");
Queryconfig.setHit("100");
// Specify a format of return results. Formats such as XML, JSON, and Protobuf are supported.
Queryconfig.setFormat("JSON");
// Configure parameters for a CustomConfig object.
Map<String, String> CustomConfig = new HashMap<>();
CustomConfig.put("no_summary", "yes");
CustomConfig.put("qrs_chain", "search");
Queryconfig.setCustomConfig(CustomConfig);
haQuery.setConfig(Queryconfig);
// Specify filter conditions.
// You can specify one or more filter conditions. If you want to specify more than one filter condition, use AND or OR to join the filter conditions.
// The following operators are supported: '= > < <= >= !=
haQuery.setFilter("id>100 AND id<=1000");
// Specify parameters in the kvpairs clause.
Map<String, String> haKvpairs = new HashMap<>();
haKvpairs.put("duniqfield", "cate_id");
haQuery.setKvpairs(haKvpairs);
// Specify a custom query clause.
Map<String, String> CustomQuery = new HashMap<>();
CustomQuery.put("searcher_cache", "use:no");
haQuery.setCustomQuery(CustomQuery);
SearchRequestModel searchRequestModel = new SearchRequestModel();
SearchQuery searchQuery = new SearchQuery();
searchQuery.setQuery(client.buildHaSearchQuery(haQuery));
searchRequestModel.setQuery(searchQuery);
SearchResponseModel responseModel = client.Search(searchRequestModel);
System.out.println("result:" + responseModel.getBody());
} catch (TeaException e) {
System.out.println(e.getMessage());
Map<String, Object> abc = e.getData();
System.out.println(com.aliyun.teautil.Common.toJSONString(abc));
}
}
}Use an HA query string
import java.util.Map;
import com.aliyun.ha3engine.Client;
import com.aliyun.ha3engine.models.Config;
import com.aliyun.ha3engine.models.SearchQuery;
import com.aliyun.ha3engine.models.SearchRequestModel;
import com.aliyun.ha3engine.models.SearchResponseModel;
import com.aliyun.tea.TeaException;
/**
* @author alibaba
*/
public class SearchDoc {
public static void main(String[] args) throws Exception {
Config config = new Config();
// The username. You can view the username on the API Endpoint tab of the Instance Details page.
config.setAccessUserName("<userName>");
// The password. You can change the password on the API Endpoint tab of the Instance Details page.
config.setAccessPassWord("<password>");
// The name of the OpenSearch instance. You can view the instance name in the upper-left corner of the Instance Details page. Example: ha-cn-i7*****605.
config.setInstanceId("<instanceID>");
// To access the instance by using an internal endpoint, uncomment the following line and specify the endpoint.
//config.setEndpoint("ha-cn-******.ha.aliyuncs.com");
// To access the instance by using a public endpoint, uncomment the following line and specify the endpoint.
//config.setEndpoint("ha-cn-******.public.ha.aliyuncs.com");
// To access the instance over the Internet, uncomment the following line and specify the HTTP proxy.
// config.setHttpProxy("http://Public IP address:Port number");
Client client = new Client(config);
try {
SearchRequestModel haQueryRequestModel = new SearchRequestModel();
SearchQuery haRawQuery = new SearchQuery();
haRawQuery.setQuery("query=index_id:1&&config=start:0,hit:100,format:json&&cluster=general");
haQueryRequestModel.setQuery(haRawQuery);
SearchResponseModel responseModel = client.Search(haQueryRequestModel);
System.out.println("result:" + responseModel.getBody());
} catch (TeaException e) {
System.out.println(e.getMessage());
Map<String, Object> abc = e.getData();
System.out.println(com.aliyun.teautil.Common.toJSONString(abc));
}
}
}