如果实际业务中要用到非主键列查询、多条件组合查询等多种查询功能,您可以为数据表创建多元索引,然后使用多元索引查询数据。本文介绍如何通过Tablestore CLI快速使用多元索引以及多元索引的创建、查看、使用和删除操作。
创建多元索引
命令格式
create_search_index -n search_index_name
示例
创建search_index多元索引。
create_search_index -n search_index
根据系统提示输入索引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"
]
}
]
}
查看多元索引列表
命令格式
list_search_index
示例
查看当前表下的多元索引列表。
list_search_index -d
查看多元索引信息
命令格式
describe_search_index -n search_index_name
示例
查看search_index多元索引的信息。
describe_search_index -n search_index
返回结果中的Index schema
为多元索引的Schema,TimeToLive为多元索引的数据生命周期。
使用多元索引查询数据
支持使用命令行工具操作的多元索引查询类型包括精确查询、多词精确查询、全匹配查询、匹配查询、短语匹配查询、前缀查询、范围查询、通配符查询、基于分词的通配符查询、组合查询、地理位置查询和列存在性查询,您可以选择合适的查询类型进行多维度数据查询。
命令格式
search -n search_index_name --return_all_indexed
示例
使用search_index多元索引查询表中的数据,并返回所有建立索引的列。
search -n search_index --return_all_indexed
根据系统提示输入查询条件。
以下示例用于查询当前表中uid精确匹配10001的行数据,同时对行数据的pid列进行求平均值操作。
{ "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" } }] }
删除多元索引
命令格式
drop_search_index -n search_index_name -y
示例
删除search_index多元索引。
drop_search_index -n search_index -y
该文章对您有帮助吗?