CreateSearchIndex メソッドを使用して、データテーブルに 1 つ以上の検索インデックスを作成します。 検索インデックスを作成する際、クエリ対象のフィールドを指定し、オプションでカスタムルーティングキーや事前ソートなどの高度なオプションを設定できます。
前提条件
開始する前に、次のことを確認してください:
-
Tablestore クライアントが初期化されていること。 詳細については、「Tablestore クライアントの初期化」をご参照ください。
-
データテーブルが作成されており、次の条件を満たしていること。 詳細については、「データテーブルの作成」をご参照ください。
-
最大バージョン数が 1 に設定されていること。
-
生存時間 (TTL) が -1 に設定されているか、データテーブルの更新が無効になっていること。
-
フィールド型の互換性
検索インデックス内の各フィールドのデータ型は、データテーブル内の対応するフィールドのデータ型と一致している必要があります。
パラメーター
検索インデックスを作成するには、テーブル名 (TableName)、検索インデックス名 (IndexName)、およびインデックススキーマ (IndexSchema) を指定します。 IndexSchema には、FieldSchemas (フィールド設定)、IndexSetting (インデックス設定)、および IndexSort (事前ソート設定) が含まれます。
|
パラメーター |
説明 |
|
TableName |
データテーブルの名前。 |
|
IndexName |
検索インデックスの名前。 |
|
FieldSchemas |
FieldSchema オブジェクトのリスト。 各 FieldSchema オブジェクトには、次のパラメーターが含まれます:
|
|
IndexSetting |
インデックス設定。 次のパラメーターが含まれます: RoutingFields (任意):カスタムルーティングフィールド。 1 つ以上のプライマリキー列をルーティングフィールドとして選択します。 ほとんどの場合、ルーティングフィールドは 1 つで十分です。複数のルーティングキーを設定した場合、システムはそれらの値を連結して単一の値にします。 同じルーティングフィールド値を持つレコードは、同じデータパーティションにインデックスが作成されます。 |
|
IndexSort |
インデックスの事前ソート設定。 指定しない場合、データはデフォルトでプライマリキーによってソートされます。 Nested フィールドを含むインデックスは IndexSort をサポートしません。 Sorters (必須):事前ソートメソッド。 プライマリキーまたはフィールド値によるソートをサポートします。 詳細については、「ソートとページネーション」をご参照ください。
|
|
TimeToLive |
任意。 検索インデックス内のデータの生存時間 (TTL) (秒単位)。 デフォルト:-1 (データは期限切れになりません)。 最小値:86400 秒 (1 日)。 保持期間が設定された TTL を超えると、期限切れのデータは自動的に削除されます。 |
例
デフォルト設定での検索インデックスの作成
Keyword_type_col (Keyword)、Long_type_col (Long)、および Text_type_col (Text) の 3 つのフィールドを持つ検索インデックスを作成します。Keyword フィールドと Long フィールドでは、ソートと集約が有効になっています。
/// <summary>
/// 3 つの属性列 (Keyword_type_col、Long_type_col、Text_type_col) を持つ検索インデックスを作成します。
/// フィールド型:Keyword (トークン化されていない文字列)、Long (整数)、および Text (トークン化された文字列)。
/// </summary>
/// <param name="otsClient"></param>
public static void CreateSearchIndex(OTSClient otsClient)
{
// テーブル名と検索インデックス名を設定します。
CreateSearchIndexRequest request = new CreateSearchIndexRequest(TableName, IndexName);
List<FieldSchema> FieldSchemas = new List<FieldSchema>() {
new FieldSchema(Keyword_type_col, FieldType.KEYWORD) {
index = true, // インデックス作成を有効にします。
EnableSortAndAgg = true // ソートと集約を有効にします。
},
new FieldSchema(Long_type_col, FieldType.LONG) { index = true, EnableSortAndAgg = true },
new FieldSchema(Text_type_col, FieldType.TEXT) { index = true }
};
request.IndexSchame = new IndexSchema()
{
FieldSchemas = FieldSchemas
};
// 検索インデックスを作成します。
CreateSearchIndexResponse response = otsClient.CreateSearchIndex(request);
Console.WriteLine("Searchindex is created: " + IndexName);
}
IndexSort を指定した検索インデックスの作成
Keyword_type_col (Keyword)、Long_type_col (Long)、および Text_type_col (Text) の 3 つのフィールドを持つ検索インデックスを作成します。 Long_type_col フィールドで結果を昇順に事前ソートします。
/// <summary>
/// 3 つの属性列 (Keyword_type_col、Long_type_col、Text_type_col) を持つ検索インデックスを作成します。
/// フィールド型:Keyword (トークン化されていない文字列)、Long (整数)、および Text (トークン化された文字列)。
/// Long_type_col で結果を昇順に事前ソートします。
/// </summary>
/// <param name="otsClient"></param>
public static void CreateSearchIndexWithIndexSort(OTSClient otsClient)
{
// テーブル名と検索インデックス名を設定します。
CreateSearchIndexRequest request = new CreateSearchIndexRequest(TableName, IndexName);
List<FieldSchema> FieldSchemas = new List<FieldSchema>() {
new FieldSchema(Keyword_type_col, FieldType.KEYWORD) {
index = true,
EnableSortAndAgg = true
},
new FieldSchema(Long_type_col, FieldType.LONG) { index = true, EnableSortAndAgg = true },
new FieldSchema(Text_type_col, FieldType.TEXT) { index = true }
};
request.IndexSchame = new IndexSchema()
{
FieldSchemas = FieldSchemas,
// Long_type_col (昇順) で事前ソートします。 このフィールドでは EnableSortAndAgg を有効にする必要があります。
IndexSort = new DataModel.Search.Sort.Sort()
{
Sorters = new List<DataModel.Search.Sort.ISorter>
{
new DataModel.Search.Sort.FieldSort(Long_type_col, DataModel.Search.Sort.SortOrder.ASC)
}
}
};
CreateSearchIndexResponse response = otsClient.CreateSearchIndex(request);
Console.WriteLine("Searchindex is created: " + IndexName);
}
日付列と仮想カラムを含む検索インデックスの作成
pk0 (Keyword)、pk1 (Long)、date_col (Date)、geo_col (Geo-point)、および col0_v1 (Text) の 5 つのフィールドを持つ検索インデックスを作成します。 仮想カラム col0_v1 は、ソース列 col0 にマッピングされます。 結果は pk1 で昇順に事前ソートされます。
/// <summary>
/// Date フィールドと仮想カラムを含む検索インデックスを作成します。
/// 仮想カラム col0_v1 は、データテーブルのソース列 col0 にマッピングされます。
/// </summary>
/// <param name="otsClient"></param>
public static void CreateSearchIndexWithVirtualAndDateColumn(OTSClient otsClient)
{
List<FieldSchema> fieldSchemas = new List<FieldSchema> {
new FieldSchema("pk0", FieldType.KEYWORD)
{
index = true,
EnableSortAndAgg = true
},
new FieldSchema("pk1", FieldType.LONG)
{
index = true,
EnableSortAndAgg = true
},
new FieldSchema("date_col", FieldType.DATE)
{
index = true,
DateFormats = new List<string>(){
"yyyy-MM-dd'T'HH:mm:ss.SSSSSS",
"yyyy-MM-dd'T'HH:mm:ss.SSS"
}
},
new FieldSchema("geo_col", FieldType.GEO_POINT)
{
index = true,
EnableSortAndAgg = true
},
new FieldSchema("col0_v1", FieldType.TEXT)
{
index = true,
Analyzer = Analyzer.Split,
AnalyzerParameter = new SingleWordAnalyzerParameter(true, true),
IsVirtualField = true,
SourceFieldNames = new List<string> { "col0" } // ソース列 col0 にマッピングします。
},
};
CreateSearchIndexRequest request = new CreateSearchIndexRequest(TableName, IndexName);
request.IndexSchame = new IndexSchema()
{
FieldSchemas = fieldSchemas,
IndexSort = new Sort(new List<ISorter> { new FieldSort("pk1", SortOrder.ASC) })
};
request.TimeToLive = -1; // データは期限切れになりません。
otsClient.CreateSearchIndex(request);
}
よくある質問
次のステップ
-
多次元データ分析のために、Term クエリ、Terms クエリ、Match All クエリ、Match クエリ、Match Phrase クエリ、Prefix クエリ、Range クエリ、Wildcard クエリ、Geo クエリ、Bool クエリ、Nested クエリ、Exists クエリ など、さまざまなクエリタイプを使用して検索インデックスをクエリできます。
-
クエリ結果に ソートとページネーション または 折りたたみ (重複排除) を適用できます。
-
スキーマを動的に変更したり、検索インデックスを一覧表示したり、検索インデックスの説明をクエリしたり、検索インデックスを削除したりして、検索インデックスを管理できます。
-
特定の結果順序を必要とせずにデータを迅速にエクスポートするには、並列スキャン 機能を使用できます。