Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ jacocoTestReport {
}

tasks.register('integrationTest', Test) {
testClassesDirs = sourceSets.test.output.classesDirs
classpath = sourceSets.test.runtimeClasspath

useJUnitPlatform {
includeTags 'integration'
}
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/com/meilisearch/sdk/IndexSearchRequest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.meilisearch.sdk;


import java.util.Map;
import com.meilisearch.sdk.model.Hybrid;
import com.meilisearch.sdk.model.MatchingStrategy;
import lombok.*;
Expand Down Expand Up @@ -41,6 +43,7 @@ public class IndexSearchRequest {
protected String distinct;
protected Hybrid hybrid;
protected Boolean retrieveVectors;
private Map<String, Object> inlineRule;

/**
* Constructor for MultiSearchRequest for building search queries with the default values:
Expand Down Expand Up @@ -111,7 +114,8 @@ public String toString() {
.putOpt("locales", this.locales)
.putOpt("distinct", this.distinct)
.putOpt("retrieveVectors", this.retrieveVectors)
.putOpt("hybrid", this.hybrid != null ? this.hybrid.toJSONObject() : null);
.putOpt("hybrid", this.hybrid != null ? this.hybrid.toJSONObject() : null)
.putOpt("inlineRule", this.inlineRule);

return jsonObject.toString();
}
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/com/meilisearch/sdk/SearchRequest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.meilisearch.sdk;


import java.util.Map;
import com.meilisearch.sdk.model.Hybrid;
import com.meilisearch.sdk.model.MatchingStrategy;
import lombok.AccessLevel;
Expand Down Expand Up @@ -47,6 +49,7 @@ public class SearchRequest {
protected Double[] vector;
protected Boolean retrieveVectors;
protected Boolean showPerformanceDetails;
private Map<String, Object> inlineRule;
/**
* Constructor for SearchRequest for building search queries with the default values: offset: 0,
* limit: 20, attributesToRetrieve: ["*"], attributesToCrop: null, cropLength: 200,
Expand Down Expand Up @@ -112,7 +115,8 @@ public String toString() {
.putOpt("distinct", this.distinct)
.putOpt("vector", this.vector)
.putOpt("retrieveVectors", this.retrieveVectors)
.putOpt("showPerformanceDetails", this.showPerformanceDetails);
.putOpt("showPerformanceDetails", this.showPerformanceDetails)
.putOpt("inlineRule", this.inlineRule);

if (this.hybrid != null) {
jsonObject.put("hybrid", this.hybrid.toJSONObject());
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/com/meilisearch/sdk/model/MultiSearchResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
/**
* Multi search response
*
* <p>https://www.meilisearch.com/docs/reference/api/multi_search#response
* <p>
* https://www.meilisearch.com/docs/reference/api/multi_search#response
*/
@Getter
@ToString
public class MultiSearchResult implements Searchable {

String indexUid;
ArrayList<HashMap<String, Object>> hits;
HashMap<String, HashMap<String, Integer>> facetDistribution;
Expand All @@ -25,5 +27,9 @@ public class MultiSearchResult implements Searchable {
HashMap<String, FacetsByIndexInfo> facetsByIndex;
HashMap<String, Object> performanceDetails;

public MultiSearchResult() {}
// Metadata returned by Meilisearch
SearchMetadata metadata;

public MultiSearchResult() {
}
}
15 changes: 15 additions & 0 deletions src/main/java/com/meilisearch/sdk/model/SearchMetadata.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.meilisearch.sdk.model;

import java.util.Map;
import lombok.Getter;
import lombok.ToString;

@Getter
@ToString
public class SearchMetadata {
private String queryUid;
private String indexUid;
private Map<String, Object> inlineRuleConditionOutcomes;

public SearchMetadata() {}
}
11 changes: 8 additions & 3 deletions src/main/java/com/meilisearch/sdk/model/SearchResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
/**
* Meilisearch search response data structure for infinite pagination
*
* @see <a href="https://www.meilisearch.com/docs/reference/api/search#response">API
* specification</a>
* @see <a href="https://www.meilisearch.com/docs/reference/api/search#response">
* API specification</a>
*/
@Getter
@ToString
public class SearchResult implements Searchable {

ArrayList<HashMap<String, Object>> hits;
Object facetDistribution;
HashMap<String, FacetRating> facetStats;
Expand All @@ -26,5 +27,9 @@ public class SearchResult implements Searchable {
HashMap<String, Object> _vectors;
HashMap<String, Object> performanceDetails;

public SearchResult() {}
// Metadata returned by Meilisearch
SearchMetadata metadata;

public SearchResult() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class SearchResultPaginated implements Searchable {
int processingTimeMs;
ArrayList<Float> queryVector;
String query;
SearchMetadata metadata;

public SearchResultPaginated() {}
}
4 changes: 4 additions & 0 deletions src/main/java/com/meilisearch/sdk/model/Searchable.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ public interface Searchable {
int getProcessingTimeMs();

String getQuery();

SearchMetadata getMetadata();


}
155 changes: 135 additions & 20 deletions src/test/java/com/meilisearch/integration/DynamicSearchRulesTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package com.meilisearch.integration;



import com.meilisearch.sdk.IndexSearchRequest;
import com.meilisearch.sdk.MultiSearchRequest;
import com.meilisearch.sdk.SearchRequest;
import com.meilisearch.sdk.model.MultiSearchResult;
import com.meilisearch.sdk.model.SearchResult;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
Expand Down Expand Up @@ -37,9 +44,9 @@ static void cleanMeilisearch() {
private DynamicSearchRule buildTestRule(String uid) {
Map<String, Object> queryCondition = Map.of("query", Map.of("isEmpty", true));
Map<String, Object> action =
Map.of(
"selector", Map.of("indexUid", "products"),
"action", Map.of("type", "pin", "position", 1));
Map.of(
"selector", Map.of("indexUid", "products"),
"action", Map.of("type", "pin", "position", 1));

return new DynamicSearchRule(uid, "Test rule", 5, true, queryCondition, List.of(action));
}
Expand All @@ -65,7 +72,7 @@ public void testUpdateDynamicSearchRule() throws MeilisearchException {

@Test
public void testCreateAndUpdateDynamicSearchRuleWithFilterCondition()
throws MeilisearchException {
throws MeilisearchException {
DynamicSearchRule rule = buildTestRule("test-filter");
rule.setConditions(buildFilterCondition("red"));

Expand All @@ -74,27 +81,27 @@ public void testCreateAndUpdateDynamicSearchRuleWithFilterCondition()

DynamicSearchRule created = client.getDynamicSearchRule("test-filter");
JSONObject createdConditions =
new JSONObject(new GsonJsonHandler().encode(created.getConditions()));
new JSONObject(new GsonJsonHandler().encode(created.getConditions()));
assertThat(
createdConditions
.getJSONObject("filter")
.getJSONObject("values")
.getString("color"),
equalTo("red"));
createdConditions
.getJSONObject("filter")
.getJSONObject("values")
.getString("color"),
equalTo("red"));

rule.setConditions(buildFilterCondition("blue"));
TaskInfo updateTask = client.updateDynamicSearchRule("test-filter", rule);
client.waitForTask(updateTask.getTaskUid());

DynamicSearchRule updated = client.getDynamicSearchRule("test-filter");
JSONObject updatedConditions =
new JSONObject(new GsonJsonHandler().encode(updated.getConditions()));
new JSONObject(new GsonJsonHandler().encode(updated.getConditions()));
assertThat(
updatedConditions
.getJSONObject("filter")
.getJSONObject("values")
.getString("color"),
equalTo("blue"));
updatedConditions
.getJSONObject("filter")
.getJSONObject("values")
.getString("color"),
equalTo("blue"));
assertThat(updated.getLastUpdatedAt(), notNullValue());
}

Expand All @@ -117,7 +124,7 @@ public void testListDynamicSearchRules() throws MeilisearchException {
client.waitForTask(task2.getTaskUid());

Results<DynamicSearchRule> results =
client.listDynamicSearchRules(new DynamicSearchRulesQuery());
client.listDynamicSearchRules(new DynamicSearchRulesQuery());

assertThat(results.getResults().length >= 2, equalTo(true));
assertThat(results.getResults()[0].getLastUpdatedAt(), notNullValue());
Expand All @@ -133,9 +140,117 @@ public void testDeleteDynamicSearchRule() throws MeilisearchException {
client.waitForTask(deleteTask.getTaskUid());

MeilisearchException exception =
org.junit.jupiter.api.Assertions.assertThrows(
MeilisearchException.class,
() -> client.getDynamicSearchRule("test-rule-delete"));
org.junit.jupiter.api.Assertions.assertThrows(
MeilisearchException.class,
() -> client.getDynamicSearchRule("test-rule-delete"));
assertThat(exception, notNullValue());
}


@Test
public void testSearchRequestWithInlineRule() throws Exception {
Map<String, Object> scaleRule =
Map.of("filter", "color = red", "weight", 1.2);

Map<String, Object> actions =
Map.of("scale", List.of(scaleRule));

Map<String, Object> inlineRule =
Map.of(
"uid", "test-inline-rule",
"actions", actions);

SearchRequest request =
SearchRequest.builder()
.q("shirt")
.inlineRule(inlineRule)
.build();

JSONObject json = new JSONObject(request.toString());

assertThat(
json.getJSONObject("inlineRule").getString("uid"),
equalTo("test-inline-rule"));

assertThat(
json.getJSONObject("inlineRule")
.getJSONObject("actions")
.getJSONArray("scale")
.getJSONObject(0)
.getString("filter"),
equalTo("color = red"));
}




@Test
public void testSearchReturnsMetadata() throws Exception {
createEmptyIndex("metadata-test");

Map<String, Object> scaleRule =
Map.of("filter", "color = red", "weight", 1.2);

Map<String, Object> actions =
Map.of("scale", List.of(scaleRule));

Map<String, Object> inlineRule =
Map.of("uid", "test-inline-rule", "actions", actions);

var result = client.index("metadata-test").search(
SearchRequest.builder()
.q("shirt")
.inlineRule(inlineRule)
.build()
);

assertThat(result, notNullValue());
assertThat(result.getMetadata(), notNullValue());
assertThat(
result.getMetadata().getInlineRuleConditionOutcomes(),
notNullValue()
);
}




@Test
public void testMultiSearchReturnsMetadata() throws Exception {
{
createEmptyIndex("multi-metadata-test");


Map<String, Object> scaleRule =
Map.of("filter", "color = red", "weight", 1.2);

Map<String, Object> actions =
Map.of("scale", List.of(scaleRule));

Map<String, Object> inlineRule =
Map.of("uid", "test-inline-rule", "actions", actions);

IndexSearchRequest searchRequest =
IndexSearchRequest.builder()
.indexUid("multi-metadata-test")
.q("shirt")
.inlineRule(inlineRule)
.build();




MultiSearchRequest multiSearchRequest =
new MultiSearchRequest().addQuery(searchRequest);

Results<MultiSearchResult> result =
client.multiSearch(multiSearchRequest);

assertThat(result, notNullValue());
assertThat(result.getResults().length, equalTo(1));
assertThat(
result.getResults()[0].getMetadata().getInlineRuleConditionOutcomes(),
notNullValue());
}
}
}
Loading