表格存储多元索引(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"
            }
        }]
    }