Utilisez le SDK Java d'OpenSearch Retrieval Engine Edition pour interroger les données d'une instance. Cette rubrique présente quatre méthodes — deux pour les requêtes SQL et deux pour les requêtes Havenask — en utilisant le SDK com.aliyun.ha3engine. Ces quatre méthodes reposent sur le même modèle d'initialisation du client.
Prérequis
Avant de commencer, assurez-vous de disposer des éléments suivants :
Une instance OpenSearch Retrieval Engine Edition
Le SDK
com.aliyun.ha3engineajouté aux dépendances de votre projet
Configuration du client
Les quatre méthodes nécessitent les mêmes quatre paramètres pour initialiser le client. Récupérez-les depuis la page Instance Details de la console.
| Paramètre | Description | Emplacement |
|---|---|---|
<userName> |
Nom d'utilisateur pour l'authentification | Section API Endpoint de la page Instance Details |
<password> |
Mot de passe pour l'authentification | Section API Endpoint de la page Instance Details (modifiable) |
<instanceID> |
Nom de l'instance, par exemple ha-cn-i7*****605 |
Coin supérieur gauche de la page Instance Details |
<endpoint> |
Adresse d'accès de l'instance | Utilisez l'endpoint API pour un accès via VPC ; utilisez l'endpoint public pour un accès Internet |
Seules les méthodes de requête GET et POST sont prises en charge. La méthode par défaut est GET. Basculez vers POST si la longueur de la requête dépasse 30 Ko.
Requêtes SQL
Les requêtes SQL permettent d'exécuter des instructions SQL standard sur vos données OpenSearch. Vous pouvez construire une requête SQL sous forme d'objet structuré ou transmettre directement une chaîne de requête brute.
| Méthode | Cas d'utilisation |
|---|---|
| Requête construite | Lorsque vous devez définir programmatiquement les champs de requête et les paires clé-valeur (kvpairs) |
| Chaîne de requête | Lorsque la requête est simple et que vous préférez un format concis en une seule chaîne |
Requête SQL à l'aide d'une requête construite
Construisez un objet SQLQuery, définissez l'instruction de requête et les paires clé-valeur, puis appelez client.buildSQLSearchQuery() pour assembler la requête.
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 in the API Endpoint section of the Instance Details page.
config.setAccessUserName("<userName>");
// The password. You can change the password in the API Endpoint section of the Instance Details page.
config.setAccessPassWord("<password>");
// The name of the instance. You can view the name in the upper-left corner of the Instance Details page. Example: ha-cn-i7*****605.
config.setInstanceId("<instanceID>");
// The endpoint that is used to access the instance. Specify the API endpoint if the instance is accessed over a virtual private cloud (VPC). Specify the public endpoint if the instance is accessed over the Internet.
config.setEndpoint("<endpoint>");
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);
// The request method. Only the GET and POST methods are supported. Default value: GET. If the length of the query exceeds 30 KB, use the POST method.
searchRequestModel.setMethod("POST");
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));
}
}
}
Requête SQL à l'aide d'une chaîne de requête
Transmettez l'instruction SQL et les paires clé-valeur sous forme d'une seule chaîne formatée directement à searchQuery.setSql(). Privilégiez cette approche pour les requêtes simples qui ne nécessitent pas de configuration programmatique des champs individuels.
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 in the API Endpoint section of the Instance Details page.
config.setAccessUserName("<userName>");
// The password. You can change the password in the API Endpoint section of the Instance Details page.
config.setAccessPassWord("<password>");
// The name of the instance. You can view the name in the upper-left corner of the Instance Details page. Example: ha-cn-i7*****605.
config.setInstanceId("<instanceID>");
// The endpoint that is used to access the instance. Specify the API endpoint if the instance is accessed over a VPC. Specify the public endpoint if the instance is accessed over the Internet.
config.setEndpoint("<endpoint>");
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);
// The request method. Only the GET and POST methods are supported. Default value: GET. If the length of the query exceeds 30 KB, use the POST method.
searchRequestModel.setMethod("POST");
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));
}
}
}
Requêtes Havenask
Les requêtes Havenask permettent d'accéder aux fonctionnalités spécifiques à Havenask, telles que les clauses d'agrégation, de distinction et de tri. Comme pour les requêtes SQL, vous pouvez construire une requête Havenask sous forme d'objet structuré ou transmettre directement une chaîne de requête brute.
| Méthode | Cas d'utilisation |
|---|---|
| Requête construite | Lorsque vous avez besoin d'un contrôle précis sur les clauses d'agrégation, de distinction, de tri, de filtrage ou les paires clé-valeur |
| Chaîne de requête | Lorsque la requête est simple et que vous préférez un format concis en une seule chaîne |
Requête Havenask à l'aide d'une requête construite
Construisez un objet HaQuery et configurez chaque clause — agrégation, distinction, tri, filtre, paires clé-valeur et requête personnalisée — puis appelez client.buildHaSearchQuery() pour assembler la requête.
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 in the API Endpoint section of the Instance Details page.
config.setAccessUserName("<userName>");
// The password. You can change the password in the API Endpoint section of the Instance Details page.
config.setAccessPassWord("<password>");
// The name of the instance. You can view the name in the upper-left corner of the Instance Details page. Example: ha-cn-i7*****605.
config.setInstanceId("<instanceID>");
// The endpoint that is used to access the instance. Specify the API endpoint if the instance is accessed over a VPC. Specify the public endpoint if the instance is accessed over the Internet.
config.setEndpoint("<endpoint>");
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);
// Configure 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 (+) specifies an ascending order. The minus sign (-) specifies a 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");
// Specify parameters for a custom config 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 supported operators are equal to (=), greater than (>), less than (<), less than or equal to (<=), greater than or equal to (>=), and not equal to (!=).
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);
// The request method. Only the GET and POST methods are supported. Default value: GET. If the length of the query exceeds 30 KB, use the POST method.
searchRequestModel.setMethod("POST");
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));
}
}
}
Requête Havenask à l'aide d'une chaîne de requête
Transmettez les paramètres de requête, de configuration et de cluster sous forme d'une seule chaîne formatée directement à haRawQuery.setQuery(). Privilégiez cette approche pour les requêtes simples où la construction d'objets de clause individuels n'est pas nécessaire.
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 in the API Endpoint section of the Instance Details page.
config.setAccessUserName("<userName>");
// The password. You can change the password in the API Endpoint section of the Instance Details page.
config.setAccessPassWord("<password>");
// The name of the instance. You can view the name in the upper-left corner of the Instance Details page. Example: ha-cn-i7*****605.
config.setInstanceId("<instanceID>");
// The endpoint that is used to access the instance. Specify the API endpoint if the instance is accessed over a VPC. Specify the public endpoint if the instance is accessed over the Internet.
config.setEndpoint("<endpoint>");
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);
// The request method. Only the GET and POST methods are supported. Default value: GET. If the length of the query exceeds 30 KB, use the POST method.
haQueryRequestModel.setMethod("POST");
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));
}
}
}
Étapes suivantes
[Référence de la syntaxe SQL d'OpenSearch Retrieval Engine Edition]()
[Référence de la syntaxe des requêtes Havenask]()