検索インデックスは、Lastpoint インデックスからのデータ取得を高速化し、多次元クエリおよび統計分析機能を提供します。このトピックでは、Go SDK で検索インデックスを使用して Lastpoint インデックスデータを取得する方法について説明します。
注意事項
Tablestore Go SDK は、v1.7.15 以降で Lastpoint インデックス機能をサポートしています。この機能を使用するには、SDK を v1.7.15 以降にアップグレードする必要があります。
前提条件
時系列テーブルに Lastpoint インデックスを作成済みであること。詳細については、「Lastpoint インデックスを作成する」をご参照ください。
手順
1. 検索インデックスを作成する
次の例では、Lastpoint インデックスの検索インデックスを作成します。サンプルのシナリオとデータについては、「付録」をご参照ください。
この例では、_tags 列は、タグで構成される文字列配列です。_tags 内のタグを簡単にクエリできるように、対応する検索インデックスのフィールドタイプをキーワード配列に設定することをお勧めします。
func createSearchIndex(client *tablestore.TableStoreClient) {
request := &tablestore.CreateSearchIndexRequest{}
request.TableName = "<LASTPOINT_INDEX_NAME>" // Lastpoint インデックス名
request.IndexName = "<SEARCH_INDEX_NAME>" // 検索インデックス名
request.IndexSchema = &tablestore.IndexSchema{
FieldSchemas: []*tablestore.FieldSchema{
{
FieldName: proto.String("_#h"), // プライマリキーのハッシュ値
FieldType: tablestore.FieldType_KEYWORD,
Index: proto.Bool(true),
EnableSortAndAgg: proto.Bool(true),
},
{
FieldName: proto.String("_m_name"), // メジャー名
FieldType: tablestore.FieldType_KEYWORD,
Index: proto.Bool(true),
EnableSortAndAgg: proto.Bool(true),
},
{
FieldName: proto.String("_data_source"), // データソース
FieldType: tablestore.FieldType_KEYWORD,
Index: proto.Bool(true),
EnableSortAndAgg: proto.Bool(true),
},
{
FieldName: proto.String("_tags"), // タグ
FieldType: tablestore.FieldType_KEYWORD,
Index: proto.Bool(true),
EnableSortAndAgg: proto.Bool(true),
IsArray: proto.Bool(true), // タグは配列として定義されている
},
{
FieldName: proto.String("_time"), // データの報告時間
FieldType: tablestore.FieldType_LONG,
Index: proto.Bool(true),
EnableSortAndAgg: proto.Bool(true),
},
{
FieldName: proto.String("gps"), // GPS 座標
FieldType: tablestore.FieldType_GEO_POINT,
Index: proto.Bool(true),
EnableSortAndAgg: proto.Bool(true),
},
{
FieldName: proto.String("speed"), // 速度
FieldType: tablestore.FieldType_DOUBLE,
Index: proto.Bool(true),
EnableSortAndAgg: proto.Bool(true),
},
{
FieldName: proto.String("status"), // 状態
FieldType: tablestore.FieldType_KEYWORD,
Index: proto.Bool(true),
EnableSortAndAgg: proto.Bool(true),
},
{
FieldName: proto.String("total_mileage"), // 総走行距離
FieldType: tablestore.FieldType_LONG,
Index: proto.Bool(true),
EnableSortAndAgg: proto.Bool(true),
},
{
FieldName: proto.String("remaining_mileage"), // 残り走行距離
FieldType: tablestore.FieldType_LONG,
Index: proto.Bool(true),
EnableSortAndAgg: proto.Bool(true),
},
},
}
_, err := client.CreateSearchIndex(request)
if err != nil {
fmt.Println("Failed to create searchIndex with error:", err) // 検索インデックスの作成に失敗しました
return
}
}2. 検索インデックスを使用してデータを取得する
次の例は、検索インデックスの範囲クエリ機能の使用方法を示しています。
この例では、速度の値が 20.0 より大きい Lastpoint インデックスから行を取得します。
func RangeQuery(client *tablestore.TableStoreClient, lastpointName string, indexName string) {
searchRequest := &tablestore.SearchRequest{}
searchRequest.SetTableName(lastpointName) // Lastpoint インデックス名を設定
searchRequest.SetIndexName(indexName) // 検索インデックス名を設定
searchQuery := search.NewSearchQuery()
rangeQuery := &search.RangeQuery{} // クエリタイプを RangeQuery に設定します。
rangeQuery.FieldName = "speed" // 一致するフィールドを設定します
rangeQuery.GT(20.0) // このフィールドの範囲条件を 20.0 より大きく設定します。
searchQuery.SetQuery(rangeQuery)
// 速度列で降順にソートします。
searchQuery.SetSort(&search.Sort{
[]search.Sorter{
&search.FieldSort{
FieldName: "speed",
Order: search.SortOrder_DESC.Enum(),
},
},
})
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("RowCount: ", len(searchResponse.Rows)) // 取得した行数
for _, row := range searchResponse.Rows {
jsonBody, err := json.Marshal(row)
if err != nil {
panic(err)
}
fmt.Println("Row: ", string(jsonBody)) // 各行の JSON 形式のデータ
}
}FAQ
関連情報
検索インデックスのコア機能には、任意の列(プライマリキー列と属性列を含む)のクエリ、ブールクエリ、ジオクエリ、フルテキストインデックス、あいまい検索、プレフィックスクエリ、ネストされたクエリ、重複の削除、ソート、行の総数のクエリ、および統計的集約が含まれます。詳細については、「検索インデックスの機能」をご参照ください。
付録
コネクテッドカーのシナリオでは、センサーが車両の時系列データをクラウドに報告します。ユーザーは、時系列データを保存、クエリ、および分析して、車両ステータスレポート、車両測位、交通管理、車両軌跡の画面ミラーリングなどのビジネス要件を満たすことができます。
時系列テーブルのサンプルデータは次のとおりです。
このテーブルでは、_m_name、_data_source、および _tags は時系列識別子であり、時系列のメジャー名、データソース、およびタグ情報を表します。_time はデータの報告時間です。gps、speed、status、total_mileage、および remaining_mileage は時系列の時系列データであり、車両の GPS 座標、速度、状態、総走行距離、および残り走行距離を表します。
_m_name | _data_source | _tags | _time | gps | speed | status | total_mileage | remaining_mileage |
Platform A | sensor1 | ["region=hangzhou","car_model=sedan","number_plate=ZheA D7512*","color=white"] | 1730422800000000 | 30.245853,120.178564 | 0 | Idle | 20000 | 450 |
Platform A | sensor1 | ["region=hangzhou","car_model=sedan","number_plate=ZheA D7512*","color=white"] | 1730423400000000 | 30.245853,120.178564 | 0 | Idle | 20000 | 450 |
Platform A | sensor2 | ["region=hangzhou","car_model=suv","number_plate=ZheC 72B2*","color=black"] | 1730779200000000 | 30.245278,120.150269 | 50 | Active | 15000 | 300 |
Platform A | sensor2 | ["region=hangzhou","car_model=suv","number_plate=ZheC 72B2*","color=black"] | 1730779800000000 | 30.245853,120.213654 | 80 | Active | 15050 | 250 |
Platform B | sensor3 | ["region=hangzhou","car_model=sedan","number_plate=ZheB 121*9","color=blue"] | 1730862000000000 | 30.246013,120.124470 | 60 | Active | 18200 | 300 |
Platform B | sensor3 | ["region=hangzhou","car_model=sedan","number_plate=ZheB 121*9","color=blue"] | 1730862600000000 | 30.246022,120.124460 | 0 | Idle | 18230 | 270 |
Tablestore は、時系列テーブルから Lastpoint インデックステーブルに時系列の最新時点データを自動的に同期します。Lastpoint インデックスのサンプルデータは次のとおりです。
_#h | _m_name | _data_source | _tags | _time | gps | speed | status | total_mileage | remaining_mileage |
4c#Platform A#07 | Platform A | sensor1 | ["region=hangzhou","car_model=sedan","number_plate=ZheA D7512*","color=white"] | 1730423400000000 | 30.245853,120.178564 | 0 | Idle | 20000 | 450 |
25#Platform A#ae | Platform A | sensor2 | ["region=hangzhou","car_model=suv","number_plate=ZheC 72B2*","color=black"] | 1730779800000000 | 30.245853,120.213654 | 80 | Active | 15050 | 250 |
b2#Platform B#4b | Platform B | sensor3 | ["region=hangzhou","car_model=sedan","number_plate=ZheB 121*9","color=blue"] | 1730862600000000 | 30.246022,120.124460 | 0 | Idle | 18230 | 270 |