日本熟妇hd丰满老熟妇,中文字幕一区二区三区在线不卡 ,亚洲成片在线观看,免费女同在线一区二区

快速實現(xiàn)向量檢索

更新時間:

向量檢索是實現(xiàn)高效相似性搜索的關鍵技術。本文通過詳細示例為您介紹如何快速實現(xiàn)向量檢索。

前提條件

  • 已在本地客戶端成功安裝了PyMilvus庫,并將其更新至當前最新版本。

    如果您尚未在本地客戶端安裝PyMilvus庫,或者需要將其更新至當前最新版本,您可以執(zhí)行以下命令。

    pip install --upgrade pymilvus
  • 已創(chuàng)建Milvus實例,請參見詳情快速創(chuàng)建Milvus實例

注意事項

阿里云Milvus支持通過內(nèi)網(wǎng)和公網(wǎng)的方式連接,在連接Milvus實例之前,需確保您的客戶端具備適當?shù)木W(wǎng)絡訪問權限,詳情請參見網(wǎng)絡訪問與安全設置

操作流程

步驟一:連接Milvus實例

您可以使用以下代碼連接Milvus實例。

from pymilvus import MilvusClient

# 創(chuàng)建Milvus Client。
client = MilvusClient(
    uri="http://c-xxxx.milvus.aliyuncs.com:19530",  # Milvus實例的公網(wǎng)地址。
    token="<yourUsername>:<yourPassword>",  # 登錄Milvus實例的用戶名和密碼。
    db_name="default"  # 待連接的數(shù)據(jù)庫名稱,本文示例為默認的default。
)

步驟二:創(chuàng)建Collection

您可以通過以下代碼簡便地創(chuàng)建一個Collection。更多自定義參數(shù)選項,請參見管理Collections

client.create_collection(
    collection_name="demo",  #集合的名稱。
    dimension=5  #向量維度。
)

這段代碼除了設置Collection名稱和向量維度,還自動應用了以下配置:

  • 使用默認命名的主鍵字段“id”和向量字段“vector”。

  • “metric_type”屬性采用默認的COSINE度量類型。

  • 主鍵字段“id”設定為整型,其值不會自動遞增。

  • 引入了額外的“$meta”字段,以鍵值對形式存儲那些在Schema中未定義的字段數(shù)據(jù)。

步驟三:插入數(shù)據(jù)

Collection創(chuàng)建完畢后,系統(tǒng)會自動將其及其對應的索引加載至內(nèi)存中,您可以使用如下代碼向該集合中插入測試數(shù)據(jù)。

插入少量數(shù)據(jù)

這段代碼插入了預定義的10個Entity,每個Entity具有固定的向量和顏色標簽。

data=[{'id': 0, 'vector': [-0.493313706583155, -0.172001225836391, 0.16825615330139554, -0.0198911518739604, -0.9756816265213708], 'color': 'green_5760'}, {'id': 1, 'vector': [0.6695699219225086, 0.49952523907354496, -0.49870548178008534, 0.8824655547230731, -0.7182693622931615], 'color': 'blue_2330'}, {'id': 2, 'vector': [-0.6057771959702387, 0.9141473782193543, 0.32053983678483466, -0.32126010092015655, 0.725222856037071], 'color': 'grey_9673'}, {'id': 3, 'vector': [0.14082089434165868, 0.9924029949938447, 0.7943279666144052, -0.7898608705081103, -0.9941425813199956], 'color': 'white_2829'}, {'id': 4, 'vector': [-0.46180540826224026, 0.33216876051895783, 0.5786699695956004, 0.8891120357625131, 0.04872530176990697], 'color': 'pink_9061'}, {'id': 5, 'vector': [-0.6097452740606673, 0.35648319550551144, -0.5699789153006387, 0.15085357921088316, -0.8817226997144627], 'color': 'pink_8525'}, {'id': 6, 'vector': [0.7843522543512762, -0.7663837586858071, -0.8681839054724569, 0.6880645348647785, -0.5151293183261791], 'color': 'green_5016'}, {'id': 7, 'vector': [-0.9967116931989293, 0.5741923070732655, -0.019126124261334976, -0.34163875885482753, -0.8189843931354175], 'color': 'brown_7434'}, {'id': 8, 'vector': [0.7347243385915765, -0.7358853080124825, -0.23737428377511716, 0.06980552357261627, -0.30613964550461437], 'color': 'blue_5059'}, {'id': 9, 'vector': [-0.21187155428455862, -0.3288541717216129, -0.32564136453418824, -0.14054963599686743, 0.5491320339870627], 'color': 'yellow_9887'}]

res = client.insert(
    collection_name="demo",
    data=data
)

插入更多數(shù)據(jù)

這段代碼使用列表推導式動態(tài)生成了大量的Entity,這些Entity的向量和顏色標簽都是在指定范圍內(nèi)隨機生成的。

import random

colors = ["green", "blue", "yellow", "red", "black", "white", "purple", "pink", "orange", "brown", "grey"]
data = [ {
    "id": i, 
    "vector": [ random.uniform(-1, 1) for _ in range(5) ], 
    "color": f"{random.choice(colors)}_{str(random.randint(1000, 9999))}" 
} for i in range(1000) ]

res = client.insert(
    collection_name="demo",
    data=data[1:]
)

print(res)

步驟四:向量檢索

重要

數(shù)據(jù)插入過程采用異步機制,這意味著在您完成數(shù)據(jù)插入操作后,系統(tǒng)并不會立即更新相應的搜索索引。為確保查詢到最新插入的數(shù)據(jù),建議您在數(shù)據(jù)插入后耐心等待數(shù)秒,待索引更新完成后再進行搜索操作

單一向量檢索

通過提供一個查詢向量列表,您可以進行單一向量的相似性檢索。

query_vectors = [
[-0.8832567462711804, -0.2999882617491647, 0.9921295273224382, -0.272575369985379, -0.688914679645338]
]

res = client.search(
    collection_name="demo",     # 查詢collection
    data=query_vectors,                # 查詢vectors。
    limit=3,                           # 返回entities數(shù)量。
)

print(res)

批量向量檢索

對于多個查詢向量的批量檢索,只需將向量列表作為輸入?yún)?shù)即可。代碼示例如下。

query_vectors = [
[0.06586461994037252, 0.7693023529849932, 0.8199991781350795, -0.6988017611187176, 0.408383847889378],
[0.8988257992203861, 0.021911711196309414, 0.19086900086430836, 0.63590610476426, -0.6713237387993141]
]

res = client.search(
    collection_name="demo",
    data=query_vectors,
    limit=3,
)

print(res)

步驟五:Filter檢索

利用Schema中定義的字段,您可以設置過濾條件來精確限定檢索范圍,提高搜索效率。

基于數(shù)值字段過濾

以下示例展示了如何根據(jù)id字段的數(shù)值范圍進行過濾。

query_vectors = [
[-0.30932351869632435, -0.7132856078639205, 0.6006201320181415, 0.40140510356426784, -0.21223937444001328]
]

res = client.search(
    collection_name="demo",
    data=query_vectors,
    filter="3 < id < 5",  # 數(shù)值字段范圍過濾條件。
    limit=3
)

print(res)

基于元數(shù)據(jù)字段($meta)過濾

以下示例展示了如何依據(jù)color屬性值以“green”開頭的記錄進行檢索,并指定輸出包含color字段的檢索結果。

query_vectors = [
[0.9636568288732006, -0.5900490884830603, 0.2504591754023724, 0.7120903924474389, 0.7620604497390009]
]

res = client.search(
    collection_name="demo",
    data=query_vectors,
    filter='$meta["color"] like "green%"',  # 元數(shù)據(jù)字段屬性值匹配過濾條件。
    limit=3,
    output_fields=["color"]   # 指定返回結果中包含的字段
)

print(res)