全匹配查詢(xún)
MatchAllQuery可以匹配所有行,常用于查詢(xún)表中數(shù)據(jù)總行數(shù),或者隨機(jī)返回幾條數(shù)據(jù)。
前提條件
已初始化Client。具體操作,請(qǐng)參見(jiàn)初始化OTSClient。
已創(chuàng)建數(shù)據(jù)表并寫(xiě)入數(shù)據(jù)。具體操作,請(qǐng)參見(jiàn)創(chuàng)建數(shù)據(jù)表和寫(xiě)入數(shù)據(jù)。
已在數(shù)據(jù)表上創(chuàng)建多元索引。具體操作,請(qǐng)參見(jiàn)創(chuàng)建多元索引。
參數(shù)
參數(shù) | 說(shuō)明 |
TableName | 數(shù)據(jù)表名稱(chēng)。 |
IndexName | 多元索引名稱(chēng)。 |
Query | 設(shè)置查詢(xún)類(lèi)型為MatchAllQuery。 |
Limit | 本次查詢(xún)需要返回的最大數(shù)量。 如果只為了獲取行數(shù),無(wú)需獲取具體數(shù)據(jù),可以設(shè)置Limit=0,即不返回任意一行數(shù)據(jù)。 |
GetTotalCount | 是否返回匹配的總行數(shù),默認(rèn)為false,表示不返回。 返回匹配的總行數(shù)會(huì)影響查詢(xún)性能。 |
ColumnsToGet | 是否返回所有列。 ReturnAll默認(rèn)為false,表示不返回所有列,此時(shí)可以通過(guò)Columns指定返回的列;如果未通過(guò)Columns指定返回的列,則只返回主鍵列。 當(dāng)設(shè)置ReturnAll為true時(shí),表示返回所有列。 |
示例
以下示例用于查詢(xún)表中數(shù)據(jù)的總行數(shù)。
/**
* 通過(guò)MatchAllQuery查詢(xún)表中數(shù)據(jù)的總行數(shù)。
*/
func MatchAllQuery(client *tablestore.TableStoreClient, tableName string, indexName string) {
searchRequest := &tablestore.SearchRequest{}
searchRequest.SetTableName(tableName)
searchRequest.SetIndexName(indexName)
query := &search.MatchAllQuery{} //設(shè)置查詢(xún)類(lèi)型為MatchAllQuery。
searchQuery := search.NewSearchQuery()
searchQuery.SetQuery(query)
searchQuery.SetGetTotalCount(true)
searchQuery.SetLimit(0) //設(shè)置Limit為0,表示不獲取具體數(shù)據(jù)。
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) //打印總行數(shù)。
}
常見(jiàn)問(wèn)題
相關(guān)文檔
相關(guān)文檔
多元索引查詢(xún)類(lèi)型包括精確查詢(xún)、多詞精確查詢(xún)、全匹配查詢(xún)、匹配查詢(xún)、短語(yǔ)匹配查詢(xún)、前綴查詢(xún)、范圍查詢(xún)、通配符查詢(xún)、多條件組合查詢(xún)、地理位置查詢(xún)、嵌套類(lèi)型查詢(xún)、向量檢索和列存在性查詢(xún),您可以選擇合適的查詢(xún)類(lèi)型進(jìn)行多維度數(shù)據(jù)查詢(xún)。
如果要對(duì)結(jié)果集進(jìn)行排序或者翻頁(yè),您可以使用排序和翻頁(yè)功能來(lái)實(shí)現(xiàn)。具體操作,請(qǐng)參見(jiàn)排序和翻頁(yè)。
如果要按照某一列對(duì)結(jié)果集做折疊,使對(duì)應(yīng)類(lèi)型的數(shù)據(jù)在結(jié)果展示中只出現(xiàn)一次,您可以使用折疊(去重)功能來(lái)實(shí)現(xiàn)。具體操作,請(qǐng)參見(jiàn)折疊(去重)。
如果要進(jìn)行數(shù)據(jù)分析,例如求最值、求和、統(tǒng)計(jì)行數(shù)等,您可以使用Search接口的統(tǒng)計(jì)聚合功能或者SQL查詢(xún)來(lái)實(shí)現(xiàn)。具體操作,請(qǐng)參見(jiàn)統(tǒng)計(jì)聚合和SQL查詢(xún)。
如果要快速導(dǎo)出數(shù)據(jù),而不關(guān)心整個(gè)結(jié)果集的順序時(shí),您可以使用ParallelScan接口和ComputeSplits接口實(shí)現(xiàn)多并發(fā)導(dǎo)出數(shù)據(jù)。具體操作,請(qǐng)參見(jiàn)并發(fā)導(dǎo)出數(shù)據(jù)。