Skip to content

Commit 66f6ee5

Browse files
authored
Merge branch 'main' into feat-add-max_rel_per_label
2 parents f61e8ed + 7a5b0bf commit 66f6ee5

5 files changed

Lines changed: 27 additions & 9 deletions

File tree

docarray/array/storage/milvus/backend.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from docarray import Document, DocumentArray
1919
from docarray.array.storage.base.backend import BaseBackendMixin, TypeMap
2020
from docarray.helper import dataclass_from_dict, _safe_cast_int
21+
from docarray.score import NamedScore
2122

2223
if TYPE_CHECKING:
2324
from docarray.typing import (
@@ -266,16 +267,15 @@ def _docs_from_query_response(response):
266267
return DocumentArray([Document.from_base64(d['serialized']) for d in response])
267268

268269
@staticmethod
269-
def _docs_from_search_response(
270-
responses,
271-
) -> 'List[DocumentArray]':
270+
def _docs_from_search_response(responses, distance: str) -> 'List[DocumentArray]':
272271
das = []
273272
for r in responses:
274-
das.append(
275-
DocumentArray(
276-
[Document.from_base64(hit.entity.get('serialized')) for hit in r]
277-
)
278-
)
273+
da = []
274+
for hit in r:
275+
doc = Document.from_base64(hit.entity.get('serialized'))
276+
doc.scores[distance] = NamedScore(value=hit.score)
277+
da.append(doc)
278+
das.append(DocumentArray(da))
279279
return das
280280

281281
def _update_kwargs_from_config(self, field_to_update, **kwargs):

docarray/array/storage/milvus/find.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def _find(
4242
output_fields=['serialized'],
4343
**kwargs,
4444
)
45-
return self._docs_from_search_response(results)
45+
return self._docs_from_search_response(results, distance=self._config.distance)
4646

4747
def _filter(self, filter, limit=10, **kwargs):
4848
kwargs = self._update_kwargs_from_config('consistency_level', **kwargs)

docs/advanced/document-store/extend.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ class FindMixin:
289289
...
290290
```
291291

292+
Make sure to store the distance scores in the `.scores` dictionary of the Documents that are being returned with the `distance` value as key.
292293

293294
## Step 6: summarize everything in `__init__.py`.
294295

docs/advanced/document-store/milvus.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,21 @@ Embeddings Nearest Neighbours with "price" at most 7:
253253
embedding=[5. 5. 5.], price=5
254254
embedding=[4. 4. 4.], price=4
255255
```
256+
257+
You can access the scores as follows:
258+
259+
````python
260+
for doc in results:
261+
print(f"score = {doc.scores[distance].value}")
262+
````
263+
264+
```
265+
score = 3.0
266+
score = 12.0
267+
score = 27.0
268+
score = 48.0
269+
```
270+
256271
### Example of `.find` with only a filter
257272

258273
The following example shows how to use DocArray with Milvus Document Store in order to filter text documents.

tests/unit/array/storage/milvus/test_milvus.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ def test_memory_cntxt_mngr(start_storage):
7373
@pytest.fixture()
7474
def mock_response():
7575
class MockHit:
76+
score = 1.0
77+
7678
@property
7779
def entity(self):
7880
return {'serialized': Document().to_base64()}

0 commit comments

Comments
 (0)