Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 6 additions & 1 deletion src/main/java/com/meilisearch/sdk/IndexSearchRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.meilisearch.sdk.model.Hybrid;
import com.meilisearch.sdk.model.MatchingStrategy;
import com.meilisearch.sdk.model.Personalize;
import lombok.*;
import lombok.experimental.Accessors;
import org.json.JSONObject;
Expand Down Expand Up @@ -41,6 +42,7 @@ public class IndexSearchRequest {
protected String distinct;
protected Hybrid hybrid;
protected Boolean retrieveVectors;
protected Personalize personalize;

/**
* Constructor for MultiSearchRequest for building search queries with the default values:
Expand Down Expand Up @@ -111,7 +113,10 @@ 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(
"personalize",
this.personalize != null ? this.personalize.toJSONObject() : null);

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

import com.meilisearch.sdk.model.Hybrid;
import com.meilisearch.sdk.model.MatchingStrategy;
import com.meilisearch.sdk.model.Personalize;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
Expand Down Expand Up @@ -46,6 +47,7 @@ public class SearchRequest {
protected Hybrid hybrid;
protected Double[] vector;
protected Boolean retrieveVectors;
protected Personalize personalize;
/**
* Constructor for SearchRequest for building search queries with the default values: offset: 0,
* limit: 20, attributesToRetrieve: ["*"], attributesToCrop: null, cropLength: 200,
Expand Down Expand Up @@ -116,6 +118,10 @@ public String toString() {
jsonObject.put("hybrid", this.hybrid.toJSONObject());
}

if (this.personalize != null) {
jsonObject.put("personalize", this.personalize.toJSONObject());
}

return jsonObject.toString();
}
}
43 changes: 43 additions & 0 deletions src/main/java/com/meilisearch/sdk/model/Personalize.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.meilisearch.sdk.model;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.json.JSONObject;

/** Personalization configuration for personalized search (experimental) */
@Builder
@AllArgsConstructor(access = AccessLevel.PACKAGE)
@NoArgsConstructor(access = AccessLevel.PACKAGE)
@Getter
@Setter
@Accessors(chain = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Personalize {
/** Natural-language description of the user's context used to personalize the search */
private String userContext;

/**
* Method that returns the JSON representation of the Personalize object
*
* @return JSONObject representation of the Personalize object
*/
public JSONObject toJSONObject() {
return new JSONObject().putOpt("userContext", this.userContext);
}

/**
* Method that returns the JSON String of the Personalize object
*
* @return JSON String of the Personalize object
*/
@Override
public String toString() {
return toJSONObject().toString();
}
}
25 changes: 25 additions & 0 deletions src/test/java/com/meilisearch/sdk/IndexSearchRequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import static org.hamcrest.Matchers.is;

import com.meilisearch.sdk.model.Hybrid;
import com.meilisearch.sdk.model.Personalize;
import org.json.JSONObject;
import org.junit.jupiter.api.Test;

class IndexSearchRequestTest {
Expand Down Expand Up @@ -54,4 +56,27 @@ void toStringWithHybridOnlyEmbedder() {
String expected = "{\"q\":\"This is a Test\",\"hybrid\":{\"embedder\":\"default\"}}";
assertThat(classToTest.toString(), is(equalTo(expected)));
}

@Test
void toStringWithPersonalizeUsingBuilder() {
IndexSearchRequest classToTest =
IndexSearchRequest.builder()
.q("This is a Test")
.personalize(
Personalize.builder()
.userContext("The user prefers sci-fi movies")
.build())
.build();

JSONObject json = new JSONObject(classToTest.toString());
assertThat(json.getString("q"), is(equalTo("This is a Test")));
assertThat(
json.getJSONObject("personalize").getString("userContext"),
is(equalTo("The user prefers sci-fi movies")));

// Verify getters
assertThat(
classToTest.getPersonalize().getUserContext(),
is(equalTo("The user prefers sci-fi movies")));
}
}
24 changes: 24 additions & 0 deletions src/test/java/com/meilisearch/sdk/SearchRequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import com.meilisearch.sdk.model.Hybrid;
import com.meilisearch.sdk.model.MatchingStrategy;
import com.meilisearch.sdk.model.Personalize;
import org.json.JSONObject;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -419,4 +420,27 @@ void toStringWithRetrieveVectors() {
JSONObject json = new JSONObject(result);
assertThat(json.getBoolean("retrieveVectors"), is(true));
}

@Test
void toStringWithPersonalizeUsingBuilder() {
SearchRequest classToTest =
SearchRequest.builder()
.q("This is a Test")
.personalize(
Personalize.builder()
.userContext("The user prefers sci-fi movies")
.build())
.build();

JSONObject json = new JSONObject(classToTest.toString());
assertThat(json.getString("q"), is(equalTo("This is a Test")));
assertThat(
json.getJSONObject("personalize").getString("userContext"),
is(equalTo("The user prefers sci-fi movies")));

// Verify getters
assertThat(
classToTest.getPersonalize().getUserContext(),
is(equalTo("The user prefers sci-fi movies")));
}
}