You can use an SDK to query data in single-level and multi-level nested type fields. When you perform a nested query, you can use the highlight feature to highlight keywords in the results. For more information about highlighting, see highlight.
Example of a single-level nested query
The following example shows how to query for data where `col_nested.nested_1` is `tablestore`. In this example, `col_nested` is a nested field whose child rows contain the `nested_1` and `nested_2` columns.
private static void nestedQuery(SyncClient client) {
SearchQuery searchQuery = new SearchQuery();
NestedQuery nestedQuery = new NestedQuery(); // Set the query type to NestedQuery.
nestedQuery.setPath("col_nested"); // Set the path of the nested column.
TermQuery termQuery = new TermQuery(); // Construct the subquery for NestedQuery.
termQuery.setFieldName("col_nested.nested_1"); // Set the column name. Note that it includes the path of the nested column.
termQuery.setTerm(ColumnValue.fromString("tablestore")); // Set the value to query.
nestedQuery.setQuery(termQuery);
nestedQuery.setScoreMode(ScoreMode.None);
searchQuery.setQuery(nestedQuery);
//searchQuery.setGetTotalCount(true); // Set this to return the total number of matched rows.
SearchRequest searchRequest = new SearchRequest("<TABLE_NAME>", "<SEARCH_INDEX_NAME>", searchQuery);
// Use the columnsToGet parameter to specify the columns to return or to return all columns. If you do not set this parameter, only the primary key columns are returned by default.
//SearchRequest.ColumnsToGet columnsToGet = new SearchRequest.ColumnsToGet();
//columnsToGet.setReturnAll(true); // Set this to return all columns.
//columnsToGet.setColumns(Arrays.asList("ColName1","ColName2")); // Set this to return specified columns.
//searchRequest.setColumnsToGet(columnsToGet);
SearchResponse resp = client.search(searchRequest);
//System.out.println("TotalCount: " + resp.getTotalCount()); // Print the total number of matched rows, not the number of returned rows.
System.out.println("Row: " + resp.getRows());
}
Example of a multi-level nested query
The following example shows how to query for data where `col_nested.nested_2.nested_2_2` is `tablestore`. In this example, `col_nested` is a nested field whose child rows contain the `nested_1` and `nested_2` columns. The `nested_2` column is also a nested field whose child rows contain the `nested_2_1` and `nested_2_2` columns.
private static void nestedQuery(SyncClient client) {
SearchQuery searchQuery = new SearchQuery();
NestedQuery nestedQuery = new NestedQuery(); // Set the query type to NestedQuery.
nestedQuery.setPath("col_nested.nested_2"); // Set the path of the nested column, which is the parent path of the field to query.
TermQuery termQuery = new TermQuery(); // Construct the subquery for NestedQuery.
termQuery.setFieldName("col_nested.nested_2.nested_2_2"); // Set the column name, which is the full path of the field to query.
termQuery.setTerm(ColumnValue.fromString("tablestore")); // Set the value to query.
nestedQuery.setQuery(termQuery);
nestedQuery.setScoreMode(ScoreMode.None);
searchQuery.setQuery(nestedQuery);
//searchQuery.setGetTotalCount(true); // Set this to return the total number of matched rows.
SearchRequest searchRequest = new SearchRequest("<TABLE_NAME>", "<SEARCH_INDEX_NAME>", searchQuery);
// Use the columnsToGet parameter to specify the columns to return or to return all columns. If you do not set this parameter, only the primary key columns are returned by default.
//SearchRequest.ColumnsToGet columnsToGet = new SearchRequest.ColumnsToGet();
//columnsToGet.setReturnAll(true); // Set this to return all columns.
//columnsToGet.setColumns(Arrays.asList("ColName1","ColName2")); // Set this to return specified columns.
//searchRequest.setColumnsToGet(columnsToGet);
SearchResponse resp = client.search(searchRequest);
//System.out.println("TotalCount: " + resp.getTotalCount()); // Print the total number of matched rows, not the number of returned rows.
System.out.println("Row: " + resp.getRows());
}
Example of a composite nested query
Query requirements
Assume that a data table has two columns: `col_string` (String) and `col_nested` (String). The `col_nested` column stores data in JSON format. The following table shows sample data from this data table.
Note
A row number column is added to each row for demonstration purposes.
|
Row number
|
col_string
|
col_nested
|
|
1
|
a
|
[{"col_keyword": "tablestore"},{"col_keyword": "searchindex","col_long": 1}]
|
|
2
|
b
|
[{"col_keyword": "tablestore","col_long": 1}]
|
|
3
|
c
|
[{"col_keyword": "searchindex"},{"col_long": 1}]
|
Assume that you have the following query requirements for the data in the `col_nested` column:
-
A single child row meets multiple query conditions
For example, you can query for rows where a single child row in the `col_nested` column meets both of the following conditions: the value of the `col_keyword` column is `tablestore` and the value of the `col_long` column is not empty.
-
Different child rows meet multiple query conditions
For example, you can query for rows where one child row in the `col_nested` column has a `col_keyword` value of `tablestore`, and another child row has a `col_long` value that is not empty.
To meet these query requirements, you can perform the following steps:
-
Create a search index for the data table and set the `col_nested` column as a nested type in the search index.
The `col_nested` column contains two sub-fields: `col_keyword` (Keyword) and `col_long` (Long).
-
Use the appropriate query method based on your requirements.
-
To perform a query where a single child row satisfies multiple query conditions, configure multiple BoolQueries within a NestedQuery.
-
To query for different child rows that meet multiple conditions, you can set multiple `NestedQuery` conditions under a `BoolQuery`.
The following examples show how to query data. You can refer to the example that matches your query requirements.
Example of a query where a single child row meets multiple conditions
The following example shows how to query for rows where a single child row in the `col_nested` column meets both of these conditions: `col_nested.col_keyword is "tablestore" and `col_nested.col_long is not empty.
Based on the sample data, only the data in row 2 meets the query conditions.
public static void nestedQuery(SyncClient client) {
// Condition 1: The value of the col_keyword column in the child row of col_nested must be "tablestore".
TermQuery termQuery = new TermQuery();
termQuery.setFieldName("col_nested.col_keyword");
termQuery.setTerm(ColumnValue.fromString("tablestore"));
// Condition 2: The col_long column in the child row of col_nested must not be empty.
ExistsQuery existsQuery = new ExistsQuery();
existsQuery.setFieldName("col_nested.col_long");
// Use the AND condition of BoolQuery to query for child rows that meet both conditions.
List<Query> mustQueries = new ArrayList<>();
mustQueries.add(termQuery);
mustQueries.add(existsQuery);
BoolQuery boolQuery = new BoolQuery();
boolQuery.setMustQueries(mustQueries);
// Set a BoolQuery within the NestedQuery to require a child row to meet multiple query conditions at the same time.
NestedQuery nestedQuery = new NestedQuery(); // Set the query type to NestedQuery.
nestedQuery.setPath("col_nested"); // Set the path of the nested column, which is the parent path of the field to query.
nestedQuery.setQuery(boolQuery);
nestedQuery.setScoreMode(ScoreMode.None);
SearchQuery searchQuery = new SearchQuery();
searchQuery.setQuery(nestedQuery);
SearchRequest searchRequest = new SearchRequest("<TABLE_NAME>", "<SEARCH_INDEX_NAME>", searchQuery);
// Use the columnsToGet parameter to specify the columns to return or to return all columns. If you do not set this parameter, only the primary key columns are returned by default.
//SearchRequest.ColumnsToGet columnsToGet = new SearchRequest.ColumnsToGet();
//columnsToGet.setReturnAll(true); // Set this to return all columns.
//columnsToGet.setColumns(Arrays.asList("ColName1","ColName2")); // Set this to return specified columns.
//searchRequest.setColumnsToGet(columnsToGet);
SearchResponse resp = client.search(searchRequest);
//System.out.println("TotalCount: " + resp.getTotalCount()); // Print the total number of matched rows, not the number of returned rows.
System.out.println("Row: " + resp.getRows());
}
Example of a query where different child rows meet multiple conditions
The following example queries for rows where nested objects in the `col_nested` field meet two conditions: col_nested.col_keyword is "tablestore" and col_nested.col_long is not empty.
Based on the sample data, the data in row 1 and row 2 meet the query conditions.
public static void nestedQuery(SyncClient client) {
// Condition 1: The value of the col_keyword column in the child row of col_nested must be "tablestore".
TermQuery termQuery = new TermQuery();
termQuery.setFieldName("col_nested.col_keyword");
termQuery.setTerm(ColumnValue.fromString("tablestore"));
NestedQuery nestedTermQuery = new NestedQuery();
nestedTermQuery.setPath("col_nested");
nestedTermQuery.setScoreMode(ScoreMode.None);
nestedTermQuery.setQuery(termQuery);
// Condition 2: The col_long column in the child row of col_nested must not be empty.
ExistsQuery existsQuery = new ExistsQuery();
existsQuery.setFieldName("col_nested.col_long");
NestedQuery nestedExistsQuery = new NestedQuery();
nestedExistsQuery.setPath("col_nested");
nestedExistsQuery.setScoreMode(ScoreMode.None);
nestedExistsQuery.setQuery(existsQuery);
// Use the AND condition of BoolQuery to query for rows that meet the preceding conditions.
List<Query> mustQueries = new ArrayList<>();
mustQueries.add(nestedTermQuery);
mustQueries.add(nestedExistsQuery);
// The BoolQuery includes multiple NestedQuery conditions. The query is successful if different child rows meet the respective query conditions.
BoolQuery boolQuery = new BoolQuery();
boolQuery.setMustQueries(mustQueries);
SearchQuery searchQuery = new SearchQuery();
searchQuery.setQuery(boolQuery);
SearchRequest searchRequest = new SearchRequest("<TABLE_NAME>", "<SEARCH_INDEX_NAME>", searchQuery);
// Use the columnsToGet parameter to specify the columns to return or to return all columns. If you do not set this parameter, only the primary key columns are returned by default.
//SearchRequest.ColumnsToGet columnsToGet = new SearchRequest.ColumnsToGet();
//columnsToGet.setReturnAll(true); // Set this to return all columns.
//columnsToGet.setColumns(Arrays.asList("ColName1","ColName2")); // Set this to return specified columns.
//searchRequest.setColumnsToGet(columnsToGet);
SearchResponse resp = client.search(searchRequest);
//System.out.println("TotalCount: " + resp.getTotalCount()); // Print the total number of matched rows, not the number of returned rows.
System.out.println("Row: " + resp.getRows());
}
Example of using summary and highlighting in a nested query
The following example shows how to use `NestedQuery` to query for data where the value of the `Level1_Col1_Nested` sub-field in the `Col_Nested` nested field matches `hangzhou shanghai. The search query is highlighted in the returned results.
/**
* Use summary and highlighting in a NestedQuery. Set parameters using innerHits.
*/
public static void nestedQueryWithHighlighting(SyncClient client) {
SearchRequest searchRequest = SearchRequest.newBuilder()
.tableName("<TABLE_NAME>")
.indexName("<SEARCH_INDEX_NAME>")
.returnAllColumnsFromIndex(true)
.searchQuery(SearchQuery.newBuilder()
.limit(5)
.query(QueryBuilders.nested()
.path("Col_Nested")
.scoreMode(ScoreMode.Min)
.query(QueryBuilders.match("Col_Nested.Level1_Col1_Nested", "hangzhou shanghai"))
.innerHits(InnerHits.newBuilder()
.highlight(Highlight.newBuilder()
.addFieldHighlightParam("Col_Nested.Level1_Col1_Nested", HighlightParameter.newBuilder().build())
.build())
.build()))
.build())
.build();
SearchResponse resp = client.search(searchRequest);
// Print the highlighted results.
printSearchHit(resp.getSearchHits(), "");
}
/**
* Print the content of searchHit.
* @param searchHits The search hits.
* @param prefix The prefix to add when printing nested structures to show hierarchical information.
*/
private static void printSearchHit(List<SearchHit> searchHits, String prefix) {
for (SearchHit searchHit : searchHits) {
if (searchHit.getScore() != null) {
System.out.printf("%s Score: %s\n", prefix, searchHit.getScore());
}
if (searchHit.getOffset() != null) {
System.out.printf("%s Offset: %s\n", prefix, searchHit.getOffset());
}
if (searchHit.getRow() != null) {
System.out.printf("%s Row: %s\n", prefix, searchHit.getRow().toString());
}
// Print the highlighted fragments for each field.
if (searchHit.getHighlightResultItem() != null) {
System.out.printf("%s Highlight: \n", prefix);
StringBuilder strBuilder = new StringBuilder();
for (Map.Entry<String, HighlightField> entry : searchHit.getHighlightResultItem().getHighlightFields().entrySet()) {
strBuilder.append(entry.getKey()).append(":").append("[");
strBuilder.append(StringUtils.join(",", entry.getValue().getFragments())).append("]\n");
}
System.out.printf("%s %s", prefix, strBuilder);
}
// Highlighted results for the nested type.
for (SearchInnerHit searchInnerHit : searchHit.getSearchInnerHits().values()) {
System.out.printf("%s Path: %s\n", prefix, searchInnerHit.getPath());
System.out.printf("%s InnerHit: \n", prefix);
printSearchHit(searchInnerHit.getSubSearchHits(), prefix + " ");
}
System.out.println();
}
}
Assume that the multi-level nested field `Col_Nested` includes two sub-fields: `Level1_Col1_Text` (Text) and `Level1_Col2_Nested` (Nested). The `Level1_Col2_Nested` nested field includes the `Level2_Col1_Text` sub-field.
The following example shows how to add a `BoolQuery` to a `NestedQuery` to use the summary and highlighting features on both the `Level1_Col1_Text` sub-field in the `Col_Nested` field and the `Level2_Col1_Text` sub-field under `Level1_Col2_Nested`.
public static void nestedQueryWithHighlighting(SyncClient client) {
SearchRequest searchRequest = SearchRequest.newBuilder()
.tableName("<TABLE_NAME>")
.indexName("<SEARCH_INDEX_NAME>")
.returnAllColumnsFromIndex(true)
.searchQuery(SearchQuery.newBuilder()
.limit(5)
.query(QueryBuilders.nested()
.path("Col_Nested")
.scoreMode(ScoreMode.Min)
.query(QueryBuilders.bool()
.should(QueryBuilders.match("Col_Nested.Level1_Col1_Text", "hangzhou shanghai"))
.should(QueryBuilders.nested()
.path("Col_Nested.Level1_Col2_Nested")
.scoreMode(ScoreMode.Min)
.query(QueryBuilders.match("Col_Nested.Level1_Col2_Nested.Level2_Col1_Text", "hangzhou shanghai"))
.innerHits(InnerHits.newBuilder()
.highlight(Highlight.newBuilder()
.addFieldHighlightParam("Col_Nested.Level1_Col2_Nested.Level2_Col1_Text", HighlightParameter.newBuilder().build())
.build())
.build())))
.innerHits(InnerHits.newBuilder()
.sort(new Sort(Arrays.asList(
new ScoreSort(),
new DocSort()
)))
.highlight(Highlight.newBuilder()
.addFieldHighlightParam("Col_Nested.Level1_Col1_Text", HighlightParameter.newBuilder().build())
.build())
.build()))
.build())
.build();
SearchResponse resp = client.search(searchRequest);
// Print the highlighted results.
printSearchHit(resp.getSearchHits(), "");
}
/**
* Print the content of searchHit.
* @param searchHits The search hits.
* @param prefix The prefix to add when printing nested structures to show hierarchical information.
*/
private static void printSearchHit(List<SearchHit> searchHits, String prefix) {
for (SearchHit searchHit : searchHits) {
if (searchHit.getScore() != null) {
System.out.printf("%s Score: %s\n", prefix, searchHit.getScore());
}
if (searchHit.getOffset() != null) {
System.out.printf("%s Offset: %s\n", prefix, searchHit.getOffset());
}
if (searchHit.getRow() != null) {
System.out.printf("%s Row: %s\n", prefix, searchHit.getRow().toString());
}
// Print the highlighted fragments for each field.
if (searchHit.getHighlightResultItem() != null) {
System.out.printf("%s Highlight: \n", prefix);
StringBuilder strBuilder = new StringBuilder();
for (Map.Entry<String, HighlightField> entry : searchHit.getHighlightResultItem().getHighlightFields().entrySet()) {
strBuilder.append(entry.getKey()).append(":").append("[");
strBuilder.append(StringUtils.join(",", entry.getValue().getFragments())).append("]\n");
}
System.out.printf("%s %s", prefix, strBuilder);
}
// Highlighted results for the nested type.
for (SearchInnerHit searchInnerHit : searchHit.getSearchInnerHits().values()) {
System.out.printf("%s Path: %s\n", prefix, searchInnerHit.getPath());
System.out.printf("%s InnerHit: \n", prefix);
printSearchHit(searchInnerHit.getSubSearchHits(), prefix + " ");
}
System.out.println();
}
}