When rewriting a Search API query (using database backend) to order by RAND(), the facets are lost. I have found the issue, steps are as follows:
1. Query is rewritten using $db_query->orderRandom(). This adds an RAND() expression field and a sort on that field alias.
2. Search query is executed properly, results are randomised.
3. Facets are built, and populated by calling the Database::getTemporaryResultsTable() method. This method re-uses the database query, but removes all the expressions. There is still a sort added on the RAND() expression, and therefor the query will fail, resulting in empty facets.
The fix is to also remove the ORDER BY clause from the query. This seems to have no effect on the facet order, as they are sorted at a later stage anyway. I will attach a patch.
| Comment | File | Size | Author |
|---|---|---|---|
| #2 | 2923976-search_api_database_random_order_empty_facets-1.patch | 793 bytes | martijn houtman |
Comments
Comment #2
martijn houtman commentedPatch attached.
Comment #3
drunken monkeyLooks good, thanks a lot for reporting and already providing the solution!
Let's just get green light from the test bot, too, and I can commit it. (I don't think we need tests, as the bug only occurs with custom adaptions.)
Comment #5
drunken monkeyTest bot is fine with the patch, too, so: committed.
Thanks again!