本文主要为您介绍前缀查询(PrefixQuery)接口。PrefixQuery接口根据前缀条件查询表中的数据。对于分词字符串类型(TEXT),只要分词后的词条中有词条满足前缀条件即可。

/**
 * 查询表中Col_Keyword这一列前缀为"hangzhou"的数据。
 */
func PrefixQuery(client *tablestore.TableStoreClient, tableName string, indexName string) {
    searchRequest := &tablestore.SearchRequest{}
    searchRequest.SetTableName(tableName)
    searchRequest.SetIndexName(indexName)
    query := &search.PrefixQuery{} // 设置查询类型为PrefixQuery
    query.FieldName = "Col_Keyword" // 设置要匹配的字段
    query.Prefix = "hangzhou" // 设置前缀
    searchQuery := search.NewSearchQuery()
    searchQuery.SetQuery(query)
    searchQuery.SetGetTotalCount(true)
    searchRequest.SetSearchQuery(searchQuery)
    // 设置返回所有列
    searchRequest.SetColumnsToGet(&tablestore.ColumnsToGet{
        ReturnAll:true,
    })
    searchResponse, err := client.Search(searchRequest)
    if err != nil {
        fmt.Printf("%#v", err)
        return
    }
    fmt.Println("IsAllSuccess: ", searchResponse.IsAllSuccess) // 查看返回结果是否完整
    fmt.Println("TotalCount: ", searchResponse.TotalCount) // 匹配的总行数
    fmt.Println("RowCount: ", len(searchResponse.Rows))
    for _, row := range searchResponse.Rows {
        jsonBody, err := json.Marshal(row)
        if err != nil {
            panic(err)
        }
        fmt.Println("Row: ", string(jsonBody))
    }
}