Skip to content

Commit

Permalink
Merge #1013
Browse files Browse the repository at this point in the history
1013: Federated Search Addition r=sanders41 a=MuddyHope

# Pull Request

## Related issue
Fixes #1009

## What does this PR do?
- Adds the federated search to the `multi-search` method

## PR checklist
Please check if your PR fulfills the following requirements:
- [x] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)?
- [x] Have you read the contributing guidelines?
- [x] Have you made sure that the title is accurate and descriptive of the changes?

Thank you so much for contributing to Meilisearch!


Co-authored-by: MuddyHope <daktarisun@gmail.com>
Co-authored-by: MuddyHope <60110059+MuddyHope@users.noreply.github.com>
  • Loading branch information
3 people authored Aug 29, 2024
2 parents 7dbade5 + 2361c9d commit 316e137
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
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

0 comments on commit 316e137

Please sign in to comment.