全部產品
Search
文件中心

Tablestore:JSON 查詢

更新時間:Aug 12, 2026

使用 Tablestore Go SDK 查詢多元索引中的 JSON Object 或 JSON Nested 欄位。

前提條件

開始前,完成以下準備工作:

功能說明

JSON 欄位支援 Object 和 Nested 兩種索引方式。Object 不保留數組中各子物件的邊界,多個查詢條件可以由不同子物件分別滿足;Nested 將數組中的每個對象作為獨立子行查詢,能夠保證多個條件在同一數組元素內匹配。

JSON 索引類型

查詢方式

JsonType_OBJECT

不保留數組子物件邊界。使用子欄位完整路徑執行普通查詢,例如 profile.city

JsonType_NESTED

保留同一子物件內各欄位的對應關係。使用 NestedQuery 查詢數組中的對象。

說明

JSON 欄位的 SubFieldSchemas 不支援 Vector 類型子欄位。

查詢 Object 欄位

tableName := "example_table"
indexName := "example_index"

query := &search.TermQuery{FieldName: "profile.city", Term: "hangzhou"}
searchQuery := search.NewSearchQuery().
    SetQuery(query).
    SetLimit(10).
    SetGetTotalCount(true)

response, err := client.Search(&tablestore.SearchRequest{
    TableName:   tableName,
    IndexName:   indexName,
    SearchQuery: searchQuery,
    ColumnsToGet: &tablestore.ColumnsToGet{
        ReturnAllFromIndex: true,
    },
})
if err != nil {
    log.Fatal(err)
}

fmt.Println(response.TotalCount)
fmt.Println(response.Rows)

查詢 Nested 欄位

tableName := "example_table"
indexName := "example_index"

query := &search.NestedQuery{Path: "orders", Query: &search.TermQuery{FieldName: "orders.product", Term: "book"}, ScoreMode: search.ScoreMode_None}
searchQuery := search.NewSearchQuery().
    SetQuery(query).
    SetLimit(10).
    SetGetTotalCount(true)

response, err := client.Search(&tablestore.SearchRequest{
    TableName:   tableName,
    IndexName:   indexName,
    SearchQuery: searchQuery,
    ColumnsToGet: &tablestore.ColumnsToGet{
        ReturnAllFromIndex: true,
    },
})
if err != nil {
    log.Fatal(err)
}

fmt.Println(response.TotalCount)
fmt.Println(response.Rows)

參數說明

Object 欄位使用具體查詢類型的參數;Nested 欄位使用 search.NestedQuery。通用查詢配置請參見精確查詢,NestedQuery 參數請參見巢狀型別查詢

Object 欄位查詢

名稱

類型

說明

FieldName(必選)

string

JSON 子欄位的完整路徑,例如 profile.city

查詢值(必選)

interface{}

具體名稱和類型取決於使用的查詢類型。

Nested 欄位查詢

名稱

類型

說明

Path(必選)

string

JsonType_NESTED 欄位路徑。

Query(必選)

search.Query

對子欄位執行的查詢條件。

ScoreMode(可選)

search.ScoreMode

子查詢評分合并方式。