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

Prerequisites

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

Parameters

Parameter Description
query The type of the query. Set the value to TermsQuery.
fieldName The name of the field that you want to match.
terms The 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 one of the keywords matches field values.

getTotalCount Specifies whether to return the total number of rows that match the query conditions. By default, this parameter is set to false. This value indicates that the total number of rows that match the query conditions is not returned.

If this parameter is set to true, query performance is affected.

tableName The name of the data table.
indexName The name of the search index.
columnsToGet Specifies whether to return all columns of each matched row. You can configure ReturnAll and Columns for this parameter.

By default, ReturnAll is set to false. This value indicates that not all columns of each matched row are returned. If ReturnAll is set to false, you can use Columns to specify the columns to return. If you do not specify the columns to return, only the primary key columns are returned.

If ReturnAll is set to true, all columns of each matched row 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 matched rows 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 to return. 
    //searchRequest.setColumnsToGet(columnsToGet);

    SearchResponse resp = client.search(searchRequest);
    //System.out.println("TotalCount: " + resp.getTotalCount()); // Specify that the total number of matched rows but not returned rows is displayed. 
    System.out.println("Row: " + resp.getRows());
}