使用CreateSearchIndex介面在資料表上建立一個多元索引。一個資料表可以建立多個多元索引。

前提條件

  • 已初始化Client。具體操作,請參見初始化
  • 已建立資料表,且資料表的資料生命週期(TimeToLive)必須為-1,最大版本數(MaxVersions)必須為1。

參數

建立多元索引時,需要指定資料表名稱(TableName)、多元索引名稱(IndexName)和索引的結構資訊(IndexSchema),其中IndexSchema包含FieldSchemas(Index的所有欄位的設定)、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(可選):分詞器類型。當欄位類型為Text時,可以設定此參數;如果不設定,則預設分詞器類型為單字分詞。關於分詞的更多資訊,請參見分詞
  • EnableSortAndAgg(可選):是否開啟排序與統計彙總功能,類型為Boolean。
    只有EnableSortAndAgg設定為true的欄位才能進行排序。關於排序的更多資訊,請參見排序和翻頁
    注意 Nested類型的欄位不支援開啟排序與統計彙總功能,但是Nested類型內部的子列支援開啟排序與統計彙總功能。
  • Store(可選):是否在多元索引中附加儲存該欄位的值,類型為Boolean。

    開啟後,可以直接從多元索引中讀取該欄位的值,而不必反查資料表,可用於查詢效能最佳化。

  • IsVirtualField(可選):該欄位是否為虛擬列,類型為Boolean類型,預設值為false。只有使用虛擬列時,才需要設定此參數。關於虛擬列的更多資訊,請參見虛擬列
  • SourceFieldNames(可選):資料表中的欄位名稱,類型為String。
    注意 當設定IsVirtualField為true時,必須設定此參數。
  • DateFormats(可選):日期的格式,類型為String。更多資訊,請參見日期資料類型
    注意 當欄位類型為Date時,必須設定此參數。
IndexSetting 索引設定,包含RoutingFields設定。

RoutingFields(可選):自訂路由欄位。可以選擇部分主鍵列作為路由欄位,在進行索引資料寫入時,會根據路由欄位的值計算索引資料的分布位置,路由欄位的值相同的記錄會被索引到相同的資料分區中。

IndexSort 索引預排序設定,包含Sorters設定。如果不設定,則預設按照主鍵排序。
说明 含有Nested類型的索引不支援IndexSort,沒有預排序。
Sorters(必選):索引的預排序方式,支援按照主鍵排序和欄位值排序。關於排序的更多資訊,請參見排序和翻頁
  • PrimaryKeySort表示按照主鍵排序,包含如下設定:

    Order:排序的順序,可按升序或者降序排序,預設為升序(DataModel.Search.Sort.SortOrder.ASC)。

  • FieldSort表示按照欄位值排序,包含如下設定:

    只有建立索引且開啟排序與統計彙總功能的欄位才能進行預排序。

    • FieldName:排序的欄位名。
    • Order:排序的順序,可按照升序或者降序排序,預設為升序(DataModel.Search.Sort.SortOrder.ASC)。
    • Mode:當欄位存在多個值時的排序方式。
TimeToLive 選擇性參數,預設值為-1。資料生命週期(TTL),即資料的儲存時間。

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

資料生命週期至少為86400秒(一天)或-1(資料永不到期)。

樣本

  • 樣本1

    建立一個多元索引。

    /// <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
        };
        //調用client建立多元索引。
        CreateSearchIndexResponse response = otsClient.CreateSearchIndex(request);
        Console.WriteLine("Searchindex is created: " + IndexName);
    }
  • 樣本2
    建立多元索引時指定IndexSort。
    /// <summary>
    /// 建立一個多元索引,包含Keyword_type_col、Long_type_col、Text_type_col三個屬性列,類型分別設定為不分詞字串(Keyword)、整型(Long)、分詞字串(Text)。
    /// </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列進行預排序,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);
    }
  • 樣本3

    建立一個包含日期列和虛擬列的多元索引。

    /// <summary>
    /// 建立一個包含日期列和虛擬列的多元索引。
    /// </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" }
            },
        };
    
        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);
    }