全部產品
Search
文件中心

Tablestore:建立多元索引

更新時間:May 01, 2026

使用CreateSearchIndex方法在資料表上建立一個多元索引。一個資料表支援建立多個多元索引。建立多元索引時,您需要將要查詢的欄位添加到多元索引中,您還可以配置多元索引路由鍵、預排序等進階選項。

前提條件

開始前,請確認已完成以下準備工作:

  • 已初始化Table Store用戶端。詳情請參見初始化Tablestore Client

  • 已建立資料表,詳情請參見建立資料表,且資料表同時滿足以下條件:

    • 最大版本數為 1。

    • 資料生命週期(TTL)為 -1,或資料表處于禁止更新狀態。

欄位類型相容性

多元索引中每個欄位的資料類型必須與資料表中對應欄位的資料類型保持一致。

參數

建立多元索引時,需要指定資料表名稱(TableName)、多元索引名稱(IndexName)和索引結構資訊(IndexSchema)。IndexSchema 包含 FieldSchemas(欄位設定)、IndexSetting(索引設定)和 IndexSort(預排序設定)。

參數

說明

TableName

資料表名稱。

IndexName

多元索引名稱。

FieldSchemas

FieldSchema 的列表,每個 FieldSchema 包含以下參數:

  • FieldName(必選):多元索引的欄位名,對應列名,類型為 String。欄位可以是主鍵列或屬性列。

  • FieldType(必選):欄位的資料類型,類型為 FieldType.XXX。

  • Array(可選):是否為數群組類型,類型為 Boolean。設定為 true 時,寫入資料必須使用JSON數組格式,例如 ["a","b","c"]。Nested 類型本身即為數組,FieldType 為 Nested 時無需設定此參數。

  • Index(可選):是否為該欄位開啟索引,類型為 Boolean。預設為 true,開啟後會為該列構建倒排索引或空間索引。設定為 false 則不構建索引。

  • Analyzer(可選)分詞器類型。僅當 FieldType 為 Text 時可設定,預設為單字分詞。

  • EnableSortAndAgg(可選):是否為該欄位開啟排序與統計彙總功能,類型為 Boolean。只有設定為 true 的欄位才支援排序。Nested 類型不支援排序與統計彙總,但 Nested 類型內部的子列支援。

  • IsVirtualField(可選):該欄位是否為虛擬列,類型為 Boolean,預設為 false。僅使用虛擬列時需設定此參數。

  • SourceFieldNames(可選):資料表中的源列名稱,類型為 String。IsVirtualField 為 true 時,必須設定此參數。

  • DateFormats(可選):日期格式,類型為 String。FieldType 為 Date 時,必須設定此參數。更多資訊,請參見日期時間類型

IndexSetting

索引設定,包含以下參數:

RoutingFields(可選)自訂路由欄位。可以選擇部分主鍵列作為路由欄位,一般情況下只需要設定一個。如果設定多個路由鍵,系統會將多個路由鍵的值拼接成一個值。

在進行索引資料寫入時,系統會根據路由欄位的值計算索引資料的分布位置,路由欄位的值相同的記錄會被索引到相同的資料分區中。

IndexSort

索引的預排序設定。不設定時,預設按主鍵排序。含有 Nested 類型欄位的索引不支援 IndexSort。

Sorters(必選):預排序方式,支援按主鍵排序和按欄位值排序。更多資訊,請參見排序和翻頁

  • PrimaryKeySort:按主鍵排序。通過 Order 參數指定升序或降序,預設為升序(DataModel.Search.Sort.SortOrder.ASC)。

  • FieldSort:按欄位值排序,僅已建立索引且開啟排序與統計彙總功能的欄位才能用於預排序。包含以下設定:

    • FieldName:排序欄位名。

    • Order:排序次序,支援升序或降序,預設為升序(DataModel.Search.Sort.SortOrder.ASC)。

    • Mode:欄位存在多個值時的排序方式。

TimeToLive

選擇性參數。資料生命週期(TTL),即資料的儲存時間,單位為秒。

預設值為 -1,表示資料永不到期。資料生命週期的取值最低為 86400 秒(一天),也可設定為 -1(永不到期)。

當資料的儲存時間超過設定的資料生命週期時,系統會自動清理超過資料生命週期的資料。

樣本

建立多元索引時使用預設配置

建立一個包含三個欄位的多元索引:Keyword_type_col(Keyword 類型)、Long_type_col(Long 類型)和 Text_type_col(Text 類型),其中 Keyword 和 Long 欄位開啟排序與統計彙總功能。

/// <summary>
/// 建立多元索引,包含三個屬性列: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 類型)。同時配置按 Long_type_col 欄位升序進行預排序。

/// <summary>
/// 建立多元索引,包含三個屬性列: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 類型)。其中虛擬列 col0_v1 映射到資料表中的源列 col0,結果按 pk1 欄位升序預排序。

/// <summary>
/// 建立包含日期列和虛擬列的多元索引。
/// 虛擬列 col0_v1 映射到資料表中的源列 col0。
/// </summary>
/// <param name="otsClient"></param>
public static void CreateSearchIndex(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);
}

常見問題

相關文檔