Gunakan metode `CreateSearchIndex` untuk membuat indeks pencarian pada tabel data. Satu tabel data dapat memiliki beberapa indeks pencarian. Saat membuat indeks pencarian, Anda harus menambahkan bidang-bidang yang ingin dikueri serta mengonfigurasi opsi lanjutan seperti bidang routing dan pengurutan awal.
Prasyarat
Klien Tablestore telah diinisialisasi. Untuk informasi selengkapnya, lihat Initialize the Tablestore client.
Anda telah membuat tabel data yang memenuhi kondisi berikut. Untuk informasi selengkapnya, lihat Create a data table.
max versions harus bernilai 1.
time to live (TTL) bernilai -1, atau pembaruan dinonaktifkan untuk tabel data tersebut.
Catatan
Saat membuat indeks pencarian, data type suatu bidang dalam indeks pencarian harus sesuai dengan tipe data bidang yang bersesuaian dalam tabel data.
Jika Anda ingin menetapkan TTL tertentu untuk indeks pencarian yang bukan -1, Anda harus menonaktifkan pembaruan pada tabel data. TTL indeks pencarian harus kurang dari atau sama dengan TTL tabel data. Untuk informasi selengkapnya, lihat Lifecycle management.
Parameter
Saat membuat indeks pencarian, Anda harus menentukan nama tabel (TableName), nama indeks pencarian (IndexName), dan struktur indeks (IndexSchema). Parameter IndexSchema mencakup FieldSchemas (pengaturan untuk semua bidang indeks), IndexSetting (pengaturan indeks), dan IndexSort (pengaturan pengurutan awal untuk indeks). Tabel berikut menjelaskan parameter-parameter ini secara rinci.
Parameter | Deskripsi |
TableName | Nama tabel data. |
IndexName | Nama indeks pencarian. |
FieldSchemas | Daftar objek FieldSchema. Setiap FieldSchema berisi parameter berikut:
|
IndexSetting | Pengaturan indeks. Ini mencakup pengaturan RoutingFields. RoutingFields (Opsional): Bidang routing kustom. Anda dapat memilih beberapa kolom kunci primer sebagai bidang routing. Biasanya, Anda hanya perlu menyetel satu. Jika Anda menyetel beberapa kunci routing, sistem akan menggabungkan nilai-nilainya menjadi satu nilai tunggal. |
IndexSort | Pengaturan pengurutan awal indeks. Ini mencakup pengaturan Sorters. Jika tidak disetel, data akan diurutkan berdasarkan kunci primer secara default. Catatan IndexSort tidak didukung untuk indeks yang berisi bidang bertipe Nested. Indeks semacam ini tidak memiliki pengurutan awal. Sorters (Wajib): Metode pengurutan awal untuk indeks. Anda dapat mengurutkan berdasarkan kunci primer atau berdasarkan nilai bidang. Untuk informasi selengkapnya tentang pengurutan, lihat Sorting and pagination.
|
TimeToLive | Untuk informasi selengkapnya tentang siklus hidup indeks pencarian, lihat Lifecycle management. |
Contoh
Buat indeks pencarian dengan konfigurasi default
Contoh berikut menunjukkan cara membuat indeks pencarian yang berisi tiga kolom: col_keyword (tipe Keyword), col_long (tipe Long), dan col_vector (tipe Vector).
func createSearchIndex(client *tablestore.TableStoreClient) {
request := &tablestore.CreateSearchIndexRequest{}
request.TableName = "<TABLE_NAME>"
request.IndexName = "<SEARCH_INDEX_NAME>"
request.IndexSchema = &tablestore.IndexSchema{
FieldSchemas: []*tablestore.FieldSchema{
{
FieldName: proto.String("col_keyword"),
FieldType: tablestore.FieldType_KEYWORD, // String type
Index: proto.Bool(true),
EnableSortAndAgg: proto.Bool(true),
},
{
FieldName: proto.String("col_long"),
FieldType: tablestore.FieldType_LONG, // Numeric type
Index: proto.Bool(true),
EnableSortAndAgg: proto.Bool(true),
},
{
FieldName: proto.String("col_vector"),
FieldType: tablestore.FieldType_VECTOR, // Vector type
Index: proto.Bool(true),
VectorOptions: &tablestore.VectorOptions{
VectorDataType: tablestore.VectorDataType_FLOAT_32.Enum(),
Dimension: proto.Int32(4), // The vector dimension is 4, and the similarity algorithm is dot product.
VectorMetricType: tablestore.VectorMetricType_DOT_PRODUCT.Enum(),
},
},
},
}
_, err := client.CreateSearchIndex(request)
if err != nil {
fmt.Println("Failed to create searchIndex with error:", err)
return
}
}Tentukan IndexSort saat membuat indeks pencarian
Contoh berikut menunjukkan cara membuat indeks pencarian dan menentukan pengurutan awal untuk indeks tersebut. Indeks pencarian tersebut berisi dua kolom: col1 (tipe Keyword) dan col2 (tipe Long).
func createSearchIndex_withIndexSort(client *tablestore.TableStoreClient){
request := &tablestore.CreateSearchIndexRequest{}
request.TableName = "<TABLE_NAME>" // Set the table name.
request.IndexName = "<SEARCH_INDEX_NAME>" // Set the search index name.
schemas := []*tablestore.FieldSchema{}
field1 := &tablestore.FieldSchema{
FieldName: proto.String("col1"), // Set the field name. Use proto.String to get the string pointer.
FieldType: tablestore.FieldType_KEYWORD, // Set the field type.
Index: proto.Bool(true), // Enable indexing.
EnableSortAndAgg: proto.Bool(true), // Enable sorting and statistical aggregation.
}
field2 := &tablestore.FieldSchema{
FieldName: proto.String("col2"),
FieldType: tablestore.FieldType_LONG,
Index: proto.Bool(true),
EnableSortAndAgg: proto.Bool(true),
}
schemas = append(schemas, field1, field2)
request.IndexSchema = &tablestore.IndexSchema{
FieldSchemas: schemas, // Set the fields to include in the search index.
IndexSort: &search.Sort{ // Specify index pre-sorting. Sort by col2 in ascending order, then by col1 in descending order.
Sorters: []search.Sorter{
&search.FieldSort{
FieldName: "col2",
Order: search.SortOrder_ASC.Enum(),
},
&search.FieldSort{
FieldName: "col1",
Order: search.SortOrder_DESC.Enum(),
},
},
},
}
resp, err := client.CreateSearchIndex(request) // Call the client to create the search index.
if err != nil {
fmt.Println("error :", err)
return
}
fmt.Println("CreateSearchIndex finished, requestId:", resp.ResponseInfo.RequestId)
}Tetapkan time to live saat membuat indeks pencarian
Pastikan bahwa pembaruan dinonaktifkan untuk tabel data tersebut.
func createIndexWithTTL(client *tablestore.TableStoreClient) {
request := &tablestore.CreateSearchIndexRequest{}
request.TableName = "<TABLE_NAME>"
request.IndexName = "<SEARCH_INDEX_NAME>"
schemas := []*tablestore.FieldSchema{}
field1 := &tablestore.FieldSchema{
FieldName: proto.String("col1"), // Set the field name. Use proto.String to get the string pointer.
FieldType: tablestore.FieldType_KEYWORD, // Set the field type.
Index: proto.Bool(true), // Enable indexing.
EnableSortAndAgg: proto.Bool(true), // Enable sorting and statistical aggregation.
}
field2 := &tablestore.FieldSchema{
FieldName: proto.String("col2"),
FieldType: tablestore.FieldType_LONG,
Index: proto.Bool(true),
EnableSortAndAgg: proto.Bool(true),
}
schemas = append(schemas, field1, field2)
request.IndexSchema = &tablestore.IndexSchema{
FieldSchemas: schemas, // Set the fields to include in the search index.
}
request.TimeToLive = proto.Int32(3600 * 24 * 7) // Set the TTL of the search index to 7 days.
resp, err := client.CreateSearchIndex(request)
if err != nil {
fmt.Println("error :", err)
return
}
fmt.Println("createIndexWithTTL finished, requestId:", resp.ResponseInfo.RequestId)
}Aktifkan penyorotan kueri saat membuat indeks pencarian
Contoh berikut menunjukkan cara membuat indeks pencarian dengan empat kolom: col_keyword (tipe Keyword), col_long (tipe Long), col_text (tipe Text), dan col_nested (tipe Nested). Kolom col_nested berisi dua sub-kolom: level1_text (tipe Text) dan level1_nested (tipe Nested). Sub-kolom level1_nested, pada gilirannya, berisi sub-kolom level2_text (tipe Text). Penyorotan kueri diaktifkan untuk kolom col_text, sub-kolom level1_text dari col_nested, dan sub-kolom level2_text dari col_nested.level1_nested.
func createSearchIndexwithHighlighting(client *tablestore.TableStoreClient) {
request := &tablestore.CreateSearchIndexRequest{}
request.TableName = "<TABLE_NAME>"
request.IndexName = "<SEARCH_INDEX_NAME>"
request.IndexSchema = &tablestore.IndexSchema{
FieldSchemas: []*tablestore.FieldSchema{
{
FieldName: proto.String("col_keyword"),
FieldType: tablestore.FieldType_KEYWORD, // String type.
Index: proto.Bool(true),
EnableSortAndAgg: proto.Bool(true),
},
{
FieldName: proto.String("col_long"),
FieldType: tablestore.FieldType_LONG, // Numeric type.
Index: proto.Bool(true),
EnableSortAndAgg: proto.Bool(true),
},
{// Enable query highlighting for a non-nested type.
FieldName: proto.String("col_text"),
FieldType: tablestore.FieldType_TEXT, // Tokenizable string type.
Index: proto.Bool(true),
EnableSortAndAgg: proto.Bool(true),
EnableHighlighting: proto.Bool(true),
},
{// Enable query highlighting for sub-columns in a nested type field.
FieldName: proto.String("col_nested"),
FieldType: tablestore.FieldType_NESTED,
FieldSchemas: []*tablestore.FieldSchema{
{
FieldName: proto.String("level1_text"),
FieldType: tablestore.FieldType_TEXT,
Index: proto.Bool(true),
EnableHighlighting: proto.Bool(true),
},
{
FieldName: proto.String("level1_nested"),
FieldType: tablestore.FieldType_NESTED,
FieldSchemas: []*tablestore.FieldSchema{
{
FieldName: proto.String("level2_text"),
FieldType: tablestore.FieldType_TEXT,
Index: proto.Bool(true),
EnableHighlighting: proto.Bool(true),
},
},
},
},
},
},
}
_, err := client.CreateSearchIndex(request)
if err != nil {
fmt.Println("Failed to create searchIndex with error:", err)
return
}
}FAQ
Differences between range queries using the GetRange and Search operations
Data cannot be found using the Search operation of a search index
Does Tablestore support queries similar to IN and BETWEEN...AND in relational databases?
The "field:xx must enable enable_sort_and_agg" error occurs when you use a search index
Referensi
Setelah membuat indeks pencarian, Anda dapat menggunakan berbagai jenis kueri untuk melakukan kueri data multidimensi. Jenis kueri tersebut meliputi term query, terms query, match all query, match query, match phrase query, prefix query, range query, wildcard query, geo query, boolean query, AISearch, nested query, dan column existence query.
Saat mengkueri data, Anda dapat mengurutkan dan membagi halaman, menyorot, atau melipat (menghapus duplikat) set hasil.
Setelah membuat indeks pencarian, Anda dapat mengelolanya dengan melakukan operasi seperti memodifikasi skema secara dinamis, mengelola siklus hidup, menampilkan daftar indeks pencarian, mengkueri deskripsi indeks pencarian, dan menghapus indeks pencarian.
Jika Anda ingin melakukan analitik data, seperti mencari nilai maksimum atau minimum, menghitung jumlah, atau menghitung jumlah baris, Anda dapat menggunakan fitur agregasi statistik atau fitur SQL query.
Jika Anda ingin mengekspor data dengan cepat dan urutan seluruh set hasil tidak penting, Anda dapat menggunakan fitur parallel scan.