An exists query with the Tablestore SDK for Java filters data based on whether a specified field exists.
Prerequisites
Install the Tablestore SDK for Java and initialize a client.
Feature description
An exists query (ExistsQuery, also called a NULL query or null-value query) matches rows in which a specified indexed field exists. A field does not exist if the corresponding column is not written to the data table. An empty array is also treated as a nonexistent array field.
When you call search, set the query type to ExistsQuery and specify a field name. To query a Nested parent field or subfield, wrap ExistsQuery in a NestedQuery. To match rows in which a field does not exist, add ExistsQuery to BoolQuery.mustNotQueries.
SearchResponse search(SearchRequest request)
The following example queries rows in which the city field exists. The query returns up to 10 rows and the total number of matches.
String tableName = "example_table";
String indexName = "example_index";
ExistsQuery existsQuery = new ExistsQuery();
existsQuery.setFieldName("city");
SearchQuery searchQuery = new SearchQuery();
searchQuery.setQuery(existsQuery);
searchQuery.setLimit(10);
searchQuery.setTrackTotalCount(SearchQuery.TRACK_TOTAL_COUNT);
SearchRequest request = new SearchRequest(tableName, indexName, searchQuery);
SearchRequest.ColumnsToGet columnsToGet = new SearchRequest.ColumnsToGet();
columnsToGet.setReturnAll(true);
request.setColumnsToGet(columnsToGet);
SearchResponse response = client.search(request);
System.out.println(response.getTotalCount());
System.out.println(response.getRows());
Parameters
Search request
request is a SearchRequest object that contains the following parameters.
|
Name |
Type |
Description |
|
tableName (required) |
String |
The name of the data table. |
|
indexName (required) |
String |
The name of the search index. |
|
searchQuery (required) |
SearchQuery |
The query condition and general query settings. |
|
columnsToGet (optional) |
SearchRequest.ColumnsToGet |
The columns to return. If this parameter is not configured, only primary key columns are returned. |
|
timeoutInMillisecond (optional) |
int |
The request-level query timeout in milliseconds. The default value is |
|
routingValues (optional) |
|
The primary key values that correspond to custom routing fields. Leave this parameter unset if custom routing is not configured. |
Query settings
request.searchQuery is a SearchQuery object that contains the following parameters.
|
Name |
Type |
Description |
|
query (required) |
Query |
The query condition. Set this parameter to an |
|
offset (optional) |
Integer |
The starting position of the query. |
|
limit (optional) |
Integer |
The maximum number of rows to return. Set this parameter to |
|
collapse (optional) |
Collapse |
The field collapse settings, which deduplicate results by a specified field. For configuration details, see Collapse query results. |
|
sort (optional) |
Sort |
The result sort order. For configuration details, see Sort and paginate results. |
|
trackTotalCount (optional) |
int |
The expected maximum number of matching rows to count. The default value is |
|
filter (optional) |
SearchFilter |
A filter that is applied to the results of |
|
aggregationList (optional) |
|
The aggregation settings. For configuration details, see Aggregation. |
|
groupByList (optional) |
|
The grouping settings. For configuration details, see Aggregation. |
|
token (optional) |
byte[] |
The pagination token. Set this parameter to the |
Query condition
request.searchQuery.query is an ExistsQuery object that contains the following parameter.
|
Name |
Type |
Description |
|
fieldName (required) |
String |
The name of the indexed field to query. |
Returned columns
request.columnsToGet is a SearchRequest.ColumnsToGet object that contains the following parameters.
|
Name |
Type |
Description |
|
columns (optional) |
|
The attribute columns to return. Set this parameter only if |
|
returnAll (optional) |
boolean |
Specifies whether to return all attribute columns from the data table. The default value is |
|
returnAllFromIndex (optional) |
boolean |
Specifies whether to return all indexed attribute columns. The default value is |
Return values
search returns a SearchResponse object. The following table describes the core fields.
|
Name |
Type |
Description |
|
totalCount |
long |
The number of matching rows. Call |
|
rows |
|
The rows returned by this query. Call |
|
searchHits |
|
The query hits. Call |
|
nextToken |
byte[] |
The next-page token. Call |
|
isAllSuccess |
boolean |
Indicates whether all index partitions were queried successfully. Call |
Scenario examples
Query rows in which a field does not exist
Add ExistsQuery to BoolQuery.mustNotQueries to match rows in which the city field does not exist.
ExistsQuery existsQuery = new ExistsQuery();
existsQuery.setFieldName("city");
BoolQuery boolQuery = new BoolQuery();
boolQuery.setMustNotQueries(Collections.singletonList(existsQuery));
SearchQuery searchQuery = new SearchQuery();
searchQuery.setQuery(boolQuery);
Query a Nested field
Wrap ExistsQuery in a NestedQuery. To query the parent field, set fieldName to items. To query a subfield, specify the full path, such as items.keyword. The following example queries rows in which the items.keyword subfield exists.
ExistsQuery existsQuery = new ExistsQuery();
existsQuery.setFieldName("items.keyword");
NestedQuery nestedQuery = new NestedQuery();
nestedQuery.setPath("items");
nestedQuery.setQuery(existsQuery);
nestedQuery.setScoreMode(ScoreMode.None);
SearchQuery searchQuery = new SearchQuery();
searchQuery.setQuery(nestedQuery);