Problem/Motivation
Elasticsearch, OpenSearch, and Solr support two options for sorting suggestions:
- Score (i.e. sort by score first, then sort by frequency)
- Frequency (i.e. sort by frequency first, then sort by score)
References:
- Elasticsearch: https://www.elastic.co/docs/reference/elasticsearch/rest-apis/search-sug... (search "sort")
- OpenSearch: https://docs.opensearch.org/latest/search-plugins/searching-data/did-you... (search "sort")
- Solr: https://solr.apache.org/guide/solr/latest/query-guide/spell-checking.htm... (search "comparatorClass")
However this module does not seem to provide a way to choose the order. As a result the "score" order is always used by default, which does not always provide the best results. In our case we have better results with the "frequency" order.
Is it possible to expose this option in the admin UI?
Proposed resolution
Attached is a proposed Merge Request and the associated patch. It basically adds a new dropdown to the Views areas:

The backend modules would then be responsible for using this value when calling their search engines API.
Since Elasticsearch, OpenSearch, and Solr support the same two options (Score & Frequency) I have hardcoded them. However, as an improvement, we could add an API to let each backend module declare which orders they support (if any).
| Comment | File | Size | Author |
|---|---|---|---|
| #5 | 3569994-5-search_api_spellcheck-sort.patch | 2.21 KB | fengtan |
| search_api_spellcheck_sort.png | 57.02 KB | fengtan |
Issue fork search_api_spellcheck-3569994
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
fengtanComment #4
fengtanComment #5
fengtanI have opened a related issue in the Elasticsearch Connector queue to support this option: #3569998: Support sorting suggestions by frequency
Comment #6
mparker17The code seems clear and makes good use of available APIs. This module doesn't have any tests yet, and this patch doesn't add any.
Testing manually with Elasticsearch, and the patch in #3569998: Support sorting suggestions by frequency, I can see
"sort":"score"change to"sort":"frequency"in the raw query sent to Elasticsearch when I change the value in the select list created in this patch.Also manual-testing, I couldn't figure out a mis-spelled search phrase that I could use which would get me multiple "did you mean" suggestions, so I wasn't able to verify the UI (but that's most likely an issue with my test data... I tried test data from both
\Drupal\Tests\search_api\Functional\ExampleContentTraitand the Devel project'sdevel_generatesub-module).Regardless, I'm going to mark this as RTBC, ideally so it can unblock #3569998: Support sorting suggestions by frequency.
It's also worth noting the same options are present in OpenSearch (i.e.: in addition to Elasticsearch and Solr). I've updated the issue summary.