Skip to content

Commit

Permalink
Merge branch 'main' into locales-support
Browse files Browse the repository at this point in the history
  • Loading branch information
Barabasbalazs committed Nov 6, 2024
2 parents 10b4f3e + 031d9db commit a579d99
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 6 deletions.
10 changes: 10 additions & 0 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,16 @@ get_similar_post_1:
.setId("143")
.setEmbedder("manual");
client.index("movies").searchSimilarDocuments(query)
search_parameter_reference_distinct_1: |-
SearchRequest searchRequest = SearchRequest.builder().q("QUERY TERMS").distinct("ATTRIBUTE_A").build();
client.index("INDEX_NAME").search(searchRequest);
distinct_attribute_guide_filterable_1: |-
Settings settings = new Settings();
settings.setFilterableAttributes(new String[] {"product_id", "SKU", "url"});
client.index("products").updateSettings(settings);
distinct_attribute_guide_distinct_parameter_1: |-
SearchRequest searchRequest = SearchRequest.builder().q("white shirt").distinct("sku").build();
client.index("products").search(searchRequest);
search_parameter_reference_locales_1: |-
SearchRequest searchRequest = SearchRequest.builder().q("進撃の巨人").locales(new String[]{"jpn"}).build();
client.index("INDEX_NAME").search(searchRequest);
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/com/meilisearch/sdk/IndexSearchRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ public class IndexSearchRequest {
private String[] attributesToSearchOn;
private FederationOptions federationOptions;
protected String[] locales;
protected String distinct;

/**
* Constructor for MultiSearchRequest for building search queries with the default values:
* 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
*
* @param indexUid uid of the requested index String
*/
Expand Down Expand Up @@ -103,7 +105,8 @@ public String toString() {
.putOpt("showRankingScoreDetails", this.showRankingScoreDetails)
.putOpt("rankingScoreThreshold", this.rankingScoreThreshold)
.putOpt("attributesToSearchOn", this.attributesToSearchOn)
.putOpt("locales", this.locales);
.putOpt("locales", this.locales)
.putOpt("distinct", this.distinct);

return jsonObject.toString();
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/meilisearch/sdk/SearchRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class SearchRequest {
protected Boolean showRankingScoreDetails;
protected Double rankingScoreThreshold;
protected String[] locales;
protected String distinct;

/**
* Constructor for SearchRequest for building search queries with the default values: offset: 0,
Expand Down Expand Up @@ -102,7 +103,8 @@ public String toString() {
.putOpt("showRankingScore", this.showRankingScore)
.putOpt("showRankingScoreDetails", this.showRankingScoreDetails)
.putOpt("rankingScoreThreshold", this.rankingScoreThreshold)
.putOpt("locales", this.locales);
.putOpt("locales", this.locales)
.putOpt("distinct", this.distinct);

return jsonObject.toString();
}
Expand Down
60 changes: 60 additions & 0 deletions src/test/java/com/meilisearch/integration/SearchTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,29 @@ public void testSearchWithRankingScoreThreshold() throws Exception {
assertThat(resGson.hits[0].getRankingScore(), is(greaterThanOrEqualTo(0.9)));
}

/** Test with show distinct */
@Test
public void testSearchWithDistinct() throws Exception {
String indexUid = "SearchDistinct";
Index index = client.index(indexUid);
GsonJsonHandler jsonGson = new GsonJsonHandler();

TestData<Movie> testData = this.getTestData(MOVIES_INDEX, Movie.class);
TaskInfo task = index.addDocuments(testData.getRaw());

index.waitForTask(task.getTaskUid());

Settings settings = index.getSettings();

settings.setFilterableAttributes(new String[] {"language"});
index.waitForTask(index.updateSettings(settings).getTaskUid());

SearchRequest searchRequest = SearchRequest.builder().q("").distinct("language").build();

Results resGson = jsonGson.decode(index.rawSearch(searchRequest), Results.class);
assertThat(resGson.hits, is(arrayWithSize(4)));
}

/** Test search with phrase */
@Test
public void testSearchPhrase() throws Exception {
Expand Down Expand Up @@ -871,6 +894,43 @@ public void testMultiSearchWithRankingScoreThreshold() throws Exception {
}
}

/** Test multisearch with distinct */
@Test
public void testMultiSearchWithDistinct() throws Exception {
HashSet<String> indexUids = new HashSet<String>();
indexUids.add("MultiSearch1");
indexUids.add("MultiSearch2");

for (String indexUid : indexUids) {
Index index = client.index(indexUid);

TestData<Movie> testData = this.getTestData(MOVIES_INDEX, Movie.class);
TaskInfo task = index.addDocuments(testData.getRaw());

Settings settings = new Settings();
settings.setFilterableAttributes(new String[] {"language", "title"});

index.waitForTask(index.updateSettings(settings).getTaskUid());

index.waitForTask(task.getTaskUid());
}

MultiSearchRequest search = new MultiSearchRequest();

for (String indexUid : indexUids) {
search.addQuery(new IndexSearchRequest(indexUid).setQuery("").setDistinct("language"));
}

MultiSearchResult[] results = client.multiSearch(search).getResults();

assertThat(results.length, is(2));

for (MultiSearchResult searchResult : results) {
assertThat(indexUids.contains(searchResult.getIndexUid()), is(true));
assertThat(searchResult.getHits().size(), is(4));
}
}

@Test
public void testSimilarDocuments() throws Exception {
HashMap<String, Boolean> features = new HashMap();
Expand Down
18 changes: 14 additions & 4 deletions src/test/java/com/meilisearch/sdk/SearchRequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ void toStringEveryParameters() {
.setSort(new String[] {"sort"})
.setPage(10)
.setHitsPerPage(2)
.setLocales(new String[] {"eng"});
.setLocales(new String[] {"eng"})
.setDistinct("distinct");

assertThat(classToTest.getQ(), is(equalTo("This is a Test")));
assertThat(classToTest.getOffset(), is(equalTo(200)));
Expand All @@ -155,6 +156,7 @@ void toStringEveryParameters() {
assertThat(classToTest.getPage(), is(equalTo(10)));
assertThat(classToTest.getHitsPerPage(), is(equalTo(2)));
assertThat(classToTest.getLocales()[0], is(equalTo("eng")));
assertThat(classToTest.getDistinct(), is(equalTo("distinct")));
}

@Test
Expand All @@ -175,6 +177,7 @@ void toStringEveryParametersWithBuilder() {
.page(10)
.hitsPerPage(2)
.locales(new String[] {"eng"})
.distinct("distinct")
.build();

assertThat(classToTest.getQ(), is(equalTo("This is a Test")));
Expand All @@ -196,6 +199,7 @@ void toStringEveryParametersWithBuilder() {
assertThat(classToTest.getPage(), is(equalTo(10)));
assertThat(classToTest.getHitsPerPage(), is(equalTo(2)));
assertThat(classToTest.getLocales()[0], is(equalTo("eng")));
assertThat(classToTest.getDistinct(), is(equalTo("distinct")));
}

@Test
Expand All @@ -222,9 +226,10 @@ void toStringEveryParametersWithArray() {
.setSort(new String[] {"sort"})
.setPage(0)
.setHitsPerPage(0)
.setDistinct("distinct")
.setLocales(new String[] {"eng"});
String expectedToString =
"{\"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\",\"locales\":[\"eng\"],\"matchingStrategy\":\"all\",\"showMatchesPosition\":true,\"limit\":900,\"cropLength\":900,\"highlightPostTag\":\"zyx\",\"attributesToHighlight\":[\"highlight\"],\"page\":0,\"attributesToCrop\":[\"crop\"]}";
"{\"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\",\"locales\":[\"eng\"],\"matchingStrategy\":\"all\",\"showMatchesPosition\":true,\"limit\":900,\"cropLength\":900,\"highlightPostTag\":\"zyx\",\"attributesToHighlight\":[\"highlight\"],\"page\":0,\"attributesToCrop\":[\"crop\"]}";

assertThat(classToTest.getQ(), is(equalTo("This is a Test")));
assertThat(classToTest.getOffset(), is(equalTo(200)));
Expand All @@ -247,6 +252,7 @@ void toStringEveryParametersWithArray() {
assertThat(classToTest.getPage(), is(equalTo(0)));
assertThat(classToTest.getHitsPerPage(), is(equalTo(0)));
assertThat(classToTest.getLocales()[0], is(equalTo("eng")));
assertThat(classToTest.getDistinct(), is(equalTo("distinct")));
assertThat(classToTest.toString(), is(equalTo(expectedToString)));
}

Expand Down Expand Up @@ -276,9 +282,10 @@ void toStringEveryParametersWithArrayWithBuilder() {
.page(0)
.hitsPerPage(0)
.locales(new String[] {"eng"})
.distinct("distinct")
.build();
String expectedToString =
"{\"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\",\"locales\":[\"eng\"],\"matchingStrategy\":\"all\",\"showMatchesPosition\":true,\"limit\":900,\"cropLength\":900,\"highlightPostTag\":\"zyx\",\"attributesToHighlight\":[\"highlight\"],\"page\":0,\"attributesToCrop\":[\"crop\"]}";
"{\"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\",\"locales\":[\"eng\"],\"matchingStrategy\":\"all\",\"showMatchesPosition\":true,\"limit\":900,\"cropLength\":900,\"highlightPostTag\":\"zyx\",\"attributesToHighlight\":[\"highlight\"],\"page\":0,\"attributesToCrop\":[\"crop\"]}";

assertThat(classToTest.getQ(), is(equalTo("This is a Test")));
assertThat(classToTest.getOffset(), is(equalTo(200)));
Expand All @@ -301,6 +308,7 @@ void toStringEveryParametersWithArrayWithBuilder() {
assertThat(classToTest.getPage(), is(equalTo(0)));
assertThat(classToTest.getHitsPerPage(), is(equalTo(0)));
assertThat(classToTest.getLocales()[0], is(equalTo("eng")));
assertThat(classToTest.getDistinct(), is(equalTo("distinct")));
assertThat(classToTest.toString(), is(equalTo(expectedToString)));
}

Expand Down Expand Up @@ -329,9 +337,10 @@ void toStringEveryParametersWithArrayMatchingStrategyNull() {
.sort(new String[] {"sort"})
.page(0)
.hitsPerPage(0)
.distinct("distinct")
.build();
String expectedToString =
"{\"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\"]}";
"{\"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\"]}";

assertThat(classToTest.getQ(), is(equalTo("This is a Test")));
assertThat(classToTest.getOffset(), is(equalTo(200)));
Expand All @@ -351,6 +360,7 @@ void toStringEveryParametersWithArrayMatchingStrategyNull() {
assertThat(classToTest.getFacets()[0], is(equalTo("facets")));
assertThat(classToTest.getSort()[0], is(equalTo("sort")));
assertThat(classToTest.getCropLength(), is(equalTo(900)));
assertThat(classToTest.getDistinct(), is(equalTo("distinct")));
assertThat(classToTest.toString(), is(equalTo(expectedToString)));
}
}

0 comments on commit a579d99

Please sign in to comment.