Tablestore多元索引(Search Index)基於倒排索引和列式儲存,可以解決多種巨量資料複雜的查詢問題。建立多元索引後,您可以使用多元索引進行資料查詢。

前提條件

  • 已下載命令列工具。具體操作,請參見下載
  • 已啟動並配置執行個體。具體操作,請參見啟動並配置
  • 已擷取AccessKey。具體操作,請參見擷取AccessKey
  • 已建立並使用資料表,且資料表的最大版本數(max Versions)必須為1。具體操作,請參見建立並使用資料表

步驟一:建立多元索引

  1. 執行create_search_index命令建立一個多元索引search_index。
    create_search_index -n search_index
  2. 根據系統提示輸入索引Schema,樣本如下:

    索引Schema包括IndexSetting(索引設定)、FieldSchemas(Index的所有欄位的設定)和IndexSort(索引預排序設定)。關於索引Schema的更多資訊,請參見建立多元索引

     {
    
        "IndexSetting": {
            "RoutingFields": null
        },
        "FieldSchemas": [
            {
                "FieldName": "gid",
                "FieldType": "LONG",
                "Index": true,
                "EnableSortAndAgg": true,
                "Store": true,
                "IsArray": false,
                "IsVirtualField": false
            },
            {
                "FieldName": "uid",
                "FieldType": "LONG",
                "Index": true,
                "EnableSortAndAgg": true,
                "Store": true,
                "IsArray": false,
                "IsVirtualField": false
            },
            {
                "FieldName": "col2",
                "FieldType": "LONG",
                "Index": true,
                "EnableSortAndAgg": true,
                "Store": true,
                "IsArray": false,
                "IsVirtualField": false
            },
            {
                "FieldName": "col3",
                "FieldType": "TEXT",
                "Index": true,
                "Analyzer": "single_word",
                "AnalyzerParameter": {
                    "CaseSensitive": true,
                    "DelimitWord": null
                },
                "EnableSortAndAgg": false,
                "Store": true,
                "IsArray": false,
                "IsVirtualField": false
            },
            {
                "FieldName": "col1",
                "FieldType": "KEYWORD",
                "Index": true,
                "EnableSortAndAgg": true,
                "Store": true,
                "IsArray": false,
                "IsVirtualField": false
            },
            {
                "FieldName": "col3V",
                "FieldType": "LONG",
                "Index": true,
                "EnableSortAndAgg": true,
                "Store": true,
                "IsArray": false,
                "IsVirtualField": true,
                "SourceFieldNames": [
                    "col3"
                ]
            }
        ]
    }

步驟二:查詢資料

  1. 執行search命令使用search_index多元索引查詢資料,並返回所有建立索引的列。
    search -n search_index --return_all_indexed
  2. 根據系統提示輸入查詢條件,樣本如下:

    多元索引支援全匹配查詢(MatchAllQuery)、匹配查詢(MatchQuery)、短語匹配查詢(MatchPhraseQuery)、精確查詢(TermQuery)、多詞精確查詢(TermsQuery)、首碼查詢(PrefixQuery)等多種查詢方式,此處以精確查詢為例介紹。關於精確查詢的更多資訊,請參見精確查詢

    {
        "Offset": -1,
        "Limit": 10,
        "Collapse": null,
        "Sort": null,
        "GetTotalCount": true,
        "Token": null,
        "Query": {
            "Name": "TermQuery",
            "Query": {
                "FieldName": "uid",
                "Term": 10001
            }
        },
        "Aggregations": [{
            "Name": "avg",
            "Aggregation": {
                "AggName": "agg1",
                "Field": "pid"
            }
        }]
    }