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。 |
|
IsArray (任意) |
*bool |
フィールドが配列かどうかを指定します。配列値は JSON 配列形式で書き込む必要があります。Nested フィールドにはこのパラメータを設定しないでください。 |
|
FieldSchemas (任意) |
[]*tablestore.FieldSchema |
Nested フィールドまたは JSON フィールドの子フィールド。Nested フィールドと JSON フィールドでは、このパラメータが必須です。 |
|
IsVirtualField (任意) |
*bool |
フィールドが仮想カラムかどうかを指定します。デフォルト値:false。 |
|
SourceFieldNames (任意) |
[]string |
仮想カラムにマッピングされるソースフィールド。仮想カラムではこのパラメータが必須です。サポートされるソースフィールドは 1 つのみです。 |
|
DateFormats (任意) |
[]string |
Date フィールドでサポートされる日付形式。Date フィールドではこのパラメータが必須です。 |
|
VectorOptions (任意) |
*tablestore.VectorOptions |
ベクトルフィールドのベクトル設定。ベクトルフィールドではこのパラメータが必須です。 |
|
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),
},
}