Tablestore SDK for Go を使用して検索インデックスを作成し、インデックスフィールド、事前ソート、TTL、仮想カラム、およびハイライトを設定します。
前提条件
作業を開始する前に、以下の準備を完了してください。
Tablestore Go SDK をインストールし、クライアントを初期化します。
データテーブルが作成されており、最大バージョン数が 1 に設定されています。
データテーブルの生存時間 (TTL) が -1 であるか、UpdateRow を使用した更新が無効になっています。
概要
CreateSearchIndex を呼び出して、データテーブルの多次元インデックスを作成します。1 つのデータテーブルに対して複数の多次元インデックスを作成できます。データテーブル名、インデックス名、完全なインデックススキーマを指定し、クエリ可能なカラムを FieldSchemas に追加します。インデックスフィールドのデータの型は、対応するテーブルカラムのデータの型と一致している必要があります。サポートされている型については、「データの型」をご参照ください。
func (client *tablestore.TableStoreClient) CreateSearchIndex(request *tablestore.CreateSearchIndexRequest) (*tablestore.CreateSearchIndexResponse, error)
多次元インデックスの作成は非同期で実行されます。リクエストが成功した後は、インデックスデータが同期されるまで待機してから、インデックスをクエリしてください。
以下の例では、Keyword フィールドおよび Long フィールドを含む多次元インデックスを作成します。事前ソートおよび TTL を構成しない場合、インデックスはプライマリキーでソートされ、データは有効期限切れになりません。
tableName := "example_table"
indexName := "example_index"
request := &tablestore.CreateSearchIndexRequest{
TableName: tableName,
IndexName: indexName,
IndexSchema: &tablestore.IndexSchema{
FieldSchemas: []*tablestore.FieldSchema{
{
FieldName: proto.String("category"),
FieldType: tablestore.FieldType_KEYWORD,
Index: proto.Bool(true),
},
{
FieldName: proto.String("price"),
FieldType: tablestore.FieldType_LONG,
Index: proto.Bool(true),
EnableSortAndAgg: proto.Bool(true),
},
},
},
}
_, err := client.CreateSearchIndex(request)
if err != nil {
log.Fatal(err)
}
パラメーター
CreateSearchIndexRequest には、以下のパラメーターが含まれます。
|
名前 |
型 |
説明 |
|
TableName (必須) |
string |
データテーブルの名前です。 |
|
IndexName (必須) |
string |
検索インデックスの名前。 |
|
IndexSchema (必須) |
*tablestore.IndexSchema |
インデックススキーマです。 |
|
SourceIndexName (オプション) |
*string |
スキーマを動的に変更するために使用するソースインデックスです。詳細については、「多次元インデックススキーマの動的変更」をご参照ください。 |
|
TimeToLive (オプション) |
*int32 |
インデックスデータの TTL (秒単位) です。デフォルト値は -1 です。有効値は -1 および 86,400 以上の int32 整数です。-1 はデータが有効期限切れにならないことを示します。-1 以外の値を設定する場合は、UpdateRow を使用した更新を無効にする必要があり、インデックスの TTL はテーブルの TTL を超えてはなりません。詳細については、「多次元インデックスの TTL の構成」をご参照ください。 |
インデックススキーマ
|
名前 |
型 |
説明 |
|
FieldSchemas (必須) |
[]*tablestore.FieldSchema |
インデックスフィールドのリストです。 |
|
IndexSetting (オプション) |
*tablestore.IndexSetting |
インデックス設定です。 |
|
IndexSort (オプション) |
*search.Sort |
インデックスの事前ソート構成です。このパラメーターを省略し、かつインデックスに Nested フィールドが含まれない場合、インデックスはプライマリキーでソートされます。Nested フィールドを含むインデックスでは、事前ソートはサポートされません。 |
インデックスフィールド
|
名前 |
型 |
説明 |
|
FieldName (必須) |
*string |
インデックスフィールド名です。プライマリキーや属性列に対応できます。 |
|
FieldType (必須) |
tablestore.FieldType |
インデックスフィールドのデータの型です。 |
|
Index (オプション) |
*bool |
転置インデックスまたは空間インデックスを作成するかどうかを指定します。Nested フィールドおよび JSON フィールド以外のフィールドでは、省略された場合 true として扱われます。 |
|
IndexOptions (オプション) |
*tablestore.IndexOptions |
Text フィールドのインデックスコンテンツの粒度です。通常、このパラメーターは省略されます。 |
|
Analyzer (オプション) |
*tablestore.Analyzer |
Text フィールドのアナライザです。省略された場合、文字トークン化が使用されます。 |
|
AnalyzerParameter (オプション) |
interface{} |
アナライザのパラメーターです。Analyzer の値に基づいてこのパラメーターを構成します。 |
|
EnableSortAndAgg (オプション) |
*bool |
ソートおよび集約を有効にするかどうかを指定します。Text フィールドおよび Nested フィールドはこの機能をサポートしませんが、Nested フィールドの子フィールドはサポートします。 |
|
EnableHighlighting (オプション) |
*bool |
まとめおよびハイライトを有効にするかどうかを指定します。この機能は Text フィールドのみがサポートします。デフォルト値は false です。 |
|
Store (オプション) |
*bool |
元のフィールド値を多次元インデックス内に格納するかどうかを指定します。有効にすると、ReturnAllFromIndex で値を返すことができます。 |
|
IsArray (オプション) |
*bool |
フィールドが配列かどうかを指定します。配列値は JSON 配列形式で記述する必要があります。Nested フィールドにはこのパラメーターを構成しないでください。 |
|
FieldSchemas (オプション) |
[]*tablestore.FieldSchema |
Nested フィールドまたは JSON フィールドの子フィールドです。Nested フィールドおよび JSON フィールドでは、このパラメーターが必須です。 |
|
IsVirtualField (オプション) |
*bool |
フィールドが仮想カラムかどうかを指定します。デフォルト値は false です。 |
|
SourceFieldNames (オプション) |
[]string |
仮想カラムにマッピングされるソースフィールドです。仮想カラムでは、このパラメーターが必須です。ソースフィールドは 1 つだけサポートされます。 |
|
DateFormats (オプション) |
[]string |
Date フィールドでサポートされる日付形式です。Date フィールドでは、このパラメーターが必須です。 |
|
VectorOptions (オプション) |
*tablestore.VectorOptions |
Vector フィールドのベクトル構成です。Vector フィールドでは、このパラメーターが必須です。 |
|
JsonType (オプション) |
*tablestore.JsonType |
JSON フィールドのインデックスタイプです。有効値は JsonType_OBJECT および JsonType_NESTED です。JSON フィールドでは、このパラメーターが必須です。 |
|
TextSimilarity (オプション) |
*tablestore.TextSimilarity |
Text フィールドの類似性アルゴリズムです。有効値は TextSimilarity_BM25 および TextSimilarity_SHORT_TEXT です。 |
ベクトル構成
|
名前 |
型 |
説明 |
|
VectorDataType (必須) |
*tablestore.VectorDataType |
ベクトルデータの型です。VectorDataType_FLOAT_32 のみがサポートされます。 |
|
VectorMetricType (必須) |
*tablestore.VectorMetricType |
距離メトリックです。ユークリッド距離、余弦類似度、内積がサポートされます。 |
|
Dimension (必須) |
*int32 |
ベクトルのディメンションです。最大値は 4096 です。 |
インデックス設定
|
名前 |
型 |
説明 |
|
RoutingFields (オプション) |
[]string |
カスタムルートフィールドです。1 つ以上のプライマリキー列を指定できます。同じルート値を持つ行は、同じインデックスパーティションに書き込まれます。 |
事前ソート構成
|
名前 |
型 |
説明 |
|
Sorters (必須) |
[]search.Sorter |
事前ソーターです。PrimaryKeySort および FieldSort がサポートされます。FieldSort で使用するフィールドは、ソートおよび集約を有効にする必要があります。詳細については、「結果のソートとページネーション」をご参照ください。 |
使用例
インデックスの事前ソートの構成
indexSort := &search.Sort{Sorters: []search.Sorter{
&search.FieldSort{
FieldName: "price",
Order: search.SortOrder_ASC.Enum(),
},
}}
request := &tablestore.CreateSearchIndexRequest{
TableName: "example_table",
IndexName: "example_index",
IndexSchema: &tablestore.IndexSchema{
FieldSchemas: []*tablestore.FieldSchema{
{
FieldName: proto.String("price"),
FieldType: tablestore.FieldType_LONG,
Index: proto.Bool(true),
EnableSortAndAgg: proto.Bool(true),
},
},
IndexSort: indexSort,
},
}
_, err := client.CreateSearchIndex(request)
if err != nil {
log.Fatal(err)
}
インデックスの TTL の構成
ttl := int32(7 * 24 * 60 * 60)
request := &tablestore.CreateSearchIndexRequest{
TableName: "example_table",
IndexName: "example_index",
TimeToLive: &ttl,
IndexSchema: &tablestore.IndexSchema{
FieldSchemas: []*tablestore.FieldSchema{
{
FieldName: proto.String("category"),
FieldType: tablestore.FieldType_KEYWORD,
Index: proto.Bool(true),
},
},
},
}
_, err := client.CreateSearchIndex(request)
if err != nil {
log.Fatal(err)
}
アナライザの構成
analyzer := tablestore.Analyzer_Split
delimiter := ","
fields := []*tablestore.FieldSchema{
{
FieldName: proto.String("tags"),
FieldType: tablestore.FieldType_TEXT,
Index: proto.Bool(true),
Analyzer: &analyzer,
AnalyzerParameter: tablestore.SplitAnalyzerParameter{
Delimiter: &delimiter,
},
},
}
ベクトルフィールドの作成
fields := []*tablestore.FieldSchema{
{
FieldName: proto.String("embedding"),
FieldType: tablestore.FieldType_VECTOR,
Index: proto.Bool(true),
VectorOptions: &tablestore.VectorOptions{
VectorDataType: tablestore.VectorDataType_FLOAT_32.Enum(),
VectorMetricType: tablestore.VectorMetricType_COSINE.Enum(),
Dimension: proto.Int32(4),
},
},
}
仮想カラムの作成
fields := []*tablestore.FieldSchema{
{
FieldName: proto.String("price"),
FieldType: tablestore.FieldType_LONG,
Index: proto.Bool(true),
},
{
FieldName: proto.String("price_text"),
FieldType: tablestore.FieldType_KEYWORD,
Index: proto.Bool(true),
IsVirtualField: proto.Bool(true),
SourceFieldNames: []string{"price"},
},
}
まとめおよびハイライトの有効化
analyzer := tablestore.Analyzer_SingleWord
fields := []*tablestore.FieldSchema{
{
FieldName: proto.String("description"),
FieldType: tablestore.FieldType_TEXT,
Index: proto.Bool(true),
Analyzer: &analyzer,
EnableHighlighting: proto.Bool(true),
},
}