Skip to content

Commit e5d1bc3

Browse files
authored
Merge pull request #1097 from MuddyHope/remote_fedarated_search
Add Or Update Networks
2 parents 39f8263 + bd9f808 commit e5d1bc3

File tree

6 files changed

+124
-1
lines changed

6 files changed

+124
-1
lines changed

meilisearch/client.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,8 @@ def multi_search(
233233
queries:
234234
List of dictionaries containing the specified indexes and their search queries
235235
https://www.meilisearch.com/docs/reference/api/search#search-in-an-index
236+
It can also include remote options in federationOptions for each query
237+
https://www.meilisearch.com/docs/reference/api/network
236238
federation: (optional):
237239
Dictionary containing offset and limit
238240
https://www.meilisearch.com/docs/reference/api/multi_search
@@ -756,6 +758,43 @@ def generate_tenant_token(
756758

757759
return jwt_token
758760

761+
def add_or_update_networks(self, body: Union[MutableMapping[str, Any], None]) -> Dict[str, str]:
762+
"""Set all the Remote Networks
763+
764+
Parameters
765+
----------
766+
body:
767+
Remote networks that are allowed
768+
769+
Returns
770+
-------
771+
remote networks:
772+
Remote Networks containing information about the networks allowed/present.
773+
https://www.meilisearch.com/docs/reference/api/network
774+
775+
Raises
776+
------
777+
MeilisearchApiError
778+
An error containing details about why Meilisearch can't process your request. Meilisearch error codes are described here: https://www.meilisearch.com/docs/reference/errors/error_codes#meilisearch-errors
779+
"""
780+
781+
return self.http.patch(path=f"{self.config.paths.network}", body=body)
782+
783+
def get_all_networks(self) -> Dict[str, str]:
784+
"""Fetches all the remote-networks present
785+
786+
Returns
787+
-------
788+
remote networks:
789+
All remote networks containing information about each remote and their respective remote-name and searchApi key
790+
791+
Raises
792+
------
793+
MeilisearchApiError
794+
An error containing details about why Meilisearch can't process your request. Meilisearch error codes are described here: https://www.meilisearch.com/docs/reference/errors/error_codes#meilisearch-errors
795+
"""
796+
return self.http.get(path=f"{self.config.paths.network}")
797+
759798
@staticmethod
760799
def _base64url_encode(data: bytes) -> str:
761800
return base64.urlsafe_b64encode(data).decode("utf-8").replace("=", "")

meilisearch/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class Paths:
4646
proximity_precision = "proximity-precision"
4747
localized_attributes = "localized-attributes"
4848
edit = "edit"
49+
network = "network"
4950

5051
def __init__(
5152
self,

tests/client/test_client_multi_search_meilisearch.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pytest
22

33
from meilisearch.errors import MeilisearchApiError
4-
from tests.common import INDEX_UID
4+
from tests.common import INDEX_UID, REMOTE_MS_1, REMOTE_MS_2
55

66

77
def test_basic_multi_search(client, empty_index):
@@ -75,3 +75,36 @@ def test_multi_search_with_federation_options(client, index_with_documents):
7575
assert response["hits"][0]["_federation"]["weightedRankingScore"] >= 0.99
7676
assert response["limit"] == 2
7777
assert response["offset"] == 0
78+
79+
80+
@pytest.mark.usefixtures("enable_network_options")
81+
def test_multi_search_with_network(client, index_with_documents):
82+
"""Tests multi-search with network, with federation options."""
83+
index_with_documents()
84+
resp = client.add_or_update_networks(
85+
{
86+
"self": REMOTE_MS_1,
87+
"remotes": {
88+
REMOTE_MS_1: {
89+
"url": "http://ms-1235.example.meilisearch.io",
90+
"searchApiKey": "xxxxxxxx",
91+
},
92+
REMOTE_MS_2: {
93+
"url": "http://ms-1255.example.meilisearch.io",
94+
"searchApiKey": "xxxxxxxx",
95+
},
96+
},
97+
}
98+
)
99+
response = client.multi_search(
100+
[{"indexUid": INDEX_UID, "q": "", "federationOptions": {"remote": REMOTE_MS_1}}],
101+
federation={},
102+
)
103+
104+
assert "results" not in resp
105+
assert "results" not in response
106+
assert isinstance(response["hits"], list)
107+
assert len(response["hits"]) >= 0
108+
assert response["hits"][0]["_federation"]["indexUid"] == INDEX_UID
109+
assert response["hits"][0]["_federation"]["remote"] == REMOTE_MS_1
110+
assert response["remoteErrors"] == {}

tests/client/test_client_network.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import pytest
2+
3+
from tests.common import REMOTE_MS_1, REMOTE_MS_2
4+
5+
6+
@pytest.mark.usefixtures("enable_network_options")
7+
def test_get_all_networks(client):
8+
"""Tests get all network in MS"""
9+
response = client.get_all_networks()
10+
11+
assert isinstance(response, dict)
12+
13+
14+
@pytest.mark.usefixtures("enable_network_options")
15+
def test_add_or_update_networks(client):
16+
"""Tests upsert network remote instance."""
17+
body = {
18+
"self": REMOTE_MS_1,
19+
"remotes": {
20+
REMOTE_MS_1: {"url": "http://localhost:7700", "searchApiKey": "xxxxxxxxxxxxxx"},
21+
REMOTE_MS_2: {"url": "http://localhost:7720", "searchApiKey": "xxxxxxxxxxxxxxx"},
22+
},
23+
}
24+
response = client.add_or_update_networks(body=body)
25+
26+
assert isinstance(response, dict)
27+
assert response["self"] == REMOTE_MS_1
28+
assert len(response["remotes"]) >= 2
29+
assert REMOTE_MS_2 in response["remotes"]
30+
assert REMOTE_MS_1 in response["remotes"]

tests/common.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@
1313
{"uid": INDEX_UID2, "options": {"primaryKey": "book_id"}},
1414
{"uid": INDEX_UID3, "options": {"uid": "wrong", "primaryKey": "book_id"}},
1515
]
16+
17+
REMOTE_MS_1 = "ms_001"
18+
REMOTE_MS_2 = "ms_002"

tests/conftest.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,3 +291,20 @@ def enable_composite_embedders():
291291
json={"compositeEmbedders": False},
292292
timeout=10,
293293
)
294+
295+
296+
@fixture
297+
def enable_network_options():
298+
requests.patch(
299+
f"{common.BASE_URL}/experimental-features",
300+
headers={"Authorization": f"Bearer {common.MASTER_KEY}"},
301+
json={"network": True},
302+
timeout=10,
303+
)
304+
yield
305+
requests.patch(
306+
f"{common.BASE_URL}/experimental-features",
307+
headers={"Authorization": f"Bearer {common.MASTER_KEY}"},
308+
json={"network": False},
309+
timeout=10,
310+
)

0 commit comments

Comments
 (0)