This query is similar to a term query. A terms query supports multiple terms. A row of data is returned if at least one of the keywords matches the field value. Terms queries can be used in the same manner as the IN operator in SQL statements.

Prerequisites

  • The OTSClient is initialized. For more information, see Initialization.
  • A data table is created. Data is written to the table.
  • A search index is created for the data table. For more information, see Create search indexes.

Parameters

ParameterDescription
queryThe type of the query. Set the value to TermsQuery.
fieldNameThe name of the field that you want to match.
termsThe keywords that are used to match the field values when you perform a terms query. You can specify up to 1,024 keywords.

A row of data is returned if at least one of the keywords matches the field value.

getTotalCountSpecifies whether to return the total number of rows that meet the query conditions. The default value of this parameter is false, which indicates that the total number of rows that meet the query conditions is not returned.

If you set this parameter to true, the query performance is compromised.

tableNameThe name of the data table.
indexNameThe name of the search index.
columnsToGetSpecifies whether to return all columns of each row that meets the query conditions. You can configure returnAll and columns for this parameter.

The default value of returnAll is false, which indicates that not all columns are returned. In this case, you can use columns to specify the columns that you want to return. If you do not specify the columns that you want to return, only the primary key columns are returned.

If you set returnAll to true, all columns are returned.

Examples

/**
 * Search the table for rows in which the value of Col_Keyword is "hangzhou" or "xi'an". 
 * @param client
 */
private static void termQuery(SyncClient client) {
    SearchQuery searchQuery = new SearchQuery();
    TermsQuery termsQuery = new TermsQuery(); // Set the query type to TermsQuery. 
    termsQuery.setFieldName("Col_Keyword"); // Specify the name of the field that you want to match. 
    termsQuery.addTerm(ColumnValue.fromString("hangzhou")); // Specify the keyword that you want to match. 
    termsQuery.addTerm(ColumnValue.fromString("xi'an")); // Specify the keyword that you want to match. 
    searchQuery.setQuery(termsQuery);
    //searchQuery.setGetTotalCount(true); // Specify that the total number of rows that meet the query conditions is returned. 

    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 ReturnAll 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 rows that are returned is displayed. 
    System.out.println("Row: " + resp.getRows());
}