地方门户类网站,智慧团建官网手机版,外贸谷歌seo,中国网站排名站长之家流程
milvus的使用流程是 创建collection - 创建partition - 创建索引(如果需要检索) - 插入数据 - 检索 这里以Python为例, 使用的milvus版本为2.3.x 首先按照库#xff0c; python3 -m pip install pymilvus
Connect
from pymilvus import connections
c…流程
milvus的使用流程是 创建collection - 创建partition - 创建索引(如果需要检索) - 插入数据 - 检索 这里以Python为例, 使用的milvus版本为2.3.x 首先按照库 python3 -m pip install pymilvus
Connect
from pymilvus import connections
connections.connect(aliasdefault,userusername,passwordpassword,hostlocalhost,port19530
)connections.list_connections()
connections.get_connection_addr(default)connections.disconnect(default)以上是源码可以看出alias只是一个字典的映射的key 通过源码可以看到还有两种连接方式:
在.env文件中添加参数MILVUS_URImilvus://Your_Host:Your_Port之后可以使用connections.connect()连接在一次连接成功后将连接配置数据保存在内存下次近执行connections.connect()即可连接可以通过connections.remove_connection删除连接配置数据
Database
from pymilvus import connections, dbconn connections.connect(host127.0.0.1, port19530)database db.create_database(book)db.using_database(book) # 切换数据库
db.list_database()
db.drop_database(book)Collection
和一些非关系型数据库(MongoDB)类似Collection就是表
# collection
from pymilvus import Collection, CollectionSchema, FieldSchema, DataType, utility## 需要提前创建列的名称、类型等数据并且必须添加一个主键
book_id FieldSchema(namebook_id,dtypeDataType.INT64,is_primaryTrue,
)
book_name FieldSchema(namebook_name,dtypeDataType.VARCHAR,max_length200,# The default value will be used if this field is left empty during data inserts or upserts.# The data type of default_value must be the same as that specified in dtype.default_valueUnknown
)
word_count FieldSchema(nameword_count,dtypeDataType.INT64,# The default value will be used if this field is left empty during data inserts or upserts.# The data type of default_value must be the same as that specified in dtype.default_value9999
)
book_intro FieldSchema(namebook_intro,dtypeDataType.FLOAT_VECTOR,dim2
)
# dim2是向量的维度schema CollectionSchema(fields[book_id, book_name, word_count, book_intro],descriptionTest book search,enable_dynamic_fieldTrue
)collection_name bookcollection Collection(namecollection_name,schemaschema,usingdefault,shards_num2)utility.rename_collection(book, lights4)
utility.has_collection(lights1)
utility.list_collections()
# utility.drop_collection(lights)collection Collection(lights3)
collection.load(replica_number2)
# reduce memory usage
collection.release()Partition
# Create a Partitioncollection Collection(book) # Get an existing collection.
collection.create_partition(novel)Index
milvus的索引决定了搜索所用的算法必须设置好所引才能进行搜索。
# Index
index_params {metric_type:L2,index_type:IVF_FLAT,params:{nlist:1024}
}collection.create_index(field_namebook_intro, index_paramsindex_params
)## metric_type是相似性计算算法可选的有以下
## For floating point vectors:
## L2 (Euclidean distance)
## IP (Inner product)
## COSINE (Cosine similarity)
## For binary vectors:
## JACCARD (Jaccard distance)
## HAMMING (Hamming distance)
utility.index_building_progress(Your_Collection)Data
数据可以从dataFrame来也可以从其他方式获得只要列名对上即可。
import pandas as pd
import numpy as npinsert_data pd.read_csv(Your_File)
mr collection.insert(insert_data)Search
# search
search_params {metric_type: L2, offset: 5, ignore_growing: False, params: {nprobe: 10}
}results collection.search(data[[0.1, 0.2]], anns_fieldbook_intro, # the sum of offset in param and limit # should be less than 16384.paramsearch_params,limit10,exprNone,# 这里需要将想看的列名列举出来output_fields[title],consistency_levelStrong
)# get the IDs of all returned hits
results[0].ids# get the distances to the query vector from all returned hits
results[0].distances# get the value of an output field specified in the search request.
hit results[0][0]
hit.entity.get(title)具体的代码在我的github。希望对你有所帮助