🚀 免費嘗試 Zilliz Cloud,完全托管的 Milvus,體驗速度提升 10 倍!立即嘗試

milvus-logo
LFAI
主頁
  • 使用者指南
  • Home
  • Docs
  • 使用者指南

  • 索引

  • 標量索引

  • 索引標量欄位

索引標量欄位

在 Milvus 中,標量索引用來加速以特定的非向量欄位值進行元篩選,類似於傳統的資料庫索引。本指南將教您如何為整數、字串等欄位建立及設定標量索引。

標量索引的類型

  • 自動索引:Milvus 根據標量字段的資料類型自動決定索引類型。這適用於不需要控制特定索引類型的情況。

  • 自訂索引:您可以指定確切的索引類型,例如反向索引或位圖索引。這提供對索引類型選擇的更多控制。

自動索引

若要使用自動索引,請省略以下的index_type參數 add_index()中省略 index_type 參數,這樣 Milvus 可以根據標量值欄位類型推斷索引類型。

要使用自動索引,請省略在 IndexParam中省略 indexType 參數,這樣 Milvus 可以根據標量值欄位類型推斷索引類型。

若要使用自動索引,請省略...中的index_type參數。 createIndex()中省略 index_type 參數,這樣 Milvus 就可以根據標量值欄位類型推斷索引類型。

標量資料類型與預設索引演算法之間的對應關係,請參考 標量欄位索引演算法

# Auto indexing
client = MilvusClient(
    uri="http://localhost:19530"
)

index_params = MilvusClient.prepare_index_params() # Prepare an empty IndexParams object, without having to specify any index parameters

index_params.add_index(
    field_name="scalar_1", # Name of the scalar field to be indexed
    index_type="", # Type of index to be created. For auto indexing, leave it empty or omit this parameter.
    index_name="default_index" # Name of the index to be created
)

client.create_index(
  collection_name="test_scalar_index", # Specify the collection name
  index_params=index_params
)
import io.milvus.v2.common.IndexParam;
import io.milvus.v2.service.index.request.CreateIndexReq;

IndexParam indexParamForScalarField = IndexParam.builder()
    .fieldName("scalar_1") // Name of the scalar field to be indexed
    .indexName("default_index") // Name of the index to be created
    .indexType("") // Type of index to be created. For auto indexing, leave it empty or omit this parameter.
    .build();

List<IndexParam> indexParams = new ArrayList<>();
indexParams.add(indexParamForVectorField);

CreateIndexReq createIndexReq = CreateIndexReq.builder()
    .collectionName("test_scalar_index") // Specify the collection name
    .indexParams(indexParams)
    .build();

client.createIndex(createIndexReq);
await client.createIndex({
    collection_name: "test_scalar_index", // Specify the collection name
    field_name: "scalar_1", // Name of the scalar field to be indexed
    index_name: "default_index", // Name of the index to be created
    index_type: "" // Type of index to be created. For auto indexing, leave it empty or omit this parameter.
})

自訂索引

若要使用自訂索引,請在 add_index().

若要使用自訂索引,請使用.NET 的indexType參數指定特定的索引類型。 IndexParam.

要使用自訂索引,請使用.NET Framework中的index_type參數指定特定的索引類型。 createIndex().

下面的範例為標量欄位建立了一個倒轉索引scalar_2

index_params = MilvusClient.prepare_index_params() #  Prepare an IndexParams object

index_params.add_index(
    field_name="scalar_2", # Name of the scalar field to be indexed
    index_type="INVERTED", # Type of index to be created
    index_name="inverted_index" # Name of the index to be created
)

client.create_index(
  collection_name="test_scalar_index", # Specify the collection name
  index_params=index_params
)
import io.milvus.v2.common.IndexParam;
import io.milvus.v2.service.index.request.CreateIndexReq;

IndexParam indexParamForScalarField = IndexParam.builder()
    .fieldName("scalar_1") // Name of the scalar field to be indexed
    .indexName("inverted_index") // Name of the index to be created
    .indexType("INVERTED") // Type of index to be created
    .build();

List<IndexParam> indexParams = new ArrayList<>();
indexParams.add(indexParamForVectorField);

CreateIndexReq createIndexReq = CreateIndexReq.builder()
    .collectionName("test_scalar_index") // Specify the collection name
    .indexParams(indexParams)
    .build();

client.createIndex(createIndexReq);
await client.createIndex({
    collection_name: "test_scalar_index", // Specify the collection name
    field_name: "scalar_1", // Name of the scalar field to be indexed
    index_name: "inverted_index", // Name of the index to be created
    index_type: "INVERTED" // Type of index to be created
})

方法與參數

  • prepare_index_params()

    準備一個IndexParams物件。

  • add_index()

    新增索引配置到IndexParams物件。

    • field_name(string)

      要建立索引的標量欄位名稱。

    • index_type(字串):

      要建立的標量索引類型。對於隱含索引,請留空或省略此參數。

      對於自訂索引,有效值為

      • INVERTED: (建議) 反向索引由包含所有按字母順序排序的標記化字詞的詞彙字典組成。如需詳細資訊,請參閱標量索引

      • STL_SORT:使用標準模板函式庫排序演算法對標量字段排序。僅支援數值欄位 (例如:INT8、INT16、INT32、INT64、FLOAT、DOUBLE)。

      • Trie:樹狀資料結構,用於快速前綴搜尋和檢索。支援 VARCHAR 欄位。

    • index_name(字串)

      要建立的標量索引名稱。每個標量欄位支援一個索引。

  • create_index()

    在指定的集合中建立索引。

    • collection_name(字串)

      要建立索引的集合名稱。

    • index_params

      包含索引設定的IndexParams物件。

方法與參數

  • IndexParam準備一個 IndexParam 物件。
    • fieldName(String) 要建立索引的標量欄位名稱。
    • indexName(字串) 要建立的標量索引名稱。每個標量欄位支援一個索引。
    • indexType(字串) 要建立的標量索引類型。對於隱含索引,請將它留空或省略此參數。 對於自訂索引,有效值為:
      • INVERTED: (建議) 反向索引由包含按字母順序排序的所有標記化字詞的詞彙字典組成。如需詳細資訊,請參閱標量索引
      • STL_SORT:使用標準模板函式庫排序演算法對標量字段排序。支援布林欄位和數值欄位 (例如:INT8、INT16、INT32、INT64、FLOAT、DOUBLE)。
      • Trie:一種樹狀資料結構,用於快速前綴搜尋和檢索。支援 VARCHAR 欄位。
  • CreateIndexReq在指定的集合中建立索引。
    • collectionName(String) 建立索引的集合名稱。
    • indexParams(List) 包含索引配置的 IndexParam 物件清單。

方法和參數

  • 建立索引

    在指定的集合中建立索引。

    • collection_name(string) 建立索引的集合名稱。
    • field_name(字串) 要建立索引的標量欄位名稱。
    • index_name(string) 要建立的標量索引的名稱。每個標量欄位支援一個索引。
    • index_type(string) 要建立的標量索引類型。對於隱含索引,請將它留空或省略此參數。 對於自訂索引,有效值為:
      • INVERTED: (建議) 反向索引由包含所有按字母順序排序的標記化字詞的詞彙字典組成。如需詳細資訊,請參閱標量索引
      • STL_SORT:使用標準模板函式庫排序演算法對標量字段排序。支援布林欄位和數值欄位 (例如:INT8、INT16、INT32、INT64、FLOAT、DOUBLE)。
      • Trie:一種樹狀資料結構,用於快速前綴搜尋和檢索。支援 VARCHAR 欄位。

驗證結果

使用 list_indexes()方法驗證標量索引的建立:

使用listIndexes() 方法驗證標量索引的建立:

使用listIndexes() 方法驗證標量索引的建立:

client.list_indexes(
    collection_name="test_scalar_index"  # Specify the collection name
)

# Output:
# ['default_index','inverted_index']
import java.util.List;
import io.milvus.v2.service.index.request.ListIndexesReq;

ListIndexesReq listIndexesReq = ListIndexesReq.builder()
    .collectionName("test_scalar_index")  // Specify the collection name
    .build();

List<String> indexNames = client.listIndexes(listIndexesReq);

System.out.println(indexNames);

// Output:
// [
//     "default_index",
//     "inverted_index"
// ]
res = await client.listIndexes({
    collection_name: 'test_scalar_index'
})

console.log(res.indexes)

// Output:
// [
//     "default_index",
//     "inverted_index"
// ]   

限制

  • 目前,標量索引支援 INT8、INT16、INT32、INT64、FLOAT、DOUBLE、BOOL、VARCHAR 及 ARRAY 資料類型,但不支援 JSON 資料類型。

免費嘗試托管的 Milvus

Zilliz Cloud 無縫接入,由 Milvus 提供動力,速度提升 10 倍。

開始使用
反饋

這個頁面有幫助嗎?