-
-
Notifications
You must be signed in to change notification settings - Fork 35
Description
sapi:autocomplete function is a part of the search.xql module.
Retrieving data can be modified or implemented in a custom autocomplete($doc, $type, $q) function.
Unfortunately, the returned results do not keep their order, but are sorted according to the following code in the sapi:autocomplete function:
array {
for $item in $items
group by $item
return
map {
"text": $item,
"value": $item
}
}It is useful for the user experience if the suggestions are ordered in the expected way, i.e. alphabetically in the case of text or numerically in the case of numbers.
It seems that group by in the mentioned code is used to keep unique values from the returned set, but it also changes the sorting of items in the $items variable.
I suggest three ways how to set expected order of suggestions:
1. Simple (it assumes that returned values are sorted):
for $item in distinct-values($items) (: instead of group by :)2. Advanced (it assumes that all suggestions will be sorted in the same way):
In search.xql:
for $item in
sort(distinct-values($items), $config:default-autocomplete-collation)In config.xqm:
declare variable $config:default-autocomplete-collation := "http://www.w3.org/2013/collation/UCA?lang=cs";3. Customized (with the greatest degree of control over sorting):
In search.xql:
let $language := request:get-parameter("language", ())
...
for $item in config:autocomplete-sort($items, $type, $language)In config.xqm:
declare variable $config:default-autocomplete-collation := "http://www.w3.org/2013/collation/UCA?lang=cs";
declare function config:autocomplete-sort($items as item()*, $field as xs:string, $language as xs:string?) {
let $collation := if(empty($language))
then $config:default-autocomplete-collation
else "http://www.w3.org/2013/collation/UCA?lang=" || $language
let $distinct := for $item in $items
group by $item
return $item
return switch($field)
case "date"
return for $item in $distinct
order by xs:date($item)
return $item
case "def-en"
case "def-de"
case "def-la"
return sort($distinct, "?lang=" || substring-after($field, 'def-') )
default
return sort($distinct, $collation)
};