全部產品
Search
文件中心

Tablestore:檢索Lastpoint索引

更新時間:Apr 01, 2025

多元索引可以加速Lastpoint索引的資料檢索,並提供多維查詢和統計分析功能。本文介紹在Go SDK中如何通過多元索引來檢索Lastpoint索引資料。

注意事項

Table StoreGo SDK從v1.7.15版本開始支援Lastpoint索引功能。使用該功能時,請將SDK版本升級到v1.7.15及以上版本。

前提條件

已在時序表上建立Lastpoint索引。具體操作,請參見建立Lastpoint索引

使用流程

1. 建立多元索引

以下樣本用於為Lastpoint索引建立一個多元索引。樣本情境及資料請參見附錄

說明

樣本中_tags列為標籤構成的字串數組,建議將對應的多元索引欄位類型設定為Keyword數組,以便更加方便地對_tags內的標籤進行查詢。

func createSearchIndex(client *tablestore.TableStoreClient) {
	request := &tablestore.CreateSearchIndexRequest{}
	request.TableName = "<LASTPOINT_INDEX_NAME>"
	request.IndexName = "<SEARCH_INDEX_NAME>"
	request.IndexSchema = &tablestore.IndexSchema{
		FieldSchemas: []*tablestore.FieldSchema{
			{
				FieldName:        proto.String("_#h"),
				FieldType:        tablestore.FieldType_KEYWORD,
				Index:            proto.Bool(true),
				EnableSortAndAgg: proto.Bool(true),
			},
			{
				FieldName:        proto.String("_m_name"),
				FieldType:        tablestore.FieldType_KEYWORD,
				Index:            proto.Bool(true),
				EnableSortAndAgg: proto.Bool(true),
			},
			{
				FieldName:        proto.String("_data_source"),
				FieldType:        tablestore.FieldType_KEYWORD,
				Index:            proto.Bool(true),
				EnableSortAndAgg: proto.Bool(true),
			},
			{
				FieldName:        proto.String("_tags"),
				FieldType:        tablestore.FieldType_KEYWORD,
				Index:            proto.Bool(true),
				EnableSortAndAgg: proto.Bool(true),
				IsArray:          proto.Bool(true),
			},
			{
				FieldName:        proto.String("_time"),
				FieldType:        tablestore.FieldType_LONG,
				Index:            proto.Bool(true),
				EnableSortAndAgg: proto.Bool(true),
			},
			{
				FieldName:        proto.String("gps"),
				FieldType:        tablestore.FieldType_GEO_POINT,
				Index:            proto.Bool(true),
				EnableSortAndAgg: proto.Bool(true),
			},
			{
				FieldName:        proto.String("speed"),
				FieldType:        tablestore.FieldType_DOUBLE,
				Index:            proto.Bool(true),
				EnableSortAndAgg: proto.Bool(true),
			},
			{
				FieldName:        proto.String("status"),
				FieldType:        tablestore.FieldType_KEYWORD,
				Index:            proto.Bool(true),
				EnableSortAndAgg: proto.Bool(true),
			},
			{
				FieldName:        proto.String("total_mileage"),
				FieldType:        tablestore.FieldType_LONG,
				Index:            proto.Bool(true),
				EnableSortAndAgg: proto.Bool(true),
			},
			{
				FieldName:        proto.String("remaining_mileage"),
				FieldType:        tablestore.FieldType_LONG,
				Index:            proto.Bool(true),
				EnableSortAndAgg: proto.Bool(true),
			},
		},
	}
	_, err := client.CreateSearchIndex(request)
	if err != nil {
		fmt.Println("Failed to create searchIndex with error:", err)
		return
	}
}

2. 通過多元索引檢索資料

此處以範圍查詢為例介紹多元索引查詢功能的使用。

以下樣本用於通過多元索引檢索Lastpoint索引中speed值大於20.0的行資料。

func RangeQuery(client *tablestore.TableStoreClient, lastpointName string, indexName string) {
	searchRequest := &tablestore.SearchRequest{}
	searchRequest.SetTableName(lastpointName)
	searchRequest.SetIndexName(indexName)
	searchQuery := search.NewSearchQuery()
	rangeQuery := &search.RangeQuery{} //設定查詢類型為RangeQuery。
	rangeQuery.FieldName = "speed" //設定要匹配的欄位
	rangeQuery.GT(20.0)                //設定該欄位的範圍條件為大於20.0。
	searchQuery.SetQuery(rangeQuery)
	//設定按照speed列逆序排序。
	searchQuery.SetSort(&search.Sort{
		[]search.Sorter{
			&search.FieldSort{
				FieldName: "speed",
				Order:     search.SortOrder_DESC.Enum(),
			},
		},
	})
	searchRequest.SetSearchQuery(searchQuery)
	searchRequest.SetColumnsToGet(&tablestore.ColumnsToGet{
		ReturnAll: true,
	})
	searchResponse, err := client.Search(searchRequest)
	if err != nil {
		fmt.Printf("%#v", err)
		return
	}
	fmt.Println("IsAllSuccess: ", searchResponse.IsAllSuccess) //查看返回結果是否完整。
	fmt.Println("RowCount: ", len(searchResponse.Rows))
	for _, row := range searchResponse.Rows {
		jsonBody, err := json.Marshal(row)
		if err != nil {
			panic(err)
		}
		fmt.Println("Row: ", string(jsonBody))
	}
}

常見問題

相關文檔

多元索引的核心功能包括任意列的查詢(包括主鍵列和非主鍵列)、多欄位自由組合查詢、地理位置查詢、全文檢索索引、模糊查詢、首碼查詢、巢狀查詢、去重、排序、查詢資料總行數和統計彙總等。更多資訊,請參見多元索引功能

附錄

在車連網情境中,車輛通過感應器上報時序資料到雲端。通過儲存、查詢和分析這些時序資料,使用者可以實現車況報告、車輛定位、交通管理和軌跡投屏等業務需求。

假設時序表的資料樣本如下:

說明

其中_m_name_data_source_tags為時間軸標識,分別代表度量名稱、資料來源和時間軸的標籤資訊,_time為資料上報時間。gpsspeedstatustotal_mileageremaining_mileage為時間軸的時序資料,分別代表車輛GPS座標、車輛速度、車輛狀態、車輛總裡程和車輛剩餘裡程。

_m_name

_data_source

_tags

_time

gps

speed

status

total_mileage

remaining_mileage

平台A

sensor1

["region=hangzhou","car_model=sedan","number_plate=浙AD7512*","color=white"]

1730422800000000

30.245853,120.178564

0

閑置

20000

450

平台A

sensor1

["region=hangzhou","car_model=sedan","number_plate=浙AD7512*","color=white"]

1730423400000000

30.245853,120.178564

0

閑置

20000

450

平台A

sensor2

["region=hangzhou","car_model=suv","number_plate=浙C72B2*","color=black"]

1730779200000000

30.245278,120.150269

50

使用中

15000

300

平台A

sensor2

["region=hangzhou","car_model=suv","number_plate=浙C72B2*","color=black"]

1730779800000000

30.245853,120.213654

80

使用中

15050

250

平台B

sensor3

["region=hangzhou","car_model=sedan","number_plate=浙B121*9","color=blue"]

1730862000000000

30.246013,120.124470

60

使用中

18200

300

平台B

sensor3

["region=hangzhou","car_model=sedan","number_plate=浙B121*9","color=blue"]

1730862600000000

30.246022,120.124460

0

閑置

18230

270

Table Store會自動同步時序表中時間軸的最新時間點資料到Lastpoint索引表,Lastpoint索引中的資料樣本如下:

_#h

_m_name

_data_source

_tags

_time

gps

speed

status

total_mileage

remaining_mileage

4c#平台A#07

平台A

sensor1

["region=hangzhou","car_model=sedan","number_plate=浙AD7512*","color=white"]

1730423400000000

30.245853,120.178564

0

閑置

20000

450

25#平台A#ae

平台A

sensor2

["region=hangzhou","car_model=suv","number_plate=浙C72B2*","color=black"]

1730779800000000

30.245853,120.213654

80

使用中

15050

250

b2#平台B#4b

平台B

sensor3

["region=hangzhou","car_model=sedan","number_plate=浙B121*9","color=blue"]

1730862600000000

30.246022,120.124460

0

閑置

18230

270