Skip to content

Commit 2c13cdb

Browse files
committed
feat: add page_retain_order param during search with offset
Signed-off-by: Patrick Weizhi Xu <[email protected]>
1 parent 66c2362 commit 2c13cdb

File tree

5 files changed

+32
-0
lines changed

5 files changed

+32
-0
lines changed

pymilvus/client/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
GROUP_BY_FIELD = "group_by_field"
1313
GROUP_SIZE = "group_size"
1414
ITERATOR_FIELD = "iterator"
15+
PAGE_RETAIN_ORDER_FIELD = "page_retain_order"
1516

1617
RANKER_TYPE_RRF = "rrf"
1718
RANKER_TYPE_WEIGHTED = "weighted"

pymilvus/client/prepare.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
GROUP_BY_FIELD,
1818
GROUP_SIZE,
1919
ITERATOR_FIELD,
20+
PAGE_RETAIN_ORDER_FIELD,
2021
REDUCE_STOP_FOR_BEST,
2122
)
2223
from .types import (
@@ -690,6 +691,20 @@ def search_requests_with_expr(
690691
raise ParamError(message=f"wrong type for offset, expect int, got {type(offset)}")
691692
search_params["offset"] = offset
692693

694+
if PAGE_RETAIN_ORDER_FIELD in kwargs and PAGE_RETAIN_ORDER_FIELD in param:
695+
raise ParamError(
696+
message="Provide page_retain_order both in kwargs and param, expect just one"
697+
)
698+
page_retain_order = kwargs.get(PAGE_RETAIN_ORDER_FIELD) or param.get(
699+
PAGE_RETAIN_ORDER_FIELD
700+
)
701+
if page_retain_order is not None:
702+
if not isinstance(page_retain_order, bool):
703+
raise ParamError(
704+
message=f"wrong type for page_retain_order, expect bool, got {type(page_retain_order)}"
705+
)
706+
search_params[PAGE_RETAIN_ORDER_FIELD] = page_retain_order
707+
693708
is_iterator = kwargs.get(ITERATOR_FIELD)
694709
if is_iterator is not None:
695710
search_params[ITERATOR_FIELD] = is_iterator

pymilvus/orm/collection.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,13 +677,16 @@ def search(
677677
similar metricy types, the value must be of type str.
678678
* *offset* (``int``, optional)
679679
offset for pagination.
680+
* *page_retain_order* (``bool``, optional)
681+
Whether to retain the order of the search result when offset is provided.
680682
* *params of index: *nprobe*, *ef*, *search_k*, etc
681683
Corresponding search params for a certain index.
682684
example for param::
683685
684686
{
685687
"metric_type": "L2",
686688
"offset": 10,
689+
"page_retain_order": True,
687690
"params": {"nprobe": 12},
688691
}
689692
@@ -716,6 +719,9 @@ def search(
716719
* *offset* (``int``, optinal)
717720
offset for pagination.
718721
722+
* *page_retain_order* (``bool``, optional)
723+
Whether to retain the order of the search result when offset is provided.
724+
719725
* *consistency_level* (``str/int``, optional)
720726
Which consistency level to use when searching in the collection.
721727

pymilvus/orm/partition.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,9 @@ def search(
387387
* *offset* (``int``, optional)
388388
offset for pagination.
389389
390+
* *page_retain_order* (``bool``, optional)
391+
Whether to retain the order of the search result when offset is provided.
392+
390393
* *limit* (``int``, optional)
391394
limit for the search results and pagination.
392395
@@ -396,6 +399,7 @@ def search(
396399
"nprobe": 128,
397400
"metric_type": "L2",
398401
"offset": 10,
402+
"page_retain_order": True,
399403
"limit": 10,
400404
}
401405

tests/test_prepare.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,23 @@ def test_search_requests_with_expr_offset(self):
2424
search_params = {
2525
"metric_type": "L2",
2626
"offset": 10,
27+
"page_retain_order": True,
2728
}
2829

2930
ret = Prepare.search_requests_with_expr("name", data, "v", search_params, 100)
3031

3132
offset_exists = False
33+
page_retain_order_exists = False
3234
for p in ret.search_params:
3335
if p.key == "offset":
3436
offset_exists = True
3537
assert p.value == "10"
38+
elif p.key == "page_retain_order":
39+
page_retain_order_exists = True
40+
assert p.value == "True" # it was dumped as string
3641

3742
assert offset_exists is True
43+
assert page_retain_order_exists is True
3844

3945

4046
class TestCreateCollectionRequest:

0 commit comments

Comments
 (0)