Dados JSON incluem os tipos Object e Nested. Para consultar dados JSON do tipo Object, use diretamente o tipo de consulta desejado. Para consultar dados JSON do tipo Nested, envolva as condições de consulta em uma NestedQuery.
Pré-requisitos
Crie um índice de pesquisa na tabela de dados e configure o tipo de campo JSON.
Limites
O JSON não aceita campos do tipo vetor.
Cenários de consulta
Considere uma tabela de dados com uma coluna id do tipo String e uma coluna address, também do tipo String. A coluna address armazena dados no formato JSON.
Suponha que uma linha tenha a coluna address com o valor [{ "country": "China", "city": "hangzhou" }, { "country": "usa", "city": "Seattle" }]. Uma consulta por country="China" e city="Seattle" não retorna essa linha se a coluna address for do tipo Nested, mas a retorna caso seja do tipo Object.
Código de exemplo
Exemplo de consulta para o tipo JSON Nested
O exemplo a seguir consulta linhas em que o mesmo objeto aninhado do campo address atende a duas condições: address.country é "China" e address.city é "Seattle".
public static void nestedQuery(SyncClient client) {
// Condition 1: The value of the country field in the address sub-row must be "China".
TermQuery termQuery1 = new TermQuery();
termQuery1.setFieldName("address.country");
termQuery1.setTerm(ColumnValue.fromString("China"));
// Condition 2: The value of the city field in the address sub-row must be "Seattle".
TermQuery termQuery2 = new TermQuery();
termQuery2.setFieldName("address.city");
termQuery2.setTerm(ColumnValue.fromString("Seattle"));
// Use the AND condition of BoolQuery to query for sub-rows that meet both conditions.
List<Query> mustQueries = new ArrayList<>();
mustQueries.add(termQuery1);
mustQueries.add(termQuery2);
BoolQuery boolQuery = new BoolQuery();
boolQuery.setMustQueries(mustQueries);
// Set BoolQuery within NestedQuery to require a sub-row to meet multiple query conditions at the same time.
NestedQuery nestedQuery = new NestedQuery(); // Set the query type to NestedQuery.
nestedQuery.setPath("address"); // Set the path of the nested type 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);
SearchResponse resp = client.search(searchRequest);
System.out.println("Row: " + resp.getRows());
}
Exemplo de consulta para o tipo JSON Object
Este exemplo consulta linhas nas quais o campo address satisfaz duas condições entre seus objetos aninhados: address.country é "China" e address.city é "Seattle".
public static void boolQuery(SyncClient client) {
// Condition 1: The value of the country field in the address sub-row must be "China".
TermQuery termQuery1 = new TermQuery();
termQuery1.setFieldName("address.country");
termQuery1.setTerm(ColumnValue.fromString("China"));
// Condition 2: The value of the city field in the address sub-row must be "Seattle".
TermQuery termQuery2 = new TermQuery();
termQuery2.setFieldName("address.city");
termQuery2.setTerm(ColumnValue.fromString("Seattle"));
// Use the AND condition of BoolQuery to query for sub-rows that meet both conditions.
List<Query> mustQueries = new ArrayList<>();
mustQueries.add(termQuery1);
mustQueries.add(termQuery2);
BoolQuery boolQuery = new BoolQuery();
boolQuery.setMustQueries(mustQueries);
SearchQuery searchQuery = new SearchQuery();
searchQuery.setQuery(boolQuery);
SearchRequest searchRequest = new SearchRequest("<TABLE_NAME>", "<SEARCH_INDEX_NAME>", searchQuery);
SearchResponse resp = client.search(searchRequest);
System.out.println("Row: " + resp.getRows());
}