Skip to content

feat: support distinct in federated search#972

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

feat: support distinct in federated search#972
GabrielBBaldez wants to merge 1 commit into
meilisearch:mainfrom
GabrielBBaldez:feat/federated-distinct

Conversation

@GabrielBBaldez

@GabrielBBaldez GabrielBBaldez commented Jun 18, 2026

Copy link
Copy Markdown

Adds support for the distinct attribute in federated search, introduced in Meilisearch 1.40.0. Closes #949.

  • MultiSearchFederation now carries an optional distinct string, serialized into the federation object of the multi-search payload (alongside the existing limit / offset / mergeFacets / facetsByIndex options).
  • Unit tests verifying distinct is serialized when set and omitted when not.

Changes Made

MultiSearchFederation.java

Added support for the distinct attribute in federated search operations:

  • Added private String distinct field
  • Added public setDistinct(String distinct) method that enables fluent method chaining

MultiSearchFederationTest.java

Created new test class with two test cases:

  • serializesDistinctInFederationOptions: Verifies that when distinct("movie_id") is set, the Gson-encoded JSON includes "distinct":"movie_id"
  • omitsDistinctWhenNotSet: Verifies that when distinct is not configured, the encoded JSON does not contain the distinct field

Implementation Notes

The distinct field is serialized via Gson's JSON encoding mechanism used by jsonHandler.encode(). While the toString() method only explicitly serializes limit and offset fields, Gson's default behavior will serialize all non-null fields with the @Getter annotation, allowing the distinct parameter to be included in the federation object alongside other federation options.

Alignment with Objectives

This implementation fulfills the requirements from Issue #949 by:

  1. Extending the MultiSearchFederation class to accept an optional distinct attribute
  2. Enabling the distinct parameter in federated search requests for Meilisearch 1.40.0 compatibility
  3. Including comprehensive unit tests validating proper serialization behavior

@coderabbitai

coderabbitai Bot commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Adds a distinct string field to MultiSearchFederation with a Lombok @Getter and a fluent setDistinct method. A new MultiSearchFederationTest class validates that the field appears in serialized JSON when set and is absent when not configured.

Changes

MultiSearchFederation distinct support

Layer / File(s) Summary
distinct field, setter, and serialization tests
src/main/java/com/meilisearch/sdk/MultiSearchFederation.java, src/test/java/com/meilisearch/sdk/MultiSearchFederationTest.java
Adds private String distinct field with Lombok getter and a fluent setDistinct(String) method. Two new JUnit 5 tests assert JSON encodes "distinct":"movie_id" when set and omits the field when not configured.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Poem

A bunny hopped in, ears alert and bright,
Declaring "distinct!" with federated delight,
A field was added, a setter too,
The JSON now carries this field shiny and new,
Tests confirm it appears — or stays out of sight! 🐇✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'feat: support distinct in federated search' directly and clearly describes the main change—adding distinct parameter support to federated search in the MultiSearchFederation class.
Linked Issues check ✅ Passed The PR fully addresses both objectives from issue #949: the MultiSearchFederation class now accepts an optional distinct attribute via the setDistinct() method, and test cases verify correct serialization behavior.
Out of Scope Changes check ✅ Passed All changes are directly scoped to implementing the distinct parameter in federated search as specified in issue #949; no unrelated modifications are present.

✏️ 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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/main/java/com/meilisearch/sdk/MultiSearchFederation.java (1)

47-50: ⚠️ Potential issue | 🟠 Major | ⚡ Quick win

toString() omits distinct (and other federation fields), so serialized output is incomplete.

On Line 49, the JSON only includes limit/offset. After adding distinct, this method now returns incorrect federation JSON for any caller using toString().

Suggested fix
     `@Override`
     public String toString() {
         JSONObject jsonObject =
-                new JSONObject().put("limit", this.limit).put("offset", this.offset);
+                new JSONObject()
+                        .put("limit", this.limit)
+                        .put("offset", this.offset)
+                        .put("mergeFacets", this.mergeFacets)
+                        .put("facetsByIndex", this.facetsByIndex)
+                        .put("distinct", this.distinct);
         return jsonObject.toString();
     }
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/main/java/com/meilisearch/sdk/MultiSearchFederation.java` around lines 47
- 50, The toString() method in the MultiSearchFederation class only serializes
the limit and offset fields to the JSONObject, but omits the distinct field and
any other federation fields. Update the toString() method to include all
federation instance fields when constructing the JSONObject, ensuring that the
distinct field is added to the JSON alongside the existing limit and offset
fields using the same put() pattern already in use.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@src/main/java/com/meilisearch/sdk/MultiSearchFederation.java`:
- Around line 47-50: The toString() method in the MultiSearchFederation class
only serializes the limit and offset fields to the JSONObject, but omits the
distinct field and any other federation fields. Update the toString() method to
include all federation instance fields when constructing the JSONObject,
ensuring that the distinct field is added to the JSON alongside the existing
limit and offset fields using the same put() pattern already in use.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 37c96f06-fbe3-4e70-8b86-6443785573d4

📥 Commits

Reviewing files that changed from the base of the PR and between acfe026 and 99af7a2.

📒 Files selected for processing (2)
  • src/main/java/com/meilisearch/sdk/MultiSearchFederation.java
  • src/test/java/com/meilisearch/sdk/MultiSearchFederationTest.java

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