Skip to content

Commit 031d9db

Browse files
meili-bors[bot]Barabasbalazscurquiza
authored
Merge #785
785: Distinct search parameter support added r=curquiza a=Barabasbalazs # Pull Request ## Related issue Fixes #756 ## What does this PR do? - Added support for using the _distinct_ keyword in the java client. Updated tests and code samples accordingly. ## 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: Balazs Barabas <barabas.balazska@gmail.com> Co-authored-by: Clémentine <clementine@meilisearch.com>
2 parents b5278c9 + 3cbd1f6 commit 031d9db

File tree

5 files changed

+92
-7
lines changed

5 files changed

+92
-7
lines changed

.code-samples.meilisearch.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -827,3 +827,13 @@ get_similar_post_1:
827827
.setId("143")
828828
.setEmbedder("manual");
829829
client.index("movies").searchSimilarDocuments(query)
830+
search_parameter_reference_distinct_1: |-
831+
SearchRequest searchRequest = SearchRequest.builder().q("QUERY TERMS").distinct("ATTRIBUTE_A").build();
832+
client.index("INDEX_NAME").search(searchRequest);
833+
distinct_attribute_guide_filterable_1: |-
834+
Settings settings = new Settings();
835+
settings.setFilterableAttributes(new String[] {"product_id", "SKU", "url"});
836+
client.index("products").updateSettings(settings);
837+
distinct_attribute_guide_distinct_parameter_1: |-
838+
SearchRequest searchRequest = SearchRequest.builder().q("white shirt").distinct("sku").build();
839+
client.index("products").search(searchRequest);

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,14 @@ public class IndexSearchRequest {
3636
protected Double rankingScoreThreshold;
3737
private String[] attributesToSearchOn;
3838
private FederationOptions federationOptions;
39+
protected String distinct;
3940

4041
/**
4142
* Constructor for MultiSearchRequest for building search queries with the default values:
4243
* offset: 0, limit: 20, attributesToRetrieve: ["*"], attributesToCrop: null, cropLength: 200,
4344
* attributesToHighlight: null, filter: null, showMatchesPosition: false, facets: null, sort:
4445
* null, showRankingScore: false, showRankingScoreDetails: false, rankingScoreThreshold: null
46+
* distinct: null
4547
*
4648
* @param indexUid uid of the requested index String
4749
*/
@@ -101,7 +103,8 @@ public String toString() {
101103
.putOpt("showRankingScore", this.showRankingScore)
102104
.putOpt("showRankingScoreDetails", this.showRankingScoreDetails)
103105
.putOpt("rankingScoreThreshold", this.rankingScoreThreshold)
104-
.putOpt("attributesToSearchOn", this.attributesToSearchOn);
106+
.putOpt("attributesToSearchOn", this.attributesToSearchOn)
107+
.putOpt("distinct", this.distinct);
105108

106109
return jsonObject.toString();
107110
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public class SearchRequest {
4040
protected Boolean showRankingScore;
4141
protected Boolean showRankingScoreDetails;
4242
protected Double rankingScoreThreshold;
43+
protected String distinct;
4344

4445
/**
4546
* Constructor for SearchRequest for building search queries with the default values: offset: 0,
@@ -100,7 +101,8 @@ public String toString() {
100101
.putOpt("filter", this.filterArray)
101102
.putOpt("showRankingScore", this.showRankingScore)
102103
.putOpt("showRankingScoreDetails", this.showRankingScoreDetails)
103-
.putOpt("rankingScoreThreshold", this.rankingScoreThreshold);
104+
.putOpt("rankingScoreThreshold", this.rankingScoreThreshold)
105+
.putOpt("distinct", this.distinct);
104106

105107
return jsonObject.toString();
106108
}

src/test/java/com/meilisearch/integration/SearchTest.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,29 @@ public void testSearchWithRankingScoreThreshold() throws Exception {
410410
assertThat(resGson.hits[0].getRankingScore(), is(greaterThanOrEqualTo(0.9)));
411411
}
412412

413+
/** Test with show distinct */
414+
@Test
415+
public void testSearchWithDistinct() throws Exception {
416+
String indexUid = "SearchDistinct";
417+
Index index = client.index(indexUid);
418+
GsonJsonHandler jsonGson = new GsonJsonHandler();
419+
420+
TestData<Movie> testData = this.getTestData(MOVIES_INDEX, Movie.class);
421+
TaskInfo task = index.addDocuments(testData.getRaw());
422+
423+
index.waitForTask(task.getTaskUid());
424+
425+
Settings settings = index.getSettings();
426+
427+
settings.setFilterableAttributes(new String[] {"language"});
428+
index.waitForTask(index.updateSettings(settings).getTaskUid());
429+
430+
SearchRequest searchRequest = SearchRequest.builder().q("").distinct("language").build();
431+
432+
Results resGson = jsonGson.decode(index.rawSearch(searchRequest), Results.class);
433+
assertThat(resGson.hits, is(arrayWithSize(4)));
434+
}
435+
413436
/** Test search with phrase */
414437
@Test
415438
public void testSearchPhrase() throws Exception {
@@ -871,6 +894,43 @@ public void testMultiSearchWithRankingScoreThreshold() throws Exception {
871894
}
872895
}
873896

897+
/** Test multisearch with distinct */
898+
@Test
899+
public void testMultiSearchWithDistinct() throws Exception {
900+
HashSet<String> indexUids = new HashSet<String>();
901+
indexUids.add("MultiSearch1");
902+
indexUids.add("MultiSearch2");
903+
904+
for (String indexUid : indexUids) {
905+
Index index = client.index(indexUid);
906+
907+
TestData<Movie> testData = this.getTestData(MOVIES_INDEX, Movie.class);
908+
TaskInfo task = index.addDocuments(testData.getRaw());
909+
910+
Settings settings = new Settings();
911+
settings.setFilterableAttributes(new String[] {"language", "title"});
912+
913+
index.waitForTask(index.updateSettings(settings).getTaskUid());
914+
915+
index.waitForTask(task.getTaskUid());
916+
}
917+
918+
MultiSearchRequest search = new MultiSearchRequest();
919+
920+
for (String indexUid : indexUids) {
921+
search.addQuery(new IndexSearchRequest(indexUid).setQuery("").setDistinct("language"));
922+
}
923+
924+
MultiSearchResult[] results = client.multiSearch(search).getResults();
925+
926+
assertThat(results.length, is(2));
927+
928+
for (MultiSearchResult searchResult : results) {
929+
assertThat(indexUids.contains(searchResult.getIndexUid()), is(true));
930+
assertThat(searchResult.getHits().size(), is(4));
931+
}
932+
}
933+
874934
@Test
875935
public void testSimilarDocuments() throws Exception {
876936
HashMap<String, Boolean> features = new HashMap();

src/test/java/com/meilisearch/sdk/SearchRequestTest.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ void toStringEveryParameters() {
133133
.setFacets(new String[] {"facets"})
134134
.setSort(new String[] {"sort"})
135135
.setPage(10)
136-
.setHitsPerPage(2);
136+
.setHitsPerPage(2)
137+
.setDistinct("distinct");
137138

138139
assertThat(classToTest.getQ(), is(equalTo("This is a Test")));
139140
assertThat(classToTest.getOffset(), is(equalTo(200)));
@@ -153,6 +154,7 @@ void toStringEveryParameters() {
153154
assertThat(classToTest.getCropLength(), is(equalTo(900)));
154155
assertThat(classToTest.getPage(), is(equalTo(10)));
155156
assertThat(classToTest.getHitsPerPage(), is(equalTo(2)));
157+
assertThat(classToTest.getDistinct(), is(equalTo("distinct")));
156158
}
157159

158160
@Test
@@ -172,6 +174,7 @@ void toStringEveryParametersWithBuilder() {
172174
.sort(new String[] {"sort"})
173175
.page(10)
174176
.hitsPerPage(2)
177+
.distinct("distinct")
175178
.build();
176179

177180
assertThat(classToTest.getQ(), is(equalTo("This is a Test")));
@@ -192,6 +195,7 @@ void toStringEveryParametersWithBuilder() {
192195
assertThat(classToTest.getCropLength(), is(equalTo(900)));
193196
assertThat(classToTest.getPage(), is(equalTo(10)));
194197
assertThat(classToTest.getHitsPerPage(), is(equalTo(2)));
198+
assertThat(classToTest.getDistinct(), is(equalTo("distinct")));
195199
}
196200

197201
@Test
@@ -217,9 +221,10 @@ void toStringEveryParametersWithArray() {
217221
.setFacets(new String[] {"facets"})
218222
.setSort(new String[] {"sort"})
219223
.setPage(0)
220-
.setHitsPerPage(0);
224+
.setHitsPerPage(0)
225+
.setDistinct("distinct");
221226
String expectedToString =
222-
"{\"attributesToRetrieve\":[\"bubble\"],\"offset\":200,\"cropMarker\":\"123\",\"hitsPerPage\":0,\"attributesToSearchOn\":[\"searchOn\"],\"sort\":[\"sort\"],\"highlightPreTag\":\"abc\",\"facets\":[\"facets\"],\"filter\":[[\"test='test'\"],[\"test1='test1'\"]],\"q\":\"This is a Test\",\"matchingStrategy\":\"all\",\"showMatchesPosition\":true,\"limit\":900,\"cropLength\":900,\"highlightPostTag\":\"zyx\",\"attributesToHighlight\":[\"highlight\"],\"page\":0,\"attributesToCrop\":[\"crop\"]}";
227+
"{\"attributesToRetrieve\":[\"bubble\"],\"offset\":200,\"cropMarker\":\"123\",\"hitsPerPage\":0,\"attributesToSearchOn\":[\"searchOn\"],\"distinct\":\"distinct\",\"sort\":[\"sort\"],\"highlightPreTag\":\"abc\",\"facets\":[\"facets\"],\"filter\":[[\"test='test'\"],[\"test1='test1'\"]],\"q\":\"This is a Test\",\"matchingStrategy\":\"all\",\"showMatchesPosition\":true,\"limit\":900,\"cropLength\":900,\"highlightPostTag\":\"zyx\",\"attributesToHighlight\":[\"highlight\"],\"page\":0,\"attributesToCrop\":[\"crop\"]}";
223228

224229
assertThat(classToTest.getQ(), is(equalTo("This is a Test")));
225230
assertThat(classToTest.getOffset(), is(equalTo(200)));
@@ -241,6 +246,7 @@ void toStringEveryParametersWithArray() {
241246
assertThat(classToTest.getCropLength(), is(equalTo(900)));
242247
assertThat(classToTest.getPage(), is(equalTo(0)));
243248
assertThat(classToTest.getHitsPerPage(), is(equalTo(0)));
249+
assertThat(classToTest.getDistinct(), is(equalTo("distinct")));
244250
assertThat(classToTest.toString(), is(equalTo(expectedToString)));
245251
}
246252

@@ -269,9 +275,10 @@ void toStringEveryParametersWithArrayWithBuilder() {
269275
.sort(new String[] {"sort"})
270276
.page(0)
271277
.hitsPerPage(0)
278+
.distinct("distinct")
272279
.build();
273280
String expectedToString =
274-
"{\"attributesToRetrieve\":[\"bubble\"],\"offset\":200,\"cropMarker\":\"123\",\"hitsPerPage\":0,\"attributesToSearchOn\":[\"searchOn\"],\"sort\":[\"sort\"],\"highlightPreTag\":\"abc\",\"facets\":[\"facets\"],\"filter\":[[\"test='test'\"],[\"test1='test1'\"]],\"q\":\"This is a Test\",\"matchingStrategy\":\"all\",\"showMatchesPosition\":true,\"limit\":900,\"cropLength\":900,\"highlightPostTag\":\"zyx\",\"attributesToHighlight\":[\"highlight\"],\"page\":0,\"attributesToCrop\":[\"crop\"]}";
281+
"{\"attributesToRetrieve\":[\"bubble\"],\"offset\":200,\"cropMarker\":\"123\",\"hitsPerPage\":0,\"attributesToSearchOn\":[\"searchOn\"],\"distinct\":\"distinct\",\"sort\":[\"sort\"],\"highlightPreTag\":\"abc\",\"facets\":[\"facets\"],\"filter\":[[\"test='test'\"],[\"test1='test1'\"]],\"q\":\"This is a Test\",\"matchingStrategy\":\"all\",\"showMatchesPosition\":true,\"limit\":900,\"cropLength\":900,\"highlightPostTag\":\"zyx\",\"attributesToHighlight\":[\"highlight\"],\"page\":0,\"attributesToCrop\":[\"crop\"]}";
275282

276283
assertThat(classToTest.getQ(), is(equalTo("This is a Test")));
277284
assertThat(classToTest.getOffset(), is(equalTo(200)));
@@ -293,6 +300,7 @@ void toStringEveryParametersWithArrayWithBuilder() {
293300
assertThat(classToTest.getCropLength(), is(equalTo(900)));
294301
assertThat(classToTest.getPage(), is(equalTo(0)));
295302
assertThat(classToTest.getHitsPerPage(), is(equalTo(0)));
303+
assertThat(classToTest.getDistinct(), is(equalTo("distinct")));
296304
assertThat(classToTest.toString(), is(equalTo(expectedToString)));
297305
}
298306

@@ -321,9 +329,10 @@ void toStringEveryParametersWithArrayMatchingStrategyNull() {
321329
.sort(new String[] {"sort"})
322330
.page(0)
323331
.hitsPerPage(0)
332+
.distinct("distinct")
324333
.build();
325334
String expectedToString =
326-
"{\"attributesToRetrieve\":[\"bubble\"],\"offset\":200,\"cropMarker\":\"123\",\"hitsPerPage\":0,\"attributesToSearchOn\":[\"searchOn\"],\"sort\":[\"sort\"],\"highlightPreTag\":\"abc\",\"facets\":[\"facets\"],\"filter\":[[\"test='test'\"],[\"test1='test1'\"]],\"q\":\"This is a Test\",\"showMatchesPosition\":true,\"limit\":900,\"cropLength\":900,\"highlightPostTag\":\"zyx\",\"attributesToHighlight\":[\"highlight\"],\"page\":0,\"attributesToCrop\":[\"crop\"]}";
335+
"{\"attributesToRetrieve\":[\"bubble\"],\"offset\":200,\"cropMarker\":\"123\",\"hitsPerPage\":0,\"attributesToSearchOn\":[\"searchOn\"],\"distinct\":\"distinct\",\"sort\":[\"sort\"],\"highlightPreTag\":\"abc\",\"facets\":[\"facets\"],\"filter\":[[\"test='test'\"],[\"test1='test1'\"]],\"q\":\"This is a Test\",\"showMatchesPosition\":true,\"limit\":900,\"cropLength\":900,\"highlightPostTag\":\"zyx\",\"attributesToHighlight\":[\"highlight\"],\"page\":0,\"attributesToCrop\":[\"crop\"]}";
327336

328337
assertThat(classToTest.getQ(), is(equalTo("This is a Test")));
329338
assertThat(classToTest.getOffset(), is(equalTo(200)));
@@ -343,6 +352,7 @@ void toStringEveryParametersWithArrayMatchingStrategyNull() {
343352
assertThat(classToTest.getFacets()[0], is(equalTo("facets")));
344353
assertThat(classToTest.getSort()[0], is(equalTo("sort")));
345354
assertThat(classToTest.getCropLength(), is(equalTo(900)));
355+
assertThat(classToTest.getDistinct(), is(equalTo("distinct")));
346356
assertThat(classToTest.toString(), is(equalTo(expectedToString)));
347357
}
348358
}

0 commit comments

Comments
 (0)