Skip to content
This repository has been archived by the owner on Sep 1, 2021. It is now read-only.

Commit

Permalink
Get value of metadata field value from search result output (#213)
Browse files Browse the repository at this point in the history
[+] Add entity function to Hit object
[+] Add usage in the example
[+] Add changes to CHANGELOG

Signed-off-by: Leonardo1412 <leonardo1412@andesvr.com>

Co-authored-by: Leonardo1412 <leonardo1412@andesvr.com>
  • Loading branch information
leonardokidd and Leonardo1412 authored Jun 30, 2021
1 parent bf3536a commit 01dfea8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# pymilvus 0.0.1(TBD)

## Task
- Get value of metadata fields from search result output (#90)
- Improve interface documentation (#89)
- Add examples in partition.py (#88)
- add example for index.py (#87)
Expand Down
20 changes: 16 additions & 4 deletions examples/hello_milvus.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ def hello_milvus():
dim = 128
default_fields = [
schema.FieldSchema(name="count", dtype=DataType.INT64, is_primary=True),
schema.FieldSchema(name="score", dtype=DataType.DOUBLE),
# Change field schema name to distinguish search result score
schema.FieldSchema(name="random_value", dtype=DataType.DOUBLE),
schema.FieldSchema(name="float_vector", dtype=DataType.FLOAT_VECTOR, dim=dim)
]
default_schema = schema.CollectionSchema(fields=default_fields, description="test collection")
Expand All @@ -40,7 +41,13 @@ def hello_milvus():
import random
nb = 3000
vectors = [[random.random() for _ in range(dim)] for _ in range(nb)]
collection.insert([[i for i in range(nb)], [float(i) for i in range(nb)], vectors])
collection.insert(
[
[i for i in range(nb)],
[float(random.randrange(-20,-10)) for _ in range(nb)],
vectors
]
)

print(f"\nGet collection entities...")
print(collection.num_entities)
Expand All @@ -58,13 +65,18 @@ def hello_milvus():
import time
start_time = time.time()
print(f"\nSearch...")
res = collection.search(vectors[-2:], "float_vector", search_params, topK, "count > 100")
# define output_fields of search result
res = collection.search(
vectors[-2:], "float_vector", search_params, topK,
"count > 100", output_fields=["count", "random_value"]
)
end_time = time.time()

# show result
for hits in res:
for hit in hits:
print(hit)
# Get value of the random value field for search result
print(hit, hit.entity.get("random_value"))
print("search latency = %.4fs" % (end_time - start_time))

# drop collection
Expand Down
11 changes: 11 additions & 0 deletions pymilvus_orm/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# the License.

import abc
from pymilvus.client.abstract import Entity


class _IterableWrapper:
Expand Down Expand Up @@ -99,6 +100,16 @@ def id(self) -> int:
"""
return self._hit.id

@property
def entity(self) -> Entity:
"""
Return the Entity of the hit record.
:return pymilvus Entity object:
The entity content of the hit record.
"""
return self._hit.entity

@property
def distance(self) -> float:
"""
Expand Down

0 comments on commit 01dfea8

Please sign in to comment.