Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add data cli and tests #43

Merged
merged 1 commit into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions milvus_cli/Cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from Database import MilvusDatabase
from Collection import MilvusCollection
from Index import MilvusIndex
from Data import MilvusData
from pymilvus import __version__
from Types import ParameterException

Expand All @@ -23,3 +24,4 @@ class MilvusCli(object):
database = MilvusDatabase()
collection = MilvusCollection()
index = MilvusIndex()
data = MilvusData()
57 changes: 57 additions & 0 deletions milvus_cli/Data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from Collection import getTargetCollection
from tabulate import tabulate


class MilvusData:
alias = "default"

def insert(
self, collectionName, data, partitionName=None, alias=None, timeout=None
):
temaAlias = alias if alias else self.alias
collection = getTargetCollection(collectionName, temaAlias)
result = collection.insert(data, partition_name=partitionName, timeout=timeout)
return result

def query(self, collectionName, queryParameters, alias=None):
temaAlias = alias if alias else self.alias
collection = getTargetCollection(collectionName, temaAlias)
res = collection.query(**queryParameters)
return res

def delete_entities(
self,
expr,
collectionName,
partition_name=None,
alias=None,
):
temaAlias = alias if alias else self.alias
collection = getTargetCollection(collectionName, temaAlias)
result = collection.delete(expr, partition_name=partition_name)
return result

def search(self, collectionName, searchParameters, alias=None, prettierFormat=True):
tempAlias = alias if alias else self.alias
collection = getTargetCollection(collectionName, tempAlias)
res = collection.search(**searchParameters)
if not prettierFormat:
return res
hits = res[0]
results = []
for hits in res:
results += [
tabulate(
map(lambda x: [x.id, x.distance, x.score], hits),
headers=["Index", "ID", "Distance", "Score"],
tablefmt="grid",
showindex=True,
)
]
return tabulate(
map(lambda x: [x.id, x.distance], hits),
headers=["Index", "ID", "Distance"],
tablefmt="grid",
showindex=True,
)
# return res
28 changes: 22 additions & 6 deletions milvus_cli/Index.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def create_index(
indexType,
metricType,
params,
alias,
alias=None,
):
try:
tempAlias = alias if alias else self.alias
Expand Down Expand Up @@ -44,7 +44,7 @@ def create_index(
except Exception as e:
raise Exception(f"Create index error!{str(e)}")

def get_index_details(self, collectionName, indexName, alias):
def get_index_details(self, collectionName, indexName, alias=None):
tempAlias = alias if alias else self.alias
collection = getTargetCollection(collectionName, tempAlias)

Expand All @@ -62,22 +62,22 @@ def get_index_details(self, collectionName, indexName, alias):
rows.append(["Params", paramsDetails])
return tabulate(rows, tablefmt="grid")

def drop_index(self, collectionName, indexName, alias, timeout=None):
def drop_index(self, collectionName, indexName, alias=None, timeout=None):
tempAlias = alias if alias else self.alias
collection = getTargetCollection(collectionName, tempAlias)
collection.drop_index(index_name=indexName, timeout=timeout)
return self.list_indexes(collectionName, tempAlias)

def has_index(self, collectionName, indexName, alias, timeout=None):
def has_index(self, collectionName, indexName, alias=None, timeout=None):
tempAlias = alias if alias else self.alias
collection = getTargetCollection(collectionName, tempAlias)
return collection.has_index(index_name=indexName, timeout=timeout)

def get_index_build_progress(self, collectionName, indexName, alias):
def get_index_build_progress(self, collectionName, indexName, alias=None):
tempAlias = alias if alias else self.alias
return index_building_progress(collectionName, indexName, tempAlias)

def list_indexes(self, collectionName, alias):
def list_indexes(self, collectionName, alias=None):
tempAlias = alias if alias else self.alias
target = getTargetCollection(collectionName, tempAlias)
result = target.indexes
Expand All @@ -100,3 +100,19 @@ def list_indexes(self, collectionName, alias):
tablefmt="grid",
showindex=True,
)

def get_vector_index(self, collectionName):
target = getTargetCollection(collectionName, self.alias)
try:
result = target.index()
except Exception as e:
return {}
else:
details = {
"field_name": result.field_name,
"index_type": result.params["index_type"],
"metric_type": result.params["metric_type"],
"params": result.params["params"],
}

return details
Loading