Skip to content

Commit fe6e82c

Browse files
ben-kalmusFluf22
andauthored
feat(specs): add sortBy query param and sortingStrategy (#5686)
Co-authored-by: Thomas Raffray <Fluf22@users.noreply.github.com>
1 parent 66342a2 commit fe6e82c

8 files changed

Lines changed: 179 additions & 3 deletions

File tree

specs/composition/common/params/Composition.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,21 @@ enableABTest:
110110
default: true
111111
x-categories:
112112
- Advanced
113+
114+
# #######################
115+
# ### Category SortBy ###
116+
# #######################
117+
118+
sortBy:
119+
type: string
120+
description: |
121+
Indicates which sorting strategy to apply for the request.
122+
The value must match one of the labels defined in the "sortingStrategy" mapping. For example, "Price (asc)", see Upsert Composition.
123+
At runtime, this label is used to look up the corresponding index or replica configured in "sortingStrategy", and the query is executed using that index instead of main's.
124+
125+
In addition to "sortingStrategy", this parameter is also used to apply a matching Composition Rule that contains a condition defined to trigger on "sortBy", see Composition Rules.
126+
127+
If no value is provided or an invalid value, no sorting strategy is applied.
128+
default: ''
129+
example:
130+
'Price (asc)'

specs/composition/common/schemas/components/Composition.yml

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,26 @@ composition:
1414
example: 'my lovely crafted composition that is used for X purpose'
1515
behavior:
1616
$ref: './CompositionBehavior.yml#/compositionBehavior'
17+
sortingStrategy:
18+
$ref: '#/sortingStrategy'
19+
1720
required:
1821
- objectID
1922
- behavior
20-
- name
23+
- name
24+
25+
sortingStrategy:
26+
type: object
27+
description: |
28+
A mapping of sorting labels to the indices (or replicas) that implement those sorting rules. The sorting indices MUST be related to the associated main targeted index in the composition.
29+
Each key is the label your frontend sends at runtime (for example, "Price (asc)"), and each value is the name of the index that should be queried when that label is selected.
30+
31+
When a request includes a "sortBy" parameter, the platform looks up the corresponding index in this mapping and uses it to execute the query. The main targeted index is replaced
32+
with the sorting strategy index it is mapped to.
33+
34+
Up to 20 sorting strategies can be defined.
35+
additionalProperties:
36+
type: string
37+
example:
38+
'Price (asc)': 'products-low-to-high'
39+
'Price (desc)': 'products-high-to-low'

specs/composition/common/schemas/requestBodies/RunParams.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,13 @@ params:
5353
$ref: '../../params/Search.yml#/page'
5454
query:
5555
$ref: '../../params/Search.yml#/query'
56-
relevancyStrictness:
57-
$ref: '../../params/Search.yml#/relevancyStrictness'
5856
queryLanguages:
5957
$ref: '../../params/Search.yml#/queryLanguages'
58+
relevancyStrictness:
59+
$ref: '../../params/Search.yml#/relevancyStrictness'
6060
ruleContexts:
6161
$ref: '../../params/Composition.yml#/ruleContexts'
62+
sortBy:
63+
$ref: '../../params/Composition.yml#/sortBy'
6264
userToken:
6365
$ref: '../../params/Search.yml#/userToken'
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
post:
2+
tags:
3+
- Compositions
4+
operationId: updateSortingStrategyComposition
5+
x-acl:
6+
- editSettings
7+
summary: Set or update the "sortingStrategy" configuration for an existing composition
8+
description: |
9+
Updates the "sortingStrategy" field of an existing composition.
10+
This endpoint allows you to create a new sorting strategy mapping or replace the currently configured one.
11+
The provided sorting indices MUST be associated indices or replicas of the main targeted index.
12+
13+
WARNING: This endpoint cannot validate if the sort index is related to the composition's main index.
14+
Validation will fail at runtime if the index you updated is not related!
15+
16+
The update is applied to the specified composition within the current Algolia application and returns a taskID that can be used to track the operation’s completion.
17+
parameters:
18+
- $ref: '../urlParams.yml#/compositionID'
19+
requestBody:
20+
required: true
21+
content:
22+
application/json:
23+
schema:
24+
$ref: '../../common/schemas/components/Composition.yml#/sortingStrategy'
25+
responses:
26+
'200':
27+
description: OK
28+
content:
29+
application/json:
30+
schema:
31+
title: taskIDResponse
32+
type: object
33+
additionalProperties: false
34+
properties:
35+
taskID:
36+
$ref: '../../../common/responses/common.yml#/taskID'
37+
required:
38+
- taskID
39+
'400':
40+
$ref: '../../../common/responses/BadRequest.yml'
41+
'402':
42+
$ref: '../../../common/responses/FeatureNotEnabled.yml'
43+
'403':
44+
$ref: '../../../common/responses/MethodNotAllowed.yml'
45+
'404':
46+
$ref: '../../../common/responses/CompositionNotFound.yml'

specs/composition/spec.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ paths:
136136
$ref: 'paths/compositions/composition.yml'
137137
/1/compositions/*/batch:
138138
$ref: 'paths/compositions/batch.yml'
139+
/1/compositions/{compositionID}/sortingStrategy:
140+
$ref: 'paths/compositions/updateSortingStrategy.yml'
139141

140142
# ##########################################
141143
# ### Manage Composition Rules Endpoints ###

tests/CTS/requests/composition/putComposition.json

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,5 +361,53 @@
361361
}
362362
}
363363
}
364+
},
365+
366+
{
367+
"parameters": {
368+
"compositionID": "my-compo",
369+
"composition": {
370+
"objectID": "my-compo",
371+
"name": "my composition",
372+
"sortingStrategy": {
373+
"Price-asc": "products-low-to-high",
374+
"Price-desc": "products-high-to-low"
375+
},
376+
"behavior": {
377+
"injection": {
378+
"main": {
379+
"source": {
380+
"search": {
381+
"index": "products"
382+
}
383+
}
384+
}
385+
}
386+
}
387+
}
388+
},
389+
"request": {
390+
"path": "/1/compositions/my-compo",
391+
"method": "PUT",
392+
"body": {
393+
"objectID": "my-compo",
394+
"name": "my composition",
395+
"sortingStrategy": {
396+
"Price-asc": "products-low-to-high",
397+
"Price-desc": "products-high-to-low"
398+
},
399+
"behavior": {
400+
"injection": {
401+
"main": {
402+
"source": {
403+
"search": {
404+
"index": "products"
405+
}
406+
}
407+
}
408+
}
409+
}
410+
}
411+
}
364412
}
365413
]

tests/CTS/requests/composition/search.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,5 +79,27 @@
7979
}
8080
}
8181
}
82+
},
83+
84+
{
85+
"parameters": {
86+
"compositionID": "foo",
87+
"requestBody": {
88+
"params": {
89+
"query": "batman",
90+
"sortBy": "Price (asc)"
91+
}
92+
}
93+
},
94+
"request": {
95+
"path": "/1/compositions/foo/run",
96+
"method": "POST",
97+
"body": {
98+
"params": {
99+
"query": "batman",
100+
"sortBy": "Price (asc)"
101+
}
102+
}
103+
}
82104
}
83105
]
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[
2+
{
3+
"parameters": {
4+
"compositionID": "my-compo",
5+
"requestBody": {
6+
"Price-asc": "products-low-to-high",
7+
"Price-desc": "products-high-to-low"
8+
}
9+
},
10+
"request": {
11+
"path": "/1/compositions/my-compo/sortingStrategy",
12+
"method": "POST",
13+
"body": {
14+
"Price-asc": "products-low-to-high",
15+
"Price-desc": "products-high-to-low"
16+
}
17+
}
18+
}
19+
]

0 commit comments

Comments
 (0)