Skip to content

Add support for distinct in multi search federation#782

Open
aznszn wants to merge 1 commit into
meilisearch:mainfrom
aznszn:feat/add-distinct-federated
Open

Add support for distinct in multi search federation#782
aznszn wants to merge 1 commit into
meilisearch:mainfrom
aznszn:feat/add-distinct-federated

Conversation

@aznszn
Copy link
Copy Markdown
Contributor

@aznszn aznszn commented Mar 30, 2026

Pull Request

Related issue

Fixes #777

What does this PR do?

  • Added distinct as a federation parameter

PR checklist

Please check if your PR fulfills the following requirements:

  • Did you use any AI tool while implementing this PR (code, tests, docs, etc.)? If yes, disclose it in the PR description and describe what it was used for. AI usage is allowed when it is disclosed.

AI was not used

  • Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)?
  • Have you read the contributing guidelines?
  • Have you made sure that the title is accurate and descriptive of the changes?

Summary by CodeRabbit

Release Notes

  • New Features

    • Federated multi-search operations now support distinct parameters, enabling advanced deduplication and result filtering across multiple indexes. Users can specify distinct field values to optimize and control their result sets.
  • Tests

    • Added integration test for federated multi-search with distinct parameter support, validating correct behavior and result aggregation across multiple indexes.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Mar 30, 2026

📝 Walkthrough

Walkthrough

Added an optional distinct field to FederationOptions struct to support distinct value computation in federated multi-search requests. Includes an integration test validating the distinct parameter reduces hits to expected count.

Changes

Cohort / File(s) Summary
Federated Search Distinct Support
src/search.rs
Extended FederationOptions with optional distinct: Option<String> field; added integration test test_federated_multi_search_with_distinct to validate distinct filtering behavior in federated multi-search context.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐰 A whisper through the federated streams,
Distinct at last—no more scattered dreams!
With Option<String> so clean and bright,
The search results merge just right.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately and concisely summarizes the main change: adding distinct support for federated multi-search operations.
Linked Issues check ✅ Passed The pull request successfully implements both objectives from issue #777: adds optional distinct field to FederationOptions and includes test coverage for federated search with distinct.
Out of Scope Changes check ✅ Passed All changes are directly related to the linked issue #777 requirements; no out-of-scope modifications detected.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@aznszn aznszn force-pushed the feat/add-distinct-federated branch from 8d9a307 to b53fe61 Compare March 30, 2026 07:57
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
src/search.rs (1)

2406-2424: Strengthen this test to validate global distinct across merged queries.

At Line 2411-Line 2415, the test uses a single query, so it doesn’t really prove deduplication across federated merged results. Consider adding two queries and asserting uniqueness of kind in the merged hits.

Proposed test hardening
 #[meilisearch_test]
 async fn test_federated_multi_search_with_distinct(
     client: Client,
     index: Index,
 ) -> Result<(), Error> {
     setup_test_index(&client, &index).await?;
-    let search_query = SearchQuery::new(&index).build();
+    let search_query_text = SearchQuery::new(&index).with_query("Lorem").build();
+    let search_query_title = SearchQuery::new(&index).with_query("Harry").build();

     let mut multi_search = client.multi_search();
-    multi_search.with_search_query(search_query);
+    multi_search.with_search_query(search_query_text);
+    multi_search.with_search_query(search_query_title);

     let federated_multi_search = multi_search.with_federation(FederationOptions {
         distinct: Some("kind".to_string()),
         ..Default::default()
     });

     let response = federated_multi_search.execute::<Document>().await.unwrap();

     assert_eq!(response.hits.len(), 2);
+    let distinct_kinds = response
+        .hits
+        .iter()
+        .map(|hit| hit.result.kind.as_str())
+        .collect::<std::collections::HashSet<_>>();
+    assert_eq!(distinct_kinds.len(), response.hits.len());

     Ok(())
 }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/search.rs` around lines 2406 - 2424, The test function
test_federated_multi_search_with_distinct currently only adds one SearchQuery,
so it doesn't validate global deduplication across merged federated results;
update the test to add two different SearchQuery instances to the multi_search
(e.g., call multi_search.with_search_query(...) twice or use
multi_search.with_search_queries([...]) so the federation actually merges
multiple queries), execute federated_multi_search.execute::<Document>(), and
then assert that the merged response.hits contain unique values for the "kind"
field (e.g., assert the number of hits equals the number of distinct kinds and
that no duplicate kind values appear) to prove global distinct behavior when
using FederationOptions { distinct: Some("kind".to_string()), ... }.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@src/search.rs`:
- Around line 2406-2424: The test function
test_federated_multi_search_with_distinct currently only adds one SearchQuery,
so it doesn't validate global deduplication across merged federated results;
update the test to add two different SearchQuery instances to the multi_search
(e.g., call multi_search.with_search_query(...) twice or use
multi_search.with_search_queries([...]) so the federation actually merges
multiple queries), execute federated_multi_search.execute::<Document>(), and
then assert that the merged response.hits contain unique values for the "kind"
field (e.g., assert the number of hits equals the number of distinct kinds and
that no duplicate kind values appear) to prove global distinct behavior when
using FederationOptions { distinct: Some("kind".to_string()), ... }.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: d16a40d8-eff4-4b5d-abb1-130cabb77bfd

📥 Commits

Reviewing files that changed from the base of the PR and between 82fe194 and b53fe61.

📒 Files selected for processing (1)
  • src/search.rs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Meilisearch v1.40] Add support for distinct in federated search requests

1 participant