Skip to content

Commit 5fd98c8

Browse files
Merge #904
904: Support "Hybrid" parameter in `IndexSearchRequest` to enable multi search with hybrid search r=curquiza a=ErrorProne # Pull Request ## Related issue - ## What does this PR do? - Adds the Hybrid search option to IndexSearchRequest which is used in multi [search queries](https://www.meilisearch.com/docs/reference/api/multi_search) - This is basically a clone of the hybrid implementation in the SearchRequest class ## PR checklist Please check if your PR fulfills the following requirements: - [ ] 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? - [x] Have you made sure that the title is accurate and descriptive of the changes? Thank you so much for contributing to Meilisearch! <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Added hybrid search configuration to requests, allowing users to provide semantic ratio and embedder settings for combined keyword + semantic search; hybrid settings are included in request serialization. * **Tests** * Added tests verifying hybrid configuration is correctly represented in serialized request output and stringified form across multiple parameter combinations. <!-- end of auto-generated comment: release notes by coderabbit.ai --> Co-authored-by: fherpich <finn@thebuilders.de>
2 parents 7e4d851 + ad390e6 commit 5fd98c8

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

src/main/java/com/meilisearch/sdk/IndexSearchRequest.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.meilisearch.sdk;
22

3+
import com.meilisearch.sdk.model.Hybrid;
34
import com.meilisearch.sdk.model.MatchingStrategy;
45
import lombok.*;
56
import lombok.experimental.Accessors;
@@ -38,14 +39,15 @@ public class IndexSearchRequest {
3839
private FederationOptions federationOptions;
3940
protected String[] locales;
4041
protected String distinct;
42+
protected Hybrid hybrid;
4143
protected Boolean retrieveVectors;
4244

4345
/**
4446
* Constructor for MultiSearchRequest for building search queries with the default values:
4547
* offset: 0, limit: 20, attributesToRetrieve: ["*"], attributesToCrop: null, cropLength: 200,
4648
* attributesToHighlight: null, filter: null, showMatchesPosition: false, facets: null, sort:
4749
* null, showRankingScore: false, showRankingScoreDetails: false, rankingScoreThreshold: null
48-
* distinct: null, retrieveVectors: false
50+
* distinct: null, retrieveVectors: false, hybrid: null
4951
*
5052
* @param indexUid uid of the requested index String
5153
*/
@@ -108,7 +110,8 @@ public String toString() {
108110
.putOpt("attributesToSearchOn", this.attributesToSearchOn)
109111
.putOpt("locales", this.locales)
110112
.putOpt("distinct", this.distinct)
111-
.putOpt("retrieveVectors", this.retrieveVectors);
113+
.putOpt("retrieveVectors", this.retrieveVectors)
114+
.putOpt("hybrid", this.hybrid != null ? this.hybrid.toJSONObject() : null);
112115

113116
return jsonObject.toString();
114117
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.meilisearch.sdk;
2+
3+
import static org.hamcrest.MatcherAssert.assertThat;
4+
import static org.hamcrest.Matchers.equalTo;
5+
import static org.hamcrest.Matchers.is;
6+
7+
import com.meilisearch.sdk.model.Hybrid;
8+
import org.junit.jupiter.api.Test;
9+
10+
class IndexSearchRequestTest {
11+
@Test
12+
void toStringWithHybridUsingBuilder() {
13+
IndexSearchRequest classToTest =
14+
IndexSearchRequest.builder()
15+
.q("This is a Test")
16+
.hybrid(Hybrid.builder().semanticRatio(0.5).embedder("default").build())
17+
.build();
18+
19+
String expected =
20+
"{\"q\":\"This is a Test\",\"hybrid\":{\"semanticRatio\":0.5,\"embedder\":\"default\"}}";
21+
assertThat(classToTest.toString(), is(equalTo(expected)));
22+
23+
// Verify getters
24+
assertThat(classToTest.getHybrid().getSemanticRatio(), is(equalTo(0.5)));
25+
assertThat(classToTest.getHybrid().getEmbedder(), is(equalTo("default")));
26+
}
27+
28+
@Test
29+
void toStringWithHybridAndOtherParameters() {
30+
IndexSearchRequest classToTest =
31+
IndexSearchRequest.builder()
32+
.q("This is a Test")
33+
.offset(200)
34+
.limit(900)
35+
.hybrid(
36+
Hybrid.builder()
37+
.semanticRatio(0.7)
38+
.embedder("custom-embedder")
39+
.build())
40+
.build();
41+
42+
String expected =
43+
"{\"q\":\"This is a Test\",\"hybrid\":{\"semanticRatio\":0.7,\"embedder\":\"custom-embedder\"},\"offset\":200,\"limit\":900}";
44+
assertThat(classToTest.toString(), is(equalTo(expected)));
45+
}
46+
47+
@Test
48+
void toStringWithHybridOnlyEmbedder() {
49+
IndexSearchRequest classToTest =
50+
new IndexSearchRequest("someIndexUuid")
51+
.setQuery("This is a Test")
52+
.setHybrid(Hybrid.builder().embedder("default").build());
53+
54+
String expected = "{\"q\":\"This is a Test\",\"hybrid\":{\"embedder\":\"default\"}}";
55+
assertThat(classToTest.toString(), is(equalTo(expected)));
56+
}
57+
}

0 commit comments

Comments
 (0)