diff --git a/src/main/java/com/meilisearch/sdk/IndexSearchRequest.java b/src/main/java/com/meilisearch/sdk/IndexSearchRequest.java index 8e35ab3e..e0e29acc 100644 --- a/src/main/java/com/meilisearch/sdk/IndexSearchRequest.java +++ b/src/main/java/com/meilisearch/sdk/IndexSearchRequest.java @@ -1,5 +1,6 @@ package com.meilisearch.sdk; +import com.meilisearch.sdk.model.Hybrid; import com.meilisearch.sdk.model.MatchingStrategy; import lombok.*; import lombok.experimental.Accessors; @@ -38,6 +39,7 @@ public class IndexSearchRequest { private FederationOptions federationOptions; protected String[] locales; protected String distinct; + protected Hybrid hybrid; protected Boolean retrieveVectors; /** @@ -45,7 +47,7 @@ public class IndexSearchRequest { * offset: 0, limit: 20, attributesToRetrieve: ["*"], attributesToCrop: null, cropLength: 200, * attributesToHighlight: null, filter: null, showMatchesPosition: false, facets: null, sort: * null, showRankingScore: false, showRankingScoreDetails: false, rankingScoreThreshold: null - * distinct: null, retrieveVectors: false + * distinct: null, retrieveVectors: false, hybrid: null * * @param indexUid uid of the requested index String */ @@ -108,7 +110,8 @@ public String toString() { .putOpt("attributesToSearchOn", this.attributesToSearchOn) .putOpt("locales", this.locales) .putOpt("distinct", this.distinct) - .putOpt("retrieveVectors", this.retrieveVectors); + .putOpt("retrieveVectors", this.retrieveVectors) + .putOpt("hybrid", this.hybrid != null ? this.hybrid.toJSONObject() : null); return jsonObject.toString(); } diff --git a/src/test/java/com/meilisearch/sdk/IndexSearchRequestTest.java b/src/test/java/com/meilisearch/sdk/IndexSearchRequestTest.java new file mode 100644 index 00000000..b5b1c171 --- /dev/null +++ b/src/test/java/com/meilisearch/sdk/IndexSearchRequestTest.java @@ -0,0 +1,57 @@ +package com.meilisearch.sdk; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; + +import com.meilisearch.sdk.model.Hybrid; +import org.junit.jupiter.api.Test; + +class IndexSearchRequestTest { + @Test + void toStringWithHybridUsingBuilder() { + IndexSearchRequest classToTest = + IndexSearchRequest.builder() + .q("This is a Test") + .hybrid(Hybrid.builder().semanticRatio(0.5).embedder("default").build()) + .build(); + + String expected = + "{\"q\":\"This is a Test\",\"hybrid\":{\"semanticRatio\":0.5,\"embedder\":\"default\"}}"; + assertThat(classToTest.toString(), is(equalTo(expected))); + + // Verify getters + assertThat(classToTest.getHybrid().getSemanticRatio(), is(equalTo(0.5))); + assertThat(classToTest.getHybrid().getEmbedder(), is(equalTo("default"))); + } + + @Test + void toStringWithHybridAndOtherParameters() { + IndexSearchRequest classToTest = + IndexSearchRequest.builder() + .q("This is a Test") + .offset(200) + .limit(900) + .hybrid( + Hybrid.builder() + .semanticRatio(0.7) + .embedder("custom-embedder") + .build()) + .build(); + + String expected = + "{\"q\":\"This is a Test\",\"hybrid\":{\"semanticRatio\":0.7,\"embedder\":\"custom-embedder\"},\"offset\":200,\"limit\":900}"; + assertThat(classToTest.toString(), is(equalTo(expected))); + } + + @Test + void toStringWithHybridOnlyEmbedder() { + IndexSearchRequest classToTest = + new IndexSearchRequest("someIndexUuid") + .setQuery("This is a Test") + .setHybrid(Hybrid.builder().embedder("default").build()); + + String expected = "{\"q\":\"This is a Test\",\"hybrid\":{\"embedder\":\"default\"}}"; + assertThat(classToTest.toString(), is(equalTo(expected))); + } +}