如果實際業務中要用到非主鍵列查詢、多條件組合查詢等多種查詢功能,您可以為資料表建立多元索引,然後使用多元索引查詢資料。本文介紹如何通過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