Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Federated Search Addition #1013

Merged
merged 5 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions meilisearch/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,14 +219,19 @@ def index(self, uid: str) -> Index:
return Index(self.config, uid=uid)
raise ValueError("The index UID should not be None")

def multi_search(self, queries: Sequence[Mapping[str, Any]]) -> Dict[str, List[Dict[str, Any]]]:
def multi_search(
self, queries: Sequence[Mapping[str, Any]], federation: Optional[Dict[str, Any]] = None
) -> Dict[str, List[Dict[str, Any]]]:
"""Multi-index search.

Parameters
----------
queries:
List of dictionaries containing the specified indexes and their search queries
https://www.meilisearch.com/docs/reference/api/search#search-in-an-index
federation: (optional):
Dictionary containing offset and limit
https://www.meilisearch.com/docs/reference/api/multi_search

Returns
-------
Expand All @@ -240,7 +245,7 @@ def multi_search(self, queries: Sequence[Mapping[str, Any]]) -> Dict[str, List[D
"""
return self.http.post(
f"{self.config.paths.multi_search}",
body={"queries": queries},
body={"queries": queries, "federation": federation},
)

def get_all_stats(self) -> Dict[str, Any]:
Expand Down
43 changes: 43 additions & 0 deletions tests/client/test_client_multi_search_meilisearch.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest

from meilisearch.errors import MeilisearchApiError
from tests.common import INDEX_UID


def test_basic_multi_search(client, empty_index):
Expand Down Expand Up @@ -32,3 +33,45 @@ def test_multi_search_on_no_index(client):
"""Tests multi-search on a non existing index."""
with pytest.raises(MeilisearchApiError):
client.multi_search([{"indexUid": "indexDoesNotExist", "q": ""}])


def test_multi_search_with_no_value_in_federation(client, empty_index, index_with_documents):
"""Tests multi-search with federation, but no value"""
index_with_documents()
empty_index("indexB")
response = client.multi_search(
[{"indexUid": INDEX_UID, "q": ""}, {"indexUid": "indexB", "q": ""}], {}
)
assert "results" not in response
assert len(response["hits"]) > 0
assert "_federation" in response["hits"][0]
assert response["limit"] == 20
assert response["offset"] == 0


def test_multi_search_with_offset_and_limit_in_federation(client, index_with_documents):
"""Tests multi-search with federation, with offset and limit value"""
index_with_documents()
response = client.multi_search([{"indexUid": INDEX_UID, "q": ""}], {"offset": 2, "limit": 2})

assert "results" not in response
assert len(response["hits"]) == 2
assert "_federation" in response["hits"][0]
assert response["limit"] == 2
assert response["offset"] == 2


def test_multi_search_with_federation_options(client, index_with_documents):
"""Tests multi-search with federation, with federation options"""
index_with_documents()
response = client.multi_search(
[{"indexUid": INDEX_UID, "q": "", "federationOptions": {"weight": 0.99}}], {"limit": 2}
)

assert "results" not in response
assert isinstance(response["hits"], list)
assert len(response["hits"]) == 2
assert response["hits"][0]["_federation"]["indexUid"] == INDEX_UID
assert response["hits"][0]["_federation"]["weightedRankingScore"] >= 0.99
assert response["limit"] == 2
assert response["offset"] == 0