本文主要为您介绍匹配查询的相关的接口。

MatchAllQuery

MatchAllQuery用于匹配所有行,常用于查询表中数据总行数,或者查看表中任意几条数据。

/**
 * 通过MatchAllQuery查询表中数据的总行数
 */
func MatchAllQuery(client *tablestore.TableStoreClient, tableName string, indexName string) {
    searchRequest := &tablestore.SearchRequest{}
    searchRequest.SetTableName(tableName)
    searchRequest.SetIndexName(indexName)
    query := &search.MatchAllQuery{} // 设置查询类型为MatchAllQuery
    searchQuery := search.NewSearchQuery()
    searchQuery.SetQuery(query) 
    searchQuery.SetGetTotalCount(true) 
    searchQuery.SetLimit(0) // 设置Limit为0,不获取具体数据
    searchRequest.SetSearchQuery(searchQuery)
    searchResponse, err := client.Search(searchRequest)
    if err != nil { // 判断异常
        fmt.Printf("%#v", err) 
        return
    }
    fmt.Println("IsAllSuccess: ", searchResponse.IsAllSuccess)
    fmt.Println("TotalCount: ", searchResponse.TotalCount) // 总行数
}

MatchQuery

MatchQuery采用近似匹配的方式查询表中的数据。比如查询的值为"this is", 可以匹配到“...,this is tablestore”、“is this tablestore”、“tablestore is cool”、“this"、“is”等。



/**
 *  查询表中Col_Keyword这一列的值能够匹配"hangzhou"的数据,返回匹配到的总行数和一些匹配成功的行。
 */
func MatchQuery(client *tablestore.TableStoreClient, tableName string, indexName string) {
    searchRequest := &tablestore.SearchRequest{}
    searchRequest.SetTableName(tableName)
    searchRequest.SetIndexName(indexName)
    query := &search.MatchQuery{} // 设置查询类型为MatchQuery
    query.FieldName = "Col_Keyword" // 设置要匹配的字段
    query.Text = "hangzhou" // 设置要匹配的值
    searchQuery := search.NewSearchQuery()
    searchQuery.SetQuery(query)
    searchQuery.SetGetTotalCount(true) 
    searchQuery.SetOffset(0) // 设置offset为0
    searchQuery.SetLimit(20) // 设置limit为20,表示最多返回20条数据
    searchRequest.SetSearchQuery(searchQuery)
    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)) // 不设置columnsToGet,默认只返回主键
    }
    // 设置返回所有列
    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))
    }
}

MatchPhraseQuery

使用MatchPhraseQuery进行短语匹配查询。短语匹配查询与匹配查询类似,但是要求查询的短语必须完整的按照顺序匹配。比如查询的值为“this is”,可以匹配到“...,this is tablestore”、"this is a table”,但是无法匹配到"this table is ..."以及"is this a table"。

/**
 * 查询表中Col_Text这一列的值能够匹配"hangzhou shanghai"的数据,匹配条件为短语匹配(要求短语完整的按照顺序匹配),返回匹配到的总行数和一些匹配成功的行。
 */
func MatchPhraseQuery(client *tablestore.TableStoreClient, tableName string, indexName string) {
    searchRequest := &tablestore.SearchRequest{}
    searchRequest.SetTableName(tableName)
    searchRequest.SetIndexName(indexName)
    query := &search.MatchPhraseQuery{} // 设置查询类型为MatchPhraseQuery
    query.FieldName = "Col_Text"  // 设置要匹配的字段
    query.Text = "hangzhou shanghai"  // 设置要匹配的值
    searchQuery := search.NewSearchQuery()
    searchQuery.SetQuery(query)
    searchQuery.SetGetTotalCount(true) 
    searchQuery.SetOffset(0) // 设置offset为0
    searchQuery.SetLimit(20) // 设置limit为20,表示最多返回20条数据
    searchRequest.SetSearchQuery(searchQuery)
    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))
    }
    // 设置返回所有列
    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))
    }
}