You can use the Tablestore SDK for Java to query subfields of Object or Nested JSON fields in a search index. Object fields do not preserve child-object boundaries, whereas Nested fields preserve them.
Prerequisites
Install the Tablestore SDK for Java and initialize the client.
The target field is configured as a JSON field in the search index, and
jsonTypeis set toOBJECTorNESTED. For more information, see Create a search index.
Feature description
JSON queries do not use a dedicated query type. Select a query method based on the jsonType of the JSON field in the search index.
|
JSON type |
Field relationships |
Query method |
|
Object |
Does not preserve the boundaries of objects in an array. Different query conditions can be satisfied by different objects. |
Directly use a query type suitable for the subfield type and matching requirements. Specify the full path in each subfield name. |
|
Nested |
Stores each object in an array as an independent child row and preserves the relationships among fields in the same object. |
Wrap the subquery in a |
For example, assume that the address column in a table is of the String type and stores the following JSON array:
[
{ "country": "China", "city": "hangzhou" },
{ "country": "usa", "city": "Seattle" }
]
If you query for both country="China" and city="Seattle", the row is returned when address is configured as an Object field because different objects can satisfy the two conditions. The row is not returned when address is configured as a Nested field because no single object satisfies both conditions.
Call search to run a JSON query.
SearchResponse search(SearchRequest request)
The subFieldSchemas of a JSON field cannot contain Vector fields.
Query an Object field
The following example queries rows in which address.country is China and address.city is Seattle. Because address is an Object field, different objects can satisfy the two conditions.
String tableName = "example_table";
String indexName = "example_index";
TermQuery countryQuery = new TermQuery();
countryQuery.setFieldName("address.country");
countryQuery.setTerm(ColumnValue.fromString("China"));
TermQuery cityQuery = new TermQuery();
cityQuery.setFieldName("address.city");
cityQuery.setTerm(ColumnValue.fromString("Seattle"));
BoolQuery objectQuery = new BoolQuery();
objectQuery.setMustQueries(Arrays.asList(countryQuery, cityQuery));
SearchQuery searchQuery = new SearchQuery();
searchQuery.setQuery(objectQuery);
searchQuery.setLimit(10);
SearchRequest request =
new SearchRequest(tableName, indexName, searchQuery);
SearchResponse response = client.search(request);
System.out.println(response.getRows());
Query a Nested field
The following example queries rows in which the same object in address has an address.country value of China and an address.city value of Seattle. For more information about query methods and parameters for Nested fields, see Nested query.
String tableName = "example_table";
String indexName = "example_index";
TermQuery countryQuery = new TermQuery();
countryQuery.setFieldName("address.country");
countryQuery.setTerm(ColumnValue.fromString("China"));
TermQuery cityQuery = new TermQuery();
cityQuery.setFieldName("address.city");
cityQuery.setTerm(ColumnValue.fromString("Seattle"));
BoolQuery childQuery = new BoolQuery();
childQuery.setMustQueries(Arrays.asList(countryQuery, cityQuery));
NestedQuery nestedQuery = new NestedQuery();
nestedQuery.setPath("address");
nestedQuery.setQuery(childQuery);
nestedQuery.setScoreMode(ScoreMode.None);
SearchQuery searchQuery = new SearchQuery();
searchQuery.setQuery(nestedQuery);
searchQuery.setLimit(10);
SearchRequest request =
new SearchRequest(tableName, indexName, searchQuery);
SearchResponse response = client.search(request);
System.out.println(response.getRows());
Parameters
Search request
request is of the SearchRequest type and contains the following parameters.
|
Name |
Type |
Description |
|
tableName (required) |
String |
The name of the table. |
|
indexName (required) |
String |
The name of the search index. |
|
searchQuery (required) |
SearchQuery |
The query condition and common query configurations. |
|
columnsToGet (optional) |
SearchRequest.ColumnsToGet |
The configurations of the columns to return. If you do not configure this parameter, only primary key columns are returned. |
|
timeoutInMillisecond (optional) |
int |
The request-level query timeout period in milliseconds. The default value is |
|
routingValues (optional) |
|
The primary key values that correspond to custom routing fields. You do not need to configure this parameter if custom routing is not used. |
Query configuration
request.searchQuery is of the SearchQuery type and contains the following parameters.
|
Name |
Type |
Description |
|
query (required) |
Query |
The query condition. For an Object field, directly set a query type suitable for the subfield type and matching requirements. For a Nested field, set this parameter to a |
|
offset (optional) |
Integer |
The position from which the current query starts. |
|
limit (optional) |
Integer |
The maximum number of rows to return. If you set this parameter to |
|
highlight (optional) |
Highlight |
The summary and highlighting configurations. For a Nested field, configure summary and highlighting for matching child rows by using |
|
collapse (optional) |
Collapse |
The result collapsing configuration, which removes duplicate results based on the specified field. |
|
sort (optional) |
Sort |
The sorting method for the results. |
|
trackTotalCount (optional) |
int |
The expected maximum number of matched rows to count. The default value is |
|
filter (optional) |
SearchFilter |
A filter applied to the results of |
|
aggregationList (optional) |
|
The aggregation configurations. |
|
groupByList (optional) |
|
The grouping configurations. |
|
token (optional) |
byte[] |
The pagination token. Set this parameter to the |
Nested query condition
When you query a Nested field, request.searchQuery.query is of the NestedQuery type and contains the following parameters.
|
Name |
Type |
Description |
|
path (required) |
String |
The path of the Nested field to query. To query a multi-level Nested field, specify the full path of the target field. |
|
query (required) |
Query |
The query condition to run on the child rows at |
|
scoreMode (required) |
ScoreMode |
The method used to calculate the parent-row score when multiple child rows match. |
|
innerHits (optional) |
InnerHits |
The configurations used to return, sort, paginate, and highlight matching child rows. If you do not configure this parameter, details of matching child rows are not returned. |
|
weight (optional) |
float |
The query weight. The default value is |
Child-row return configuration
request.searchQuery.query.innerHits is of the InnerHits type and contains the following parameters.
|
Name |
Type |
Description |
|
sort (optional) |
Sort |
The sorting method for matching child rows. |
|
offset (optional) |
Integer |
The position from which matching child rows are returned. |
|
limit (optional) |
Integer |
The maximum number of matching child rows to return. The default value is |
|
highlight (optional) |
Highlight |
The summary and highlighting configurations for matching child rows. |
Returned columns
request.columnsToGet is of the SearchRequest.ColumnsToGet type and contains the following parameters.
|
Name |
Type |
Description |
|
columns (optional) |
|
The names of the attribute columns to return. Configure this parameter only when both |
|
returnAll (optional) |
boolean |
Specifies whether to return all attribute columns in the table. The default value is |
|
returnAllFromIndex (optional) |
boolean |
Specifies whether to return all attribute columns that are indexed. The default value is |
Response
The search method returns a SearchResponse object. The following table describes the main fields.
|
Name |
Type |
Description |
|
totalCount |
long |
The number of matched rows. Call |
|
rows |
|
The rows returned by the current query. Call |
|
searchHits |
|
The query hits. Call |
|
nextToken |
byte[] |
The token for the next page. Call |
|
isAllSuccess |
boolean |
Indicates whether all index partitions were queried. Call |