Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort order of the autocomplete function #234

Open
daliboris opened this issue Sep 9, 2024 · 0 comments
Open

Sort order of the autocomplete function #234

daliboris opened this issue Sep 9, 2024 · 0 comments

Comments

@daliboris
Copy link
Contributor

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)
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant