Les exemples suivants montrent comment exécuter des requêtes booléennes.
Interroger les lignes qui répondent à toutes les conditions des sous-requêtes
Lorsque vous interrogez des lignes qui répondent à toutes les conditions des sous-requêtes, vous pouvez décider de calculer ou non les scores de pertinence pour les lignes en fonction de vos besoins métier.
Calculer les scores de pertinence pour les lignes
L'exemple de code suivant montre comment exécuter une requête booléenne pour interroger les lignes qui répondent à toutes les conditions des sous-requêtes et calculer les scores de pertinence pour ces lignes.
/**
* Perform a Boolean query to query the rows that meet all subquery conditions.
* @param client
*/
public static void andQuery(SyncClient client){
/**
* Condition 1: Perform a range query to query the rows in which the value of the Col_Long column is greater than 3.
*/
RangeQuery rangeQuery = new RangeQuery();
rangeQuery.setFieldName("Col_Long");
rangeQuery.greaterThan(ColumnValue.fromLong(3));
/**
* Condition 2: Perform a match query to query the rows in which the value of the Col_Keyword column is hangzhou.
*/
MatchQuery matchQuery = new MatchQuery();
matchQuery.setFieldName("Col_Keyword");
matchQuery.setText("hangzhou");
SearchQuery searchQuery = new SearchQuery();
{
/**
* Construct a Boolean query whose query results meet both Condition 1 and Condition 2.
*/
BoolQuery boolQuery = new BoolQuery();
boolQuery.setMustQueries(Arrays.asList(rangeQuery, matchQuery));
searchQuery.setQuery(boolQuery);
//searchQuery.setGetTotalCount(true);// Set the GetTotalCount parameter to true to return the total number of rows that meet the query conditions.
SearchRequest searchRequest = new SearchRequest("<TABLE_NAME>", "<SEARCH_INDEX_NAME>", searchQuery);
// You can configure the columnsToGet parameter to specify the columns to return or specify that all columns are returned. If you do not configure this parameter, only the primary key columns are returned.
//SearchRequest.ColumnsToGet columnsToGet = new SearchRequest.ColumnsToGet();
//columnsToGet.setReturnAll(true); // Set the ReturnAll parameter to true to return all columns.
//columnsToGet.setColumns(Arrays.asList("ColName1","ColName2")); // Specify the columns that you want to return.
//searchRequest.setColumnsToGet(columnsToGet);
SearchResponse resp = client.search(searchRequest);
//System.out.println("TotalCount: " + resp.getTotalCount()); // Specify that the total number of rows that meet the query conditions instead of the number of returned rows is displayed.
System.out.println("Row: " + resp.getRows());
}
}
Ne pas calculer les scores de pertinence pour les lignes
L'exemple de code suivant montre comment exécuter une requête booléenne pour interroger les lignes qui répondent à toutes les conditions des sous-requêtes sans calculer les scores de pertinence pour ces lignes.
/**
* Perform a Boolean query to query the rows that meet all subquery conditions without calculating the relevance scores for the rows.
* @param client
*/
public static void filtersQuery(SyncClient client){
/**
* Condition 1: Perform a range query to query the rows in which the value of the Col_Long column is greater than 3.
*/
RangeQuery rangeQuery = new RangeQuery();
rangeQuery.setFieldName("Col_Long");
rangeQuery.greaterThan(ColumnValue.fromLong(3));
/**
* Condition 2: Perform a match query to query the rows in which the value of the Col_Keyword column is hangzhou.
*/
MatchQuery matchQuery = new MatchQuery();
matchQuery.setFieldName("Col_Keyword");
matchQuery.setText("hangzhou");
SearchQuery searchQuery = new SearchQuery();
{
/**
* Construct a Boolean query whose query results meet both Condition 1 and Condition 2.
*/
BoolQuery boolQuery = new BoolQuery();
boolQuery.setFilterQueries(Arrays.asList(rangeQuery, matchQuery));
searchQuery.setQuery(boolQuery);
//searchQuery.setGetTotalCount(true);// Set the GetTotalCount parameter to true to return the total number of rows that meet the query conditions.
SearchRequest searchRequest = new SearchRequest("sampleTable", "sampleSearchIndex", searchQuery);
// You can configure the columnsToGet parameter to specify the columns to return or specify that all columns are returned. If you do not configure this parameter, only the primary key columns are returned.
//SearchRequest.ColumnsToGet columnsToGet = new SearchRequest.ColumnsToGet();
//columnsToGet.setReturnAll(true); // Set the ReturnAll parameter to true to return all columns.
//columnsToGet.setColumns(Arrays.asList("ColName1","ColName2")); // Specify the columns that you want to return.
//searchRequest.setColumnsToGet(columnsToGet);
SearchResponse resp = client.search(searchRequest);
//System.out.println("TotalCount: " + resp.getTotalCount()); // Specify that the total number of rows that meet the query conditions instead of the number of returned rows is displayed.
System.out.println("Row: " + resp.getRows());
}
}
Interroger les lignes qui répondent à au moins l'une des conditions des sous-requêtes
L'exemple de code suivant montre comment exécuter une requête booléenne pour interroger les lignes qui répondent à au moins l'une des conditions de sous-requête spécifiées :
/**
* Perform a Boolean query to query the rows that meet at least one of the specified subquery conditions.
* @param client
*/
public static void orQuery(SyncClient client) {
/**
* Condition 1: Perform a range query to query the rows in which the value of the Col_Long column is greater than 3.
*/
RangeQuery rangeQuery = new RangeQuery();
rangeQuery.setFieldName("Col_Long");
rangeQuery.greaterThan(ColumnValue.fromLong(3));
/**
* Condition 2: Perform a match query to query the rows in which the value of the Col_Keyword column is hangzhou.
*/
MatchQuery matchQuery = new MatchQuery();
matchQuery.setFieldName("Col_Keyword");
matchQuery.setText("hangzhou");
SearchQuery searchQuery = new SearchQuery();
{
/**
* Construct a Boolean query whose query results meet at least one of Condition 1 and Condition 2.
*/
BoolQuery boolQuery = new BoolQuery();
boolQuery.setShouldQueries(Arrays.asList(rangeQuery, matchQuery));
boolQuery.setMinimumShouldMatch(1); // Specify that the results meet at least one of the subquery conditions.
searchQuery.setQuery(boolQuery);
//searchQuery.setGetTotalCount(true);// Set the GetTotalCount parameter to true to return the total number of rows that meet the query conditions.
SearchRequest searchRequest = new SearchRequest("<TABLE_NAME>", "<SEARCH_INDEX_NAME>", searchQuery);
// You can configure the columnsToGet parameter to specify the columns to return or specify that all columns are returned. If you do not configure this parameter, only the primary key columns are returned.
//SearchRequest.ColumnsToGet columnsToGet = new SearchRequest.ColumnsToGet();
//columnsToGet.setReturnAll(true); // Set the ReturnAll parameter to true to return all columns.
//columnsToGet.setColumns(Arrays.asList("ColName1","ColName2")); // Specify the columns that you want to return.
//searchRequest.setColumnsToGet(columnsToGet);
SearchResponse resp = client.search(searchRequest);
//System.out.println("TotalCount: " + resp.getTotalCount()); // Specify that the total number of rows that meet the query conditions instead of the number of returned rows is displayed.
System.out.println("Row: " + resp.getRows());
}
}
Interroger les lignes qui ne répondent à aucune condition des sous-requêtes
L'exemple de code suivant montre comment exécuter une requête booléenne pour interroger les lignes qui ne répondent à aucune condition des sous-requêtes :
/**
* Perform a Boolean query to query the rows that do not meet any subquery conditions.
* @param client
*/
public static void notQuery(SyncClient client) {
/**
* Condition 1: Perform a match query to query the rows in which the value of the Col_Keyword column is hangzhou.
*/
MatchQuery matchQuery = new MatchQuery();
matchQuery.setFieldName("Col_Keyword");
matchQuery.setText("hangzhou");
SearchQuery searchQuery = new SearchQuery();
{
/**
* Construct a Boolean query whose query results do not meet Condition 1.
*/
BoolQuery boolQuery = new BoolQuery();
boolQuery.setMustNotQueries(Arrays.asList(matchQuery));
searchQuery.setQuery(boolQuery);
//searchQuery.setGetTotalCount(true);// Set the GetTotalCount parameter to true to return the total number of rows that meet the query conditions.
SearchRequest searchRequest = new SearchRequest("<TABLE_NAME>", "<SEARCH_INDEX_NAME>", searchQuery);
// You can configure the columnsToGet parameter to specify the columns to return or specify that all columns are returned. If you do not configure this parameter, only the primary key columns are returned.
//SearchRequest.ColumnsToGet columnsToGet = new SearchRequest.ColumnsToGet();
//columnsToGet.setReturnAll(true); // Set the ReturnAll parameter to true to return all columns.
//columnsToGet.setColumns(Arrays.asList("ColName1","ColName2")); // Specify the columns that you want to return.
//searchRequest.setColumnsToGet(columnsToGet);
SearchResponse resp = client.search(searchRequest);
//System.out.println("TotalCount: " + resp.getTotalCount()); // Specify that the total number of rows that meet the query conditions instead of the number of returned rows is displayed.
System.out.println("Row: " + resp.getRows());
}
}
Interroger les lignes qui répondent à une combinaison de conditions de sous-requêtes
L'exemple de code suivant montre comment exécuter une requête booléenne pour interroger les lignes qui répondent à une combinaison de conditions de sous-requêtes, soit (col2<4 ou col3<5) ou (col2 = 4 et (col3 = 5 ou col3 =6)). Dans la combinaison de conditions de sous-requêtes ci-dessus, les sous-requêtes booléennes sont connectées par l'opérateur AND ou OR.
/**
* (col2<4 or col3<5) or (col2 = 4 and (col3 = 5 or col3 =6))
* The Boolean subqueries are connected by the AND or OR operator.
* @param client
*/
private static void boolQuery2(SyncClient client){
// Condition 1: The value of the col2 column is less than 4.
RangeQuery rangeQuery1 = new RangeQuery();
rangeQuery1.setFieldName("col2");
rangeQuery1.lessThan(ColumnValue.fromLong(4));
// Condition 2: The value of the col3 column is less than 5.
RangeQuery rangeQuery2 = new RangeQuery();
rangeQuery2.setFieldName("col3");
rangeQuery2.lessThan(ColumnValue.fromLong(5));
// Condition 3: The value of the col2 column is equal to 4.
TermQuery termQuery = new TermQuery();
termQuery.setFieldName("col2");
termQuery.setTerm(ColumnValue.fromLong(4));
// Condition 4: The value of the col3 column is equal to 5 or the value of the col3 column is equal to 6.
TermsQuery termsQuery = new TermsQuery();
termsQuery.setFieldName("col3");
termsQuery.addTerm(ColumnValue.fromLong(5));
termsQuery.addTerm(ColumnValue.fromLong(6));
SearchQuery searchQuery = new SearchQuery();
List<Query> queryList1 = new ArrayList<>();
queryList1.add(rangeQuery1);
queryList1.add(rangeQuery2);
// Composite condition 1: The value of the col2 column is less than 4 or the value of the col3 column is less than 5.
BoolQuery boolQuery1 = new BoolQuery();
boolQuery1.setShouldQueries(queryList1);
// Composite condition 2: The value of the col2 column is equal to 4 and the value of the col3 column is equal to 5 or 6.
List<Query> queryList2 = new ArrayList<>();
queryList2.add(termQuery);
queryList2.add(termsQuery);
BoolQuery boolQuery2 = new BoolQuery();
boolQuery2.setMustQueries(queryList2);
// Composite conditions: The value of the col2 column is less than 4 or the value of the col3 column is less than 5, or the value of the col2 column is equal to 4 and the value of the col3 column is equal to 5 or 6.
List<Query> queryList3 = new ArrayList<>();
queryList3.add(boolQuery1);
queryList3.add(boolQuery2);
BoolQuery boolQuery = new BoolQuery();
boolQuery.setShouldQueries(queryList3);
searchQuery.setQuery(boolQuery);
//searchQuery.setGetTotalCount(true);// Set the GetTotalCount parameter to true to return the total number of rows that meet the query conditions.
SearchRequest searchRequest = new SearchRequest("<TABLE_NAME>", "<SEARCH_INDEX_NAME>", searchQuery);
// You can configure the columnsToGet parameter to specify the columns to return or specify that all columns are returned. If you do not configure this parameter, only the primary key columns are returned.
//SearchRequest.ColumnsToGet columnsToGet = new SearchRequest.ColumnsToGet();
//columnsToGet.setReturnAll(true); // Set the ReturnAll parameter to true to return all columns.
//columnsToGet.setColumns(Arrays.asList("ColName1","ColName2")); // Specify the columns that you want to return.
//searchRequest.setColumnsToGet(columnsToGet);
SearchResponse response = client.search(searchRequest);
//System.out.println("TotalCount: " + resp.getTotalCount()); // Specify that the total number of rows that meet the query conditions instead of the number of returned rows is displayed.
System.out.println(response.getRows());
}