Skip to content

Commit 213725e

Browse files
authored
chore(recommend): requests as list of queries (#648)
1 parent 16bc11d commit 213725e

File tree

3 files changed

+51
-22
lines changed

3 files changed

+51
-22
lines changed

src/main/scala/algolia/definitions/GetRecommendationDefinition.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import org.json4s.Formats
3131
import org.json4s.native.Serialization._
3232

3333
case class GetRecommendationDefinition(
34-
query: RecommendationsOptions,
34+
queries: List[RecommendationsOptions],
3535
requestOptions: Option[RequestOptions] = None
3636
)(implicit val formats: Formats)
3737
extends Definition {
@@ -44,10 +44,11 @@ case class GetRecommendationDefinition(
4444
copy(requestOptions = Some(requestOptions))
4545

4646
override private[algolia] def build(): HttpPayload = {
47+
val requests = Map("requests" -> queries)
4748
HttpPayload(
4849
POST,
4950
Seq("1", "indexes", "*", "recommendations"),
50-
body = Some(write(query)),
51+
body = Some(write(requests)),
5152
isSearch = true,
5253
requestOptions = requestOptions
5354
)

src/main/scala/algolia/dsl/GetDsl.scala

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,15 @@ trait GetDsl {
9595
GetSettingsDictionaryDefinition()
9696

9797
def recommendations(
98-
query: RecommendationsQuery
98+
query: List[RecommendationsQuery]
9999
): GetRecommendationDefinition = GetRecommendationDefinition(query)
100100

101101
def relatedProducts(
102-
query: RelatedProductsQuery
102+
query: List[RelatedProductsQuery]
103103
): GetRecommendationDefinition = GetRecommendationDefinition(query)
104104

105105
def frequentlyBoughtTogether(
106-
query: FrequentlyBoughtTogetherQuery
106+
query: List[FrequentlyBoughtTogetherQuery]
107107
): GetRecommendationDefinition = GetRecommendationDefinition(query)
108108
}
109109

@@ -201,4 +201,19 @@ trait GetDsl {
201201
client.request[PersonalizationProfileResponse](query.build())
202202
}
203203
}
204+
205+
implicit object GetRecommendationDefinitionExecutable
206+
extends Executable[
207+
GetRecommendationDefinition,
208+
SearchResult
209+
] {
210+
override def apply(
211+
client: AlgoliaClient,
212+
query: GetRecommendationDefinition
213+
)(
214+
implicit executor: ExecutionContext
215+
): Future[SearchResult] = {
216+
client.request[SearchResult](query.build())
217+
}
218+
}
204219
}

src/test/scala/algolia/dsl/RecommendTest.scala

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,27 @@ class RecommendTest extends AlgoliaTest {
4040
describe("test recommend payload") {
4141

4242
it("should produce valid recommendation query payload") {
43-
(get recommendations RecommendationsQuery(
43+
val queryRelatedProducts = RecommendationsQuery(
44+
indexName = "products",
45+
model = "related-products",
46+
objectID = "B018APC4LE",
47+
maxRecommendations = Some(10),
48+
queryParameters = Some(Query(attributesToRetrieve = Some(Seq("*"))))
49+
)
50+
val queryBoughtTogether = RecommendationsQuery(
4451
indexName = "products",
4552
model = "bought-together",
4653
objectID = "B018APC4LE",
4754
maxRecommendations = Some(10),
4855
queryParameters = Some(Query(attributesToRetrieve = Some(Seq("*"))))
49-
)).build() should be(
56+
)
57+
(get recommendations List(queryRelatedProducts, queryBoughtTogether))
58+
.build() should be(
5059
HttpPayload(
5160
POST,
5261
Seq("1", "indexes", "*", "recommendations"),
5362
body = Some(
54-
"{\"indexName\":\"products\",\"model\":\"bought-together\",\"objectID\":\"B018APC4LE\",\"threshold\":0,\"maxRecommendations\":10,\"queryParameters\":{\"attributesToRetrieve\":[\"*\"]}}"
63+
"{\"requests\":[{\"indexName\":\"products\",\"model\":\"related-products\",\"objectID\":\"B018APC4LE\",\"threshold\":0,\"maxRecommendations\":10,\"queryParameters\":{\"attributesToRetrieve\":[\"*\"]}},{\"indexName\":\"products\",\"model\":\"bought-together\",\"objectID\":\"B018APC4LE\",\"threshold\":0,\"maxRecommendations\":10,\"queryParameters\":{\"attributesToRetrieve\":[\"*\"]}}]}"
5564
),
5665
isSearch = true,
5766
requestOptions = None
@@ -60,18 +69,20 @@ class RecommendTest extends AlgoliaTest {
6069
}
6170

6271
it("should produce valid related products query payload") {
63-
(get relatedProducts RelatedProductsQuery(
64-
indexName = "products",
65-
objectID = "B018APC4LE",
66-
threshold = 10,
67-
maxRecommendations = Some(10),
68-
queryParameters = Some(Query(attributesToRetrieve = Some(Seq("*"))))
72+
(get relatedProducts List(
73+
RelatedProductsQuery(
74+
indexName = "products",
75+
objectID = "B018APC4LE",
76+
threshold = 10,
77+
maxRecommendations = Some(10),
78+
queryParameters = Some(Query(attributesToRetrieve = Some(Seq("*"))))
79+
)
6980
)).build() should be(
7081
HttpPayload(
7182
POST,
7283
Seq("1", "indexes", "*", "recommendations"),
7384
body = Some(
74-
"{\"indexName\":\"products\",\"objectID\":\"B018APC4LE\",\"threshold\":10,\"maxRecommendations\":10,\"queryParameters\":{\"attributesToRetrieve\":[\"*\"]},\"model\":\"related-products\"}"
85+
"{\"requests\":[{\"indexName\":\"products\",\"objectID\":\"B018APC4LE\",\"threshold\":10,\"maxRecommendations\":10,\"queryParameters\":{\"attributesToRetrieve\":[\"*\"]},\"model\":\"related-products\"}]}"
7586
),
7687
isSearch = true,
7788
requestOptions = None
@@ -80,18 +91,20 @@ class RecommendTest extends AlgoliaTest {
8091
}
8192

8293
it("should produce valid frequently bought together query payload") {
83-
(get frequentlyBoughtTogether FrequentlyBoughtTogetherQuery(
84-
indexName = "products",
85-
objectID = "B018APC4LE",
86-
threshold = 10,
87-
maxRecommendations = Some(10),
88-
queryParameters = Some(Query(attributesToRetrieve = Some(Seq("*"))))
94+
(get frequentlyBoughtTogether List(
95+
FrequentlyBoughtTogetherQuery(
96+
indexName = "products",
97+
objectID = "B018APC4LE",
98+
threshold = 10,
99+
maxRecommendations = Some(10),
100+
queryParameters = Some(Query(attributesToRetrieve = Some(Seq("*"))))
101+
)
89102
)).build() should be(
90103
HttpPayload(
91104
POST,
92105
Seq("1", "indexes", "*", "recommendations"),
93106
body = Some(
94-
"{\"indexName\":\"products\",\"objectID\":\"B018APC4LE\",\"threshold\":10,\"maxRecommendations\":10,\"queryParameters\":{\"attributesToRetrieve\":[\"*\"]},\"model\":\"bought-together\"}"
107+
"{\"requests\":[{\"indexName\":\"products\",\"objectID\":\"B018APC4LE\",\"threshold\":10,\"maxRecommendations\":10,\"queryParameters\":{\"attributesToRetrieve\":[\"*\"]},\"model\":\"bought-together\"}]}"
95108
),
96109
isSearch = true,
97110
requestOptions = None

0 commit comments

Comments
 (0)