From cce408b07c6a7996cce6a81406cc1f82117e34ee Mon Sep 17 00:00:00 2001 From: MuddyHope Date: Wed, 9 Apr 2025 15:46:07 -0700 Subject: [PATCH 01/12] Add Or Update Networks --- meilisearch/client.py | 22 ++++++++++++++++++++++ meilisearch/config.py | 1 + 2 files changed, 23 insertions(+) diff --git a/meilisearch/client.py b/meilisearch/client.py index e3572d32..3991fe07 100644 --- a/meilisearch/client.py +++ b/meilisearch/client.py @@ -756,6 +756,28 @@ def generate_tenant_token( return jwt_token + def add_or_update_networks(self, body: Union[MutableMapping[str, Any], None]): + """Set all the Remote Networks + + Parameters + ---------- + body: + Remote networks that are allowed + + Returns + ------- + remote networks: + Remote Networks containing information about the networks allowed/present. + https://www.meilisearch.com/docs/reference/api/network + + Raises + ------ + MeilisearchApiError + 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 + """ + response = self.http.patch(path=f"{self.config.paths.network}", body=body) + return response + @staticmethod def _base64url_encode(data: bytes) -> str: return base64.urlsafe_b64encode(data).decode("utf-8").replace("=", "") diff --git a/meilisearch/config.py b/meilisearch/config.py index acf5745b..e0abc601 100644 --- a/meilisearch/config.py +++ b/meilisearch/config.py @@ -45,6 +45,7 @@ class Paths: proximity_precision = "proximity-precision" localized_attributes = "localized-attributes" edit = "edit" + network = "network" def __init__( self, From edd7579a4777d3188b24a9a06ccc2dc22758c3b7 Mon Sep 17 00:00:00 2001 From: MuddyHope Date: Wed, 9 Apr 2025 18:36:28 -0700 Subject: [PATCH 02/12] mypy fixes --- meilisearch/client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/meilisearch/client.py b/meilisearch/client.py index 3991fe07..cb2a5bf8 100644 --- a/meilisearch/client.py +++ b/meilisearch/client.py @@ -756,7 +756,7 @@ def generate_tenant_token( return jwt_token - def add_or_update_networks(self, body: Union[MutableMapping[str, Any], None]): + def add_or_update_networks(self, body: Union[MutableMapping[str, Any], None]) -> HttpRequests: """Set all the Remote Networks Parameters From 1c0c99377c53ac71ba524f5d22a01f617138061c Mon Sep 17 00:00:00 2001 From: MuddyHope Date: Fri, 11 Apr 2025 22:06:01 -0700 Subject: [PATCH 03/12] Adding Test cases for remote-search --- meilisearch/client.py | 23 +++++++++-- .../test_client_multi_search_meilisearch.py | 17 ++++++++- tests/client/test_client_network.py | 38 +++++++++++++++++++ tests/common.py | 3 ++ tests/conftest.py | 17 +++++++++ 5 files changed, 94 insertions(+), 4 deletions(-) create mode 100644 tests/client/test_client_network.py diff --git a/meilisearch/client.py b/meilisearch/client.py index cb2a5bf8..dae8d8eb 100644 --- a/meilisearch/client.py +++ b/meilisearch/client.py @@ -233,6 +233,8 @@ def multi_search( queries: List of dictionaries containing the specified indexes and their search queries https://www.meilisearch.com/docs/reference/api/search#search-in-an-index + It can also include remote options in federationOptions for each query + https://www.meilisearch.com/docs/reference/api/network federation: (optional): Dictionary containing offset and limit https://www.meilisearch.com/docs/reference/api/multi_search @@ -756,7 +758,7 @@ def generate_tenant_token( return jwt_token - def add_or_update_networks(self, body: Union[MutableMapping[str, Any], None]) -> HttpRequests: + def add_or_update_networks(self, body: Union[MutableMapping[str, Any], None]) -> Dict[str, str]: """Set all the Remote Networks Parameters @@ -775,8 +777,23 @@ def add_or_update_networks(self, body: Union[MutableMapping[str, Any], None]) -> MeilisearchApiError 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 """ - response = self.http.patch(path=f"{self.config.paths.network}", body=body) - return response + + return self.http.patch(path=f"{self.config.paths.network}", body=body) + + def get_all_networks(self) -> Dict[str, str]: + """Fetches all the remote-networks present + + Returns + ------- + remote networks: + All remote networks containeing information about each remote and their respective remote-name and searchApi key + + Raises + ------ + MeilisearchApiError + 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 + """ + return self.http.get(path=f"{self.config.paths.network}") @staticmethod def _base64url_encode(data: bytes) -> str: diff --git a/tests/client/test_client_multi_search_meilisearch.py b/tests/client/test_client_multi_search_meilisearch.py index 3d888bf3..2047ea71 100644 --- a/tests/client/test_client_multi_search_meilisearch.py +++ b/tests/client/test_client_multi_search_meilisearch.py @@ -1,7 +1,7 @@ import pytest from meilisearch.errors import MeilisearchApiError -from tests.common import INDEX_UID +from tests.common import INDEX_UID, REMOTE_MS_1 def test_basic_multi_search(client, empty_index): @@ -75,3 +75,18 @@ def test_multi_search_with_federation_options(client, index_with_documents): assert response["hits"][0]["_federation"]["weightedRankingScore"] >= 0.99 assert response["limit"] == 2 assert response["offset"] == 0 + + +def test_multi_search_with_network(client, index_with_documents): + """Tests multi-search with network, with federation options.""" + index_with_documents() + response = client.multi_search( + [{"indexUid": INDEX_UID, "q": "", "federationOptions": {"remote", REMOTE_MS_1}}] + ) + + assert "results" not in response + assert isinstance(response['hits'], list) + assert len(response['hits']) >= 0 + assert response['_federation']['indexUid'] == INDEX_UID + assert response['_federation']['remote'] == REMOTE_MS_1 + assert response['remoteErrors'] == 0 diff --git a/tests/client/test_client_network.py b/tests/client/test_client_network.py new file mode 100644 index 00000000..e935d015 --- /dev/null +++ b/tests/client/test_client_network.py @@ -0,0 +1,38 @@ +import pytest + +from tests.common import REMOTE_MS_1, REMOTE_MS_2 + + +@pytest.mark.usefixtures("enable_network_options") +def test_get_all_networks(client): + """Tests get all network in MS""" + response = client.get_all_networks() + + assert isinstance(response, dict) + + +@pytest.mark.usefixtures("enable_network_options") +def test_add_or_update_networks(client): + """Tests upsert network remote instance.""" + body = { + "self": REMOTE_MS_1, + "remotes": { + REMOTE_MS_1: { + "url": "http://localhost:7700", + "searchApiKey": "xxxxxxxxxxxxxx" + } + , + REMOTE_MS_2: { + "url": "http://localhost:7720", + "searchApiKey": "xxxxxxxxxxxxxxx" + } + } + } + response = client.add_or_update_networks(body=body) + + assert isinstance(response, dict) + assert response['self'] == REMOTE_MS_1 + assert len(response['remotes']) >= 2 + assert REMOTE_MS_2 in response['remotes'] + assert REMOTE_MS_1 in response['remotes'] + diff --git a/tests/common.py b/tests/common.py index 143a0654..253ec760 100644 --- a/tests/common.py +++ b/tests/common.py @@ -13,3 +13,6 @@ {"uid": INDEX_UID2, "options": {"primaryKey": "book_id"}}, {"uid": INDEX_UID3, "options": {"uid": "wrong", "primaryKey": "book_id"}}, ] + +REMOTE_MS_1 = "ms_001" +REMOTE_MS_2 = "ms_002" diff --git a/tests/conftest.py b/tests/conftest.py index 7a814ff7..bd662450 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -274,3 +274,20 @@ def new_embedders(): "default": UserProvidedEmbedder(dimensions=1).model_dump(by_alias=True), "open_ai": OpenAiEmbedder().model_dump(by_alias=True), } + + +@fixture +def enable_network_options(): + requests.patch( + f"{common.BASE_URL}/experimental-features", + headers={"Authorization": f"Bearer {common.MASTER_KEY}"}, + json={"network": True}, + timeout=10 + ) + yield + requests.patch( + f"{common.BASE_URL}/experimental-features", + headers={"Authorization": f"Bearer {common.MASTER_KEY}"}, + json={"network": False}, + timeout=10, + ) From 73e7e6afc5ebafddcba4b327b9e12237d5b4733a Mon Sep 17 00:00:00 2001 From: MuddyHope Date: Fri, 11 Apr 2025 23:45:11 -0700 Subject: [PATCH 04/12] Adding Test cases for remote-search --- .../test_client_multi_search_meilisearch.py | 12 +++++----- tests/client/test_client_network.py | 22 ++++++------------- tests/conftest.py | 2 +- 3 files changed, 14 insertions(+), 22 deletions(-) diff --git a/tests/client/test_client_multi_search_meilisearch.py b/tests/client/test_client_multi_search_meilisearch.py index 2047ea71..dc2df7f5 100644 --- a/tests/client/test_client_multi_search_meilisearch.py +++ b/tests/client/test_client_multi_search_meilisearch.py @@ -81,12 +81,12 @@ def test_multi_search_with_network(client, index_with_documents): """Tests multi-search with network, with federation options.""" index_with_documents() response = client.multi_search( - [{"indexUid": INDEX_UID, "q": "", "federationOptions": {"remote", REMOTE_MS_1}}] + [{"indexUid": INDEX_UID, "q": "", "federationOptions": {"remote": REMOTE_MS_1}}], federation={} ) assert "results" not in response - assert isinstance(response['hits'], list) - assert len(response['hits']) >= 0 - assert response['_federation']['indexUid'] == INDEX_UID - assert response['_federation']['remote'] == REMOTE_MS_1 - assert response['remoteErrors'] == 0 + assert isinstance(response["hits"], list) + assert len(response["hits"]) >= 0 + assert response['hits'][0]["_federation"]["indexUid"] == INDEX_UID + assert response['hits'][0]["_federation"]["remote"] == REMOTE_MS_1 + assert response["remoteErrors"] == {} diff --git a/tests/client/test_client_network.py b/tests/client/test_client_network.py index e935d015..bb6ba73a 100644 --- a/tests/client/test_client_network.py +++ b/tests/client/test_client_network.py @@ -17,22 +17,14 @@ def test_add_or_update_networks(client): body = { "self": REMOTE_MS_1, "remotes": { - REMOTE_MS_1: { - "url": "http://localhost:7700", - "searchApiKey": "xxxxxxxxxxxxxx" - } - , - REMOTE_MS_2: { - "url": "http://localhost:7720", - "searchApiKey": "xxxxxxxxxxxxxxx" - } - } + REMOTE_MS_1: {"url": "http://localhost:7700", "searchApiKey": "xxxxxxxxxxxxxx"}, + REMOTE_MS_2: {"url": "http://localhost:7720", "searchApiKey": "xxxxxxxxxxxxxxx"}, + }, } response = client.add_or_update_networks(body=body) assert isinstance(response, dict) - assert response['self'] == REMOTE_MS_1 - assert len(response['remotes']) >= 2 - assert REMOTE_MS_2 in response['remotes'] - assert REMOTE_MS_1 in response['remotes'] - + assert response["self"] == REMOTE_MS_1 + assert len(response["remotes"]) >= 2 + assert REMOTE_MS_2 in response["remotes"] + assert REMOTE_MS_1 in response["remotes"] diff --git a/tests/conftest.py b/tests/conftest.py index bd662450..36456852 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -282,7 +282,7 @@ def enable_network_options(): f"{common.BASE_URL}/experimental-features", headers={"Authorization": f"Bearer {common.MASTER_KEY}"}, json={"network": True}, - timeout=10 + timeout=10, ) yield requests.patch( From 9c4124cde55bad13927729c63d0fe366816f82b5 Mon Sep 17 00:00:00 2001 From: MuddyHope Date: Sat, 12 Apr 2025 12:56:21 -0700 Subject: [PATCH 05/12] Adding Test cases for remote-search --- tests/client/test_client_multi_search_meilisearch.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/client/test_client_multi_search_meilisearch.py b/tests/client/test_client_multi_search_meilisearch.py index dc2df7f5..9167d62b 100644 --- a/tests/client/test_client_multi_search_meilisearch.py +++ b/tests/client/test_client_multi_search_meilisearch.py @@ -81,12 +81,13 @@ def test_multi_search_with_network(client, index_with_documents): """Tests multi-search with network, with federation options.""" index_with_documents() response = client.multi_search( - [{"indexUid": INDEX_UID, "q": "", "federationOptions": {"remote": REMOTE_MS_1}}], federation={} + [{"indexUid": INDEX_UID, "q": "", "federationOptions": {"remote": REMOTE_MS_1}}], + federation={}, ) assert "results" not in response assert isinstance(response["hits"], list) assert len(response["hits"]) >= 0 - assert response['hits'][0]["_federation"]["indexUid"] == INDEX_UID - assert response['hits'][0]["_federation"]["remote"] == REMOTE_MS_1 + assert response["hits"][0]["_federation"]["indexUid"] == INDEX_UID + assert response["hits"][0]["_federation"]["remote"] == REMOTE_MS_1 assert response["remoteErrors"] == {} From 1c8a01082dc2e8e401097915636dd922208dfc95 Mon Sep 17 00:00:00 2001 From: MuddyHope Date: Sat, 12 Apr 2025 12:58:49 -0700 Subject: [PATCH 06/12] adding fixes --- tests/client/test_client_multi_search_meilisearch.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/client/test_client_multi_search_meilisearch.py b/tests/client/test_client_multi_search_meilisearch.py index 9167d62b..37656526 100644 --- a/tests/client/test_client_multi_search_meilisearch.py +++ b/tests/client/test_client_multi_search_meilisearch.py @@ -77,6 +77,7 @@ def test_multi_search_with_federation_options(client, index_with_documents): assert response["offset"] == 0 +@pytest.mark.usefixtures("enable_network_options") def test_multi_search_with_network(client, index_with_documents): """Tests multi-search with network, with federation options.""" index_with_documents() From 3d12d2c812907e1ee822e8f29cb7425abeb87800 Mon Sep 17 00:00:00 2001 From: MuddyHope Date: Sat, 12 Apr 2025 16:47:14 -0700 Subject: [PATCH 07/12] adding fixes for test cases --- .../test_client_multi_search_meilisearch.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/tests/client/test_client_multi_search_meilisearch.py b/tests/client/test_client_multi_search_meilisearch.py index 37656526..3a547493 100644 --- a/tests/client/test_client_multi_search_meilisearch.py +++ b/tests/client/test_client_multi_search_meilisearch.py @@ -1,7 +1,7 @@ import pytest from meilisearch.errors import MeilisearchApiError -from tests.common import INDEX_UID, REMOTE_MS_1 +from tests.common import INDEX_UID, REMOTE_MS_1, REMOTE_MS_2 def test_basic_multi_search(client, empty_index): @@ -81,11 +81,26 @@ def test_multi_search_with_federation_options(client, index_with_documents): def test_multi_search_with_network(client, index_with_documents): """Tests multi-search with network, with federation options.""" index_with_documents() + resp = client.add_or_update_networks( + { + "remotes": { + REMOTE_MS_1: { + "url": "http://ms-1235.example.meilisearch.io", + "searchApiKey": "xxxxxxxx", + }, + REMOTE_MS_2: { + "url": "http://ms-1255.example.meilisearch.io", + "searchApiKey": "xxxxxxxx", + }, + } + } + ) response = client.multi_search( [{"indexUid": INDEX_UID, "q": "", "federationOptions": {"remote": REMOTE_MS_1}}], federation={}, ) + assert "results" not in resp assert "results" not in response assert isinstance(response["hits"], list) assert len(response["hits"]) >= 0 From da0cb295ca2f88d7aa03f18bcae4e577b08d7374 Mon Sep 17 00:00:00 2001 From: MuddyHope Date: Sat, 12 Apr 2025 18:00:25 -0700 Subject: [PATCH 08/12] fixes --- tests/client/test_client_multi_search_meilisearch.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/client/test_client_multi_search_meilisearch.py b/tests/client/test_client_multi_search_meilisearch.py index 3a547493..12f5f186 100644 --- a/tests/client/test_client_multi_search_meilisearch.py +++ b/tests/client/test_client_multi_search_meilisearch.py @@ -83,6 +83,7 @@ def test_multi_search_with_network(client, index_with_documents): index_with_documents() resp = client.add_or_update_networks( { + "self": REMOTE_MS_1, "remotes": { REMOTE_MS_1: { "url": "http://ms-1235.example.meilisearch.io", From 4a52d181327c13aaf7c348d9cc20f0dc14640c34 Mon Sep 17 00:00:00 2001 From: MuddyHope Date: Sat, 12 Apr 2025 18:00:25 -0700 Subject: [PATCH 09/12] fixes --- tests/client/test_client_multi_search_meilisearch.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/client/test_client_multi_search_meilisearch.py b/tests/client/test_client_multi_search_meilisearch.py index 12f5f186..324a6747 100644 --- a/tests/client/test_client_multi_search_meilisearch.py +++ b/tests/client/test_client_multi_search_meilisearch.py @@ -93,7 +93,7 @@ def test_multi_search_with_network(client, index_with_documents): "url": "http://ms-1255.example.meilisearch.io", "searchApiKey": "xxxxxxxx", }, - } + }, } ) response = client.multi_search( From ca2111aef99147edb9aff7298871c76c51392623 Mon Sep 17 00:00:00 2001 From: MuddyHope Date: Sat, 12 Apr 2025 18:04:15 -0700 Subject: [PATCH 10/12] fixes --- tests/client/test_client_multi_search_meilisearch.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/client/test_client_multi_search_meilisearch.py b/tests/client/test_client_multi_search_meilisearch.py index 324a6747..12f5f186 100644 --- a/tests/client/test_client_multi_search_meilisearch.py +++ b/tests/client/test_client_multi_search_meilisearch.py @@ -93,7 +93,7 @@ def test_multi_search_with_network(client, index_with_documents): "url": "http://ms-1255.example.meilisearch.io", "searchApiKey": "xxxxxxxx", }, - }, + } } ) response = client.multi_search( From 0ffd4e314246874762c06e029a21e7de1dcf9c24 Mon Sep 17 00:00:00 2001 From: MuddyHope Date: Sat, 12 Apr 2025 18:04:35 -0700 Subject: [PATCH 11/12] fixes --- tests/client/test_client_multi_search_meilisearch.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/client/test_client_multi_search_meilisearch.py b/tests/client/test_client_multi_search_meilisearch.py index 12f5f186..324a6747 100644 --- a/tests/client/test_client_multi_search_meilisearch.py +++ b/tests/client/test_client_multi_search_meilisearch.py @@ -93,7 +93,7 @@ def test_multi_search_with_network(client, index_with_documents): "url": "http://ms-1255.example.meilisearch.io", "searchApiKey": "xxxxxxxx", }, - } + }, } ) response = client.multi_search( From cf61cf15ba0bf12b561d97d26cc19a48beab2786 Mon Sep 17 00:00:00 2001 From: MuddyHope <60110059+MuddyHope@users.noreply.github.com> Date: Tue, 15 Apr 2025 00:05:21 +0530 Subject: [PATCH 12/12] Update meilisearch/client.py Copilot changes for typo Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- meilisearch/client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/meilisearch/client.py b/meilisearch/client.py index dae8d8eb..18a6d432 100644 --- a/meilisearch/client.py +++ b/meilisearch/client.py @@ -786,7 +786,7 @@ def get_all_networks(self) -> Dict[str, str]: Returns ------- remote networks: - All remote networks containeing information about each remote and their respective remote-name and searchApi key + All remote networks containing information about each remote and their respective remote-name and searchApi key Raises ------