怎么获取milvus的链接
时间: 2024-04-20 13:18:10 浏览: 7
要获取Milvus的链接,需要先安装并启动Milvus服务器。在启动Milvus服务器后,可以通过Milvus Python SDK或Milvus RESTful API来连接Milvus服务器。
使用Python SDK连接Milvus服务器的示例代码如下:
```python
from milvus import Milvus, IndexType, MetricType
milvus = Milvus(host='localhost', port='19530')
# 创建一个collection
collection_name = 'example_collection'
milvus.create_collection(collection_name, {'fields': [
{'name': 'embedding', 'type': 'float_vector', 'params': {'dim': 128}}
]})
# 插入向量
vectors = [[0.1] * 128, [0.2] * 128]
milvus.insert(collection_name, vectors)
# 创建索引
index_param = {'index_type': IndexType.IVF_FLAT, 'metric_type': MetricType.L2}
milvus.create_index(collection_name, index_param)
# 查询向量
query_vector = [[0.1] * 128]
results = milvus.search(collection_name, query_vector, top_k=10)
# 删除collection
milvus.drop_collection(collection_name)
# 断开连接
milvus.close()
```
使用RESTful API连接Milvus服务器的示例代码如下:
```python
import requests
# 创建一个collection
collection_name = 'example_collection'
url = 'http://localhost:19121/collections'
data = {
'collection_name': collection_name,
'fields': [
{'name': 'embedding', 'type': 'float_vector', 'params': {'dim': 128}}
]
}
response = requests.post(url, json=data)
print(response.json())
# 插入向量
url = f'http://localhost:19121/collections/{collection_name}/vectors'
data = {'vectors': [[0.1] * 128, [0.2] * 128]}
response = requests.post(url, json=data)
print(response.json())
# 创建索引
url = f'http://localhost:19121/collections/{collection_name}/indexes'
data = {'index_type': 'IVF_FLAT', 'metric_type': 'L2'}
response = requests.post(url, json=data)
print(response.json())
# 查询向量
url = f'http://localhost:19121/collections/{collection_name}/search'
data = {'query': {'vector': [0.1] * 128}, 'top_k': 10}
response = requests.post(url, json=data)
print(response.json())
# 删除collection
url = f'http://localhost:19121/collections/{collection_name}'
response = requests.delete(url)
print(response.json())
```