Sorting
In the QSC it is possible to configure multiple sort parameters.
-
default Sort: the default sort. Is used when no sort option is specified in the request. The default sort is specified by a value of the sort options.
-
text: the text that can be displayed in the frontend
-
value: the sort value - The value is used in the API as the key for the sorting
-
criteria: elasticsearch sorting syntax - https://www.elastic.co/guide/en/elasticsearch/reference/current/sort-search-results.html
- Examples:
["_score"]
[{ "price" : {"order" : "desc"}}]
[{ "price" : {"order" : "asc"}}]
[{"title.sort":{"order":"desc"}}]
[{"title.sort":{"order":"asc"}}]
-
[{
"_script": {
"script": {
"source": "return doc['accountIdsSort'].stream().filter(x -> x.startsWith('{accountId=' + params.accountId)).findFirst().orElse('a');",
"params": {
"accountId": "$query.f.accountIds"
}
},
"type": "string",
"order": "desc"
}
}]
Sort by last purchased date for a product
-
the user can filter for the last purchased products
-
when the filter is selected the product must be sorted by the last purchased date
-
every product contains a list of all accountId's that purchased this product and the last date when it was purchased
{
"accountIdsSort": [
{
"accountId": "0010O33301vkfiyQBB",
"lastOrderDate": "2021-09-13T11:39:56Z"
},
{
"accountId": "0010444401vkhaaQYY",
"lastOrderDate": "2021-10-03T22:00:00Z"
},
{
"accountId": "0055555002Qhrh5QZZ",
"lastOrderDate": "2021-10-05T13:20:46Z"
}
]
}
- every item of the array is serialized to a json string and indexed in a multivalue field
- for filtering a prefix filer is used
- for sorting the following script is used
[{
"_script": {
"script": {
"source": "return doc['accountIdsSort'].stream().filter(x -> x.startsWith('{accountId=' + params.accountId)).findFirst().orElse('a');",
"params": {
"accountId": "$query.f.accountIds"
}
},
"type": "string",
"order": "desc"
}
}]
- this feature is used by the product search of Alexander Bürkle (zuletzt gekauft)
- in the blog post the technical details are discussed -> TODO write blog post
Sort by a list of filtered ids
- $materialNumberList is a array of ids that are used in a filter
- this mapping from the filter values to this variable is done in custom Search Filter
- this feature is used in the MT-Finder of the Telekom MagentaInfos Search
- in the blog post the technical details are discussed
def count = params.ids.size();
def materialNumber = doc['materialNumber.keyword'].value;
for (def i = 0; i < count; i++) {
if (materialNumber == params.ids[i]) {
return count - i;
}
}
[
{
"_script": {
"type": "number",
"script": {
"lang": "painless",
"source": "def count = params.ids.size(); def materialNumber = doc['materialNumber.keyword'].value; for (def i = 0; i < count; i++) { if (materialNumber == params.ids[i]) { return count - i; }}",
"params": {
"ids": "$materialNumberList"
}
},
"order": "desc"
}
}
]
Category Sorting Options
It is also possible to specify special sorting options for each category.
- specify which
sortOptions
can be managed by the user. This sort options must be defined in the used search.
{
"tenant": {
"code": "quasiris",
"name": "quasiris"
},
"code": "navigation",
"name": "navigation",
"type": "category",
"createdAt": "2021-11-11T17:08:54Z",
"changedAt": "2023-03-04T12:38:41Z",
"category": {},
"sortOptions": [
"lastUpdate",
"creationDate_asc",
"creationDate_desc"
]
}
- in the category options the admin can select, which searches the user can see in the frontend
- the first option is the default, if no sort option is defined in the request