Dados JSON incluem os tipos Object e Nested. Para consultar dados do tipo Object, use diretamente o tipo de consulta desejado. Para consultar dados do tipo Nested, envolva as condições da consulta em uma NestedQuery.
Pré-requisitos
Crie um índice de busca na tabela de dados e configure o tipo de campo JSON.
Limites
Não é possível usar campos do tipo vetor em JSON.
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 se for do tipo Object.
Exemplos de código
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".
import (
"fmt"
"github.com/aliyun/aliyun-tablestore-go-sdk/tablestore"
"github.com/aliyun/aliyun-tablestore-go-sdk/tablestore/search"
)
func nestedQuery1(client *tablestore.TableStoreClient) {
searchRequest := &tablestore.SearchRequest{}
searchRequest.SetTableName("<TABLE_NAME>")
searchRequest.SetIndexName("<SEARCH_INDEX_NAME>")
// Condition 1: address.country = "China"
termQuery1 := &search.TermQuery{
FieldName: "address.country",
Term: "China",
}
// Condition 2: address.city = "Seattle"
termQuery2 := &search.TermQuery{
FieldName: "address.city",
Term: "Seattle",
}
// Combine the two conditions. Both must be met.
boolQuery := &search.BoolQuery{
MustQueries: []search.Query{
termQuery1,
termQuery2,
},
}
// Nested query
nestedQuery := &search.NestedQuery{
Path: "address",
Query: boolQuery,
ScoreMode: search.ScoreMode_None,
}
// Build and execute the search.
searchQuery := search.NewSearchQuery()
searchQuery.SetQuery(nestedQuery)
searchRequest.SetSearchQuery(searchQuery)
resp, err := client.Search(searchRequest)
if err != nil {
fmt.Printf("Search failed: %v\n", err)
return
}
fmt.Printf("Found %d rows\n", len(resp.Rows))
for _, row := range resp.Rows {
fmt.Printf("Row: %+v\n", row.PrimaryKey)
}
}
Exemplo de consulta para o tipo JSON Object
Este exemplo consulta linhas em que o campo address atende a duas condições entre seus objetos aninhados: address.country é "China" e address.city é "Seattle".
import (
"fmt"
"github.com/aliyun/aliyun-tablestore-go-sdk/tablestore"
"github.com/aliyun/aliyun-tablestore-go-sdk/tablestore/search"
)
func boolQuery(client *tablestore.TableStoreClient) {
searchRequest := &tablestore.SearchRequest{}
searchRequest.SetTableName("<TABLE_NAME>")
searchRequest.SetIndexName("<SEARCH_INDEX_NAME>")
// Condition 1: address.country = "China"
termQuery1 := &search.TermQuery{
FieldName: "address.country",
Term: "China",
}
// Condition 2: address.city = "Seattle"
termQuery2 := &search.TermQuery{
FieldName: "address.city",
Term: "Seattle",
}
// Use the Must condition of BoolQuery to query for sub-rows that meet both conditions.
// Combine the two conditions. Both must be met.
boolQuery := &search.BoolQuery{
MustQueries: []search.Query{
termQuery1,
termQuery2,
},
}
// Build and execute the search.
searchQuery := search.NewSearchQuery()
searchQuery.SetQuery(boolQuery)
searchRequest.SetSearchQuery(searchQuery)
// Execute the search.
resp, err := client.Search(searchRequest)
if err != nil {
fmt.Printf("Search failed: %v\n", err)
return
}
fmt.Printf("Found %d rows\n", len(resp.Rows))
for _, row := range resp.Rows {
fmt.Printf("Row: %+v\n", row.PrimaryKey)
}
}