すべてのプロダクト
Search
ドキュメントセンター

Tablestore:プレフィックスクエリ

最終更新日:May 01, 2026

プレフィックスクエリを実行すると、指定されたプレフィックスに一致するデータをクエリできます。プレフィックス条件のマッチに使用されるフィールドが TEXT フィールドの場合、フィールド値はトークン化されます。少なくとも 1 つのトークンが指定されたプレフィックスを含む場合、その行はクエリ条件を満たします。

前提条件

  • OTSClient インスタンスが初期化されています。詳細については、「Initialize an OTSClient instance」をご参照ください。

  • データテーブルが作成され、データが書き込まれています。詳細については、「Create a data table」および「Write data」をご参照ください。

  • データテーブルに対して多次元インデックスが作成されています。詳細については、「Create search indexes」をご参照ください。

パラメーター

パラメーター

説明

TableName

データテーブルの名前です。

IndexName

検索インデックスの名前。

Query

クエリタイプです。このパラメーターを PrefixQuery に設定します。

FieldName

マッチ対象とするフィールドの名前です。

Prefix

マッチさせるプレフィックスです。

対象フィールドが TEXT フィールドの場合、マッチ前にフィールド値がトークン化されます。少なくとも 1 つのトークンが指定されたプレフィックスで始まる場合、その行はクエリ条件を満たします。

GetTotalCount

クエリ条件に一致する行の総数を返すかどうかを指定します。デフォルト値は false です。

true を設定すると、クエリパフォーマンスが低下します。

ColumnsToGet

返される列を制御します。ReturnAll および Columns のサブパラメーターを使用して設定します。

デフォルトでは ReturnAll は false であり、プライマリキー列のみが返されます。特定の非プライマリキー列を返すには、ReturnAll を false に設定し、Columns に列をリストアップします。

すべての列を返すには、ReturnAll を true に設定します。

サンプルコード

次の例では、Col_Keyword 列の値が hangzhou で始まる行をクエリします。

/**
 * Query the rows in which the value of the Col_Keyword column contains the "hangzhou" prefix in a table. 
 */
func PrefixQuery(client *tablestore.TableStoreClient, tableName string, indexName string) {
    searchRequest := &tablestore.SearchRequest{}
    searchRequest.SetTableName(tableName)
    searchRequest.SetIndexName(indexName)
    query := &search.PrefixQuery{} // Set the query type to PrefixQuery. 
    query.FieldName = "Col_Keyword" // Specify the name of the field that you want to match. 
    query.Prefix = "hangzhou" // Specify the prefix that is used to match the value of the column. 
    searchQuery := search.NewSearchQuery()
    searchQuery.SetQuery(query)
    searchQuery.SetGetTotalCount(true)
    searchRequest.SetSearchQuery(searchQuery)
    // Return all columns in the rows that meet the query conditions. 
    searchRequest.SetColumnsToGet(&tablestore.ColumnsToGet{
        ReturnAll:true,
    })
    searchResponse, err := client.Search(searchRequest)
    if err != nil {
        fmt.Printf("%#v", err)
        return
    }
    fmt.Println("IsAllSuccess: ", searchResponse.IsAllSuccess) // Check whether all rows that meet the query conditions are returned. 
    fmt.Println("TotalCount: ", searchResponse.TotalCount) // Display the total number of rows that meet the query conditions instead of the number of returned rows. 
    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))
    }
}

よくある質問

参照