`CreateSearchIndex` メソッドを使用して、データテーブルに検索インデックスを作成します。1 つのデータテーブルに複数の検索インデックスを作成できます。検索インデックスを作成する際には、クエリ対象のフィールドを追加する必要があります。また、ルートフィールドや事前ソートなどの高度なオプションも設定できます。
前提条件
Tablestore クライアントが初期化されていること。詳細については、「Tablestore クライアントの初期化」をご参照ください。
次の条件を満たすデータテーブルが作成されていること。詳細については、「データテーブルの作成」をご参照ください。
バージョンの最大数が 1 であること。
生存時間 (TTL) が -1 であるか、データテーブルの更新が無効になっていること。
注意事項
多次元インデックスを作成する場合、多次元インデックス内のフィールドのデータの型は、データテーブル内の対応するフィールドのデータの型と一致している必要があります。
検索インデックスに -1 以外の特定の TTL を設定する場合は、データテーブルの更新を無効にする必要があります。検索インデックスの TTL は、データテーブルの TTL 以下である必要があります。詳細については、「ライフサイクル管理」をご参照ください。
パラメーター
検索インデックスを作成する際には、テーブル名 (TableName)、検索インデックス名 (IndexName)、およびインデックス構造 (IndexSchema) を指定する必要があります。IndexSchema パラメーターには、FieldSchemas (すべてのインデックスフィールドの設定)、IndexSetting (インデックス設定)、および IndexSort (インデックスの事前ソート設定) が含まれます。次の表に、これらのパラメーターの詳細を示します。
パラメーター | 説明 |
TableName | データテーブルの名前。 |
IndexName | 検索インデックスの名前。 |
FieldSchemas | FieldSchema オブジェクトのリスト。各 FieldSchema には、次のパラメーターが含まれます:
|
IndexSetting | インデックス設定。これには RoutingFields 設定が含まれます。 RoutingFields (任意):カスタムルートフィールド。一部のプライマリキー列をルートフィールドとして選択できます。通常、設定する必要があるのは 1 つだけです。複数のルートキーを設定した場合、システムはそれらの値を連結して単一の値にします。 |
IndexSort | インデックスの事前ソート設定。これには Sorters 設定が含まれます。これを設定しない場合、データはデフォルトでプライマリキーによってソートされます。 説明 IndexSort は、Nested 型のフィールドを含むインデックスではサポートされていません。これらのインデックスには事前ソートがありません。 Sorters (必須):インデックスの事前ソート方法。プライマリキーまたはフィールド値でソートできます。ソートの詳細については、「ソートとページネーション」をご参照ください。
|
TimeToLive | 検索インデックスのライフサイクルについての詳細は、「ライフサイクル管理」をご参照ください。 |
例
デフォルト設定での検索インデックスの作成
次の例は、検索インデックスを作成する方法を示しています。検索インデックスには、col_keyword (Keyword 型)、col_long (Long 型)、および col_vector (Vector 型) の 3 つの列が含まれています。
func createSearchIndex(client *tablestore.TableStoreClient) {
request := &tablestore.CreateSearchIndexRequest{}
request.TableName = "<TABLE_NAME>"
request.IndexName = "<SEARCH_INDEX_NAME>"
request.IndexSchema = &tablestore.IndexSchema{
FieldSchemas: []*tablestore.FieldSchema{
{
FieldName: proto.String("col_keyword"),
FieldType: tablestore.FieldType_KEYWORD, // 文字列型
Index: proto.Bool(true),
EnableSortAndAgg: proto.Bool(true),
},
{
FieldName: proto.String("col_long"),
FieldType: tablestore.FieldType_LONG, // 数値型
Index: proto.Bool(true),
EnableSortAndAgg: proto.Bool(true),
},
{
FieldName: proto.String("col_vector"),
FieldType: tablestore.FieldType_VECTOR, // ベクター型
Index: proto.Bool(true),
VectorOptions: &tablestore.VectorOptions{
VectorDataType: tablestore.VectorDataType_FLOAT_32.Enum(),
Dimension: proto.Int32(4), // ベクターのディメンションは 4、類似度アルゴリズムはドット積
VectorMetricType: tablestore.VectorMetricType_DOT_PRODUCT.Enum(),
},
},
},
}
_, err := client.CreateSearchIndex(request)
if err != nil {
fmt.Println("Failed to create searchIndex with error:", err)
return
}
}検索インデックス作成時の IndexSort の指定
次の例は、検索インデックスを作成し、インデックスの事前ソートを指定する方法を示しています。検索インデックスには、col1 (Keyword 型) と col2 (Long 型) の 2 つの列が含まれています。
func createSearchIndex_withIndexSort(client *tablestore.TableStoreClient){
request := &tablestore.CreateSearchIndexRequest{}
request.TableName = "<TABLE_NAME>" // テーブル名を設定します。
request.IndexName = "<SEARCH_INDEX_NAME>" // 検索インデックス名を設定します。
schemas := []*tablestore.FieldSchema{}
field1 := &tablestore.FieldSchema{
FieldName: proto.String("col1"), // フィールド名を設定します。proto.String を使用して文字列ポインターを取得します。
FieldType: tablestore.FieldType_KEYWORD, // フィールドタイプを設定します。
Index: proto.Bool(true), // インデックス作成を有効にします。
EnableSortAndAgg: proto.Bool(true), // ソートと統計的集約を有効にします。
}
field2 := &tablestore.FieldSchema{
FieldName: proto.String("col2"),
FieldType: tablestore.FieldType_LONG,
Index: proto.Bool(true),
EnableSortAndAgg: proto.Bool(true),
}
schemas = append(schemas, field1, field2)
request.IndexSchema = &tablestore.IndexSchema{
FieldSchemas: schemas, // 検索インデックスに含めるフィールドを設定します。
IndexSort: &search.Sort{ // インデックスの事前ソートを指定します。col2 で昇順、次に col1 で降順にソートします。
Sorters: []search.Sorter{
&search.FieldSort{
FieldName: "col2",
Order: search.SortOrder_ASC.Enum(),
},
&search.FieldSort{
FieldName: "col1",
Order: search.SortOrder_DESC.Enum(),
},
},
},
}
resp, err := client.CreateSearchIndex(request) // クライアントを呼び出して検索インデックスを作成します。
if err != nil {
fmt.Println("error :", err)
return
}
fmt.Println("CreateSearchIndex finished, requestId:", resp.ResponseInfo.RequestId)
}検索インデックス作成時の生存時間 (TTL) の設定
データテーブルの更新が無効になっていることを確認してください。
func createIndexWithTTL(client *tablestore.TableStoreClient) {
request := &tablestore.CreateSearchIndexRequest{}
request.TableName = "<TABLE_NAME>"
request.IndexName = "<SEARCH_INDEX_NAME>"
schemas := []*tablestore.FieldSchema{}
field1 := &tablestore.FieldSchema{
FieldName: proto.String("col1"), // フィールド名を設定します。proto.String を使用して文字列ポインターを取得します。
FieldType: tablestore.FieldType_KEYWORD, // フィールドタイプを設定します。
Index: proto.Bool(true), // インデックス作成を有効にします。
EnableSortAndAgg: proto.Bool(true), // ソートと統計的集約を有効にします。
}
field2 := &tablestore.FieldSchema{
FieldName: proto.String("col2"),
FieldType: tablestore.FieldType_LONG,
Index: proto.Bool(true),
EnableSortAndAgg: proto.Bool(true),
}
schemas = append(schemas, field1, field2)
request.IndexSchema = &tablestore.IndexSchema{
FieldSchemas: schemas, // 検索インデックスに含めるフィールドを設定します。
}
request.TimeToLive = proto.Int32(3600 * 24 * 7) // 検索インデックスの TTL を 7 日に設定します。
resp, err := client.CreateSearchIndex(request)
if err != nil {
fmt.Println("error :", err)
return
}
fmt.Println("createIndexWithTTL finished, requestId:", resp.ResponseInfo.RequestId)
}検索インデックス作成時のクエリハイライトの有効化
次の例は、4 つの列を持つ検索インデックスを作成する方法を示しています:col_keyword (Keyword 型)、col_long (Long 型)、col_text (Text 型)、および col_nested (Nested 型)。col_nested 列には、level1_text (Text 型) と level1_nested (Nested 型) の 2 つのサブ列が含まれています。level1_nested サブ列には、さらに level2_text (Text 型) サブ列が含まれています。クエリハイライトは、col_text 列、col_nested の level1_text サブ列、および col_nested.level1_nested の level2_text サブ列で有効になっています。
func createSearchIndexwithHighlighting(client *tablestore.TableStoreClient) {
request := &tablestore.CreateSearchIndexRequest{}
request.TableName = "<TABLE_NAME>"
request.IndexName = "<SEARCH_INDEX_NAME>"
request.IndexSchema = &tablestore.IndexSchema{
FieldSchemas: []*tablestore.FieldSchema{
{
FieldName: proto.String("col_keyword"),
FieldType: tablestore.FieldType_KEYWORD, // 文字列型。
Index: proto.Bool(true),
EnableSortAndAgg: proto.Bool(true),
},
{
FieldName: proto.String("col_long"),
FieldType: tablestore.FieldType_LONG, // 数値型。
Index: proto.Bool(true),
EnableSortAndAgg: proto.Bool(true),
},
{// ネストされていない型のクエリハイライトを有効にします。
FieldName: proto.String("col_text"),
FieldType: tablestore.FieldType_TEXT, // トークン化可能な文字列型。
Index: proto.Bool(true),
EnableSortAndAgg: proto.Bool(true),
EnableHighlighting: proto.Bool(true),
},
{// ネストされた型フィールドのサブ列でクエリハイライトを有効にします。
FieldName: proto.String("col_nested"),
FieldType: tablestore.FieldType_NESTED,
FieldSchemas: []*tablestore.FieldSchema{
{
FieldName: proto.String("level1_text"),
FieldType: tablestore.FieldType_TEXT,
Index: proto.Bool(true),
EnableHighlighting: proto.Bool(true),
},
{
FieldName: proto.String("level1_nested"),
FieldType: tablestore.FieldType_NESTED,
FieldSchemas: []*tablestore.FieldSchema{
{
FieldName: proto.String("level2_text"),
FieldType: tablestore.FieldType_TEXT,
Index: proto.Bool(true),
EnableHighlighting: proto.Bool(true),
},
},
},
},
},
},
}
_, err := client.CreateSearchIndex(request)
if err != nil {
fmt.Println("Failed to create searchIndex with error:", err)
return
}
}よくある質問
関連ドキュメント
検索インデックスを作成した後、さまざまなクエリタイプを使用して多次元データクエリを実行できます。これらのクエリタイプには、term クエリ、terms クエリ、match all クエリ、match クエリ、match phrase クエリ、prefix クエリ、範囲クエリ、wildcard クエリ、geo クエリ、boolean クエリ、AISearch、nested クエリ、および 列存在クエリ があります。
データをクエリする際に、結果セットをソートおよびページネーション、ハイライト、または折りたたみ (重複排除) することができます。
検索インデックスを作成した後、スキーマの動的変更、ライフサイクル管理、検索インデックスのリスト表示、検索インデックス記述のクエリ、検索インデックスの削除などの操作で管理できます。
最大値や最小値の検索、合計の計算、行数のカウントなどのデータ分析を実行したい場合は、統計的集約機能または SQL クエリ機能を使用できます。
データを迅速にエクスポートしたいが、結果セット全体の順序が重要でない場合は、並列スキャン機能を使用できます。