TRIE
Milvus 中的「TRIE 」索引會將 `VARCHAR ` 欄位的值組織成一個前綴樹(Trie)。字串中的每個字元都對應樹中的一條邊,而相同的前綴僅儲存一次。此結構能快速執行精確的字串查詢與前綴比對,無需掃描該欄位中的每個值。
若您經常執行以下類型的查詢,請使用TRIE 索引:
對字串值進行精確相等比對,例如
category == "books"前綴篩選,例如
name LIKE "abc%"
支援的資料類型
VARCHAR字段。詳細資訊請參閱「字串字段」。
TRIE 此功能僅支援VARCHAR 欄位。若要為JSON 欄位中的字串值建立索引,請將路徑轉換為VARCHAR ,並改用NGRAM索引。
運作原理
Milvus 分兩個階段實作「TRIE 」:
建立索引
在資料導入過程中,Milvus 會讀取每個已建立索引欄位的值。
這些值會被插入到一個三叉樹(trie)中:字元儲存在邊上,而每個終端節點則記錄共享該字串的實體 ID。
建構完成的樹狀結構將作為索引儲存下來。
加速查詢
對於相等查詢,Milvus 會逐個字元遍歷樹,直到找到與精確值相符的節點,並返回其實體 ID。
對於前綴查詢,Milvus 會逐個字元遍歷樹,直到找到與該前綴完全匹配的節點,並返回其實體 ID。
匹配的實體 ID 會傳遞給查詢執行器,以進行最終結果的彙整。
對於等值和前綴篩選,查詢成本與字串長度成正比,而非實體數量,這使得這些查詢在大型集合中仍能保持高速。
建立 TRIE 索引
您可以在VARCHAR 欄位上建立TRIE 索引。無需任何額外參數。
以下範例展示如何在VARCHAR 欄位上建立TRIE 索引:
from pymilvus import MilvusClient
client = MilvusClient(uri="http://localhost:19530")
# Assume you have defined a VARCHAR field named "category" in your collection schema
# Prepare index parameters
index_params = client.prepare_index_params()
# Add TRIE index on the "category" field
index_params.add_index(
field_name="category", # Target VARCHAR field
index_type="TRIE", # Index type is TRIE
index_name="category_trie" # Optional, name your index
)
# Create the index on the collection
client.create_index(
collection_name="product_catalog",
index_params=index_params
)
import io.milvus.v2.client.ConnectConfig;
import io.milvus.v2.client.MilvusClientV2;
import io.milvus.v2.common.IndexParam;
import io.milvus.v2.service.index.request.CreateIndexReq;
import java.util.Collections;
MilvusClientV2 client = new MilvusClientV2(ConnectConfig.builder()
.uri("http://localhost:19530")
.build());
client.createIndex(CreateIndexReq.builder()
.collectionName("product_catalog")
.indexParams(Collections.singletonList(IndexParam.builder()
.fieldName("category")
.indexType(IndexParam.IndexType.TRIE)
.indexName("category_trie")
.build()))
.build());
import (
"context"
"log"
"github.com/milvus-io/milvus/client/v3/index"
"github.com/milvus-io/milvus/client/v3/milvusclient"
)
ctx := context.Background()
cli, err := milvusclient.New(ctx, &milvusclient.ClientConfig{
Address: "http://localhost:19530",
})
if err != nil {
log.Fatal("failed to connect to milvus server: ", err.Error())
}
_, err = cli.CreateIndex(ctx, milvusclient.NewCreateIndexOption("product_catalog", "category", index.NewTrieIndex()).WithIndexName("category_trie"))
if err != nil {
log.Fatal("failed to create index: ", err.Error())
}
use milvus::v2::prelude::*;
let config = ConnectConfig::new().uri("http://localhost:19530");
let client = ClientV2::new(&config).await?;
client
.create_index(
CreateIndexRequest::builder()
.collection_name("product_catalog")
.index_param(
IndexParam::new()
.field_name("category")
.index_type(IndexType::Trie)
.index_name("category_trie"),
)
.build()?,
)
.await?;
#include <iostream>
#include "milvus/MilvusClientV2.h"
auto client = milvus::MilvusClientV2::Create();
auto status = client->Connect(milvus::ConnectParam("http://localhost:19530"));
if (!status.IsOk()) {
std::cout << status.Message() << std::endl;
}
status = client->CreateIndex(milvus::CreateIndexRequest().WithCollectionName("product_catalog")
.AddIndex(milvus::IndexDesc("category", "category_trie", milvus::IndexType::TRIE)));
if (!status.IsOk()) {
std::cout << status.Message() << std::endl;
}
import { MilvusClient } from "@zilliz/milvus2-sdk-node";
const client = new MilvusClient({ address: "http://localhost:19530" });
await client.createIndex({
collection_name: "product_catalog",
field_name: "category",
index_type: "TRIE",
index_name: "category_trie",
});
export CLUSTER_ENDPOINT="http://localhost:19530"
export TOKEN="root:Milvus"
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/indexes/create" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
--header "Request-Timeout: 10" \
-d '{
"collectionName": "product_catalog",
"indexParams": [
{
"fieldName": "category",
"indexName": "category_trie",
"indexType": "TRIE"
}
]
}'
# {
# "code": 0,
# "data": {}
# }
由 TRIE 加速的查詢
TRIE 索引可加速針對VARCHAR 欄位的以下篩選模式:
等值
filter = 'category == "books"'成員關係
filter = 'category in ["books", "electronics"]'前綴匹配
filter = 'category LIKE "elec%"'範圍
filter = 'category >= "a" and category < "m"'範圍評估可能會遍歷多個已建立索引的字串。
後綴、子字串以及一般的LIKE 模式亦可針對索引字串進行評估,但可能需要遍歷許多不同的值。若您的工作負載主要以子字串或正規表達式比對為主,請考慮改用FMINDEX或NGRAM索引。
刪除索引
請使用 `drop_index() ` 方法從集合中移除現有索引。
在v2.6.3或更早版本中,您必須先釋放集合,才能刪除標量索引。
自v2.6.4或更新版本起,當標量索引不再需要時,可直接刪除該索引——無需先釋放集合。
client.drop_index(
collection_name="product_catalog", # Name of the collection
index_name="category_trie" # Name of the index to drop
)
import io.milvus.v2.client.ConnectConfig;
import io.milvus.v2.client.MilvusClientV2;
import io.milvus.v2.service.index.request.DropIndexReq;
MilvusClientV2 client = new MilvusClientV2(ConnectConfig.builder()
.uri("http://localhost:19530")
.build());
client.dropIndex(DropIndexReq.builder()
.collectionName("product_catalog")
.indexName("category_trie")
.build());
import (
"context"
"log"
"github.com/milvus-io/milvus/client/v3/milvusclient"
)
ctx := context.Background()
cli, err := milvusclient.New(ctx, &milvusclient.ClientConfig{
Address: "http://localhost:19530",
})
if err != nil {
log.Fatal("failed to connect to milvus server: ", err.Error())
}
err = cli.DropIndex(ctx, milvusclient.NewDropIndexOption("product_catalog", "category_trie"))
if err != nil {
log.Fatal("failed to drop index: ", err.Error())
}
use milvus::v2::prelude::*;
let config = ConnectConfig::new().uri("http://localhost:19530");
let client = ClientV2::new(&config).await?;
client
.drop_index(
DropIndexRequest::builder()
.collection_name("product_catalog")
.index_name("category_trie")
.build()?,
)
.await?;
#include <iostream>
#include "milvus/MilvusClientV2.h"
auto client = milvus::MilvusClientV2::Create();
auto status = client->Connect(milvus::ConnectParam("http://localhost:19530"));
if (!status.IsOk()) {
std::cout << status.Message() << std::endl;
}
status = client->DropIndex(milvus::DropIndexRequest()
.WithCollectionName("product_catalog")
.WithIndexName("category_trie"));
if (!status.IsOk()) {
std::cout << status.Message() << std::endl;
}
import { MilvusClient } from "@zilliz/milvus2-sdk-node";
const client = new MilvusClient({ address: "http://localhost:19530" });
await client.dropIndex({
collection_name: "product_catalog",
index_name: "category_trie",
});
export CLUSTER_ENDPOINT="http://localhost:19530"
export TOKEN="root:Milvus"
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/indexes/drop" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
--header "Request-Timeout: 10" \
-d '{
"collectionName": "product_catalog",
"indexName": "category_trie"
}'
# {
# "code": 0,
# "data": {}
# }
使用說明
欄位類型:僅適用於
VARCHAR欄位。有關資料類型的更多資訊,請參閱「字串欄位」。參數:無需任何索引參數。
模式匹配:對於相等性與前綴篩選,
TRIE的執行速度最快。範圍篩選雖受支援,但可能會遍歷多個已建立索引的字串。後綴、子字串以及一般LIKE模式皆可進行評估,但可能需要掃描大量已建立索引的字串。