Hello,

I have installation of Drupal 8.4.4 and Views 8.4.4.
I have configured a view to show my search results with a random sorting.
But the random sort is not applied, results only show up in order of date creation.
I have read this thread https://www.drupal.org/project/search_api/issues/1197538 wich at some point fixed the problem.

Can someone reproduce the bug ?

Thanks in advance for your help !

Comments

Clauce created an issue. See original summary.

drunken monkey’s picture

Which server backend are you using, Solr, database or something else?

jasonschweb’s picture

StatusFileSize
new1.48 KB

I need random DB sort for project I'm working on for Database backend, so I created this patch which works with either the standard random sort OR views_random_seed using the following patch: https://www.drupal.org/files/issues/views_random_seed_search_api.patch from here: https://www.drupal.org/project/views_random_seed/issues/2939507

views_random_seed is not required but the attached patch will only work on mysql. Hopefully this is a good starting point for getting this feature added to the module.

kbeck303’s picture

I have tested this patch (views sort using global: random field) and it works running core 8.5.5 and search_api 1.8, I also have search_api_sorts beta1 installed.

drunken monkey’s picture

Status: Active » Needs work
Issue tags: +Needs tests

Thanks a lot for the patch! That's a good start. Several things still need to be addressed, though:

  • We need Postgres and SQLite support before committing.
  • We also need automatic tests to ensure this works, and keeps working, on all three DBMSs. (Admittedly, testing whether a sort is random does seem tricky, but we at least have to test it doesn't throw any exceptions. And by just checking the order several times, I think we can also find a way that is very improbable to yield a false negative.)
  • DBMS-specific code should always live in the DatabaseCompatibility classes (\Drupal\search_api_db\DatabaseCompatibility\GenericDatabase et al.).
drunken monkey’s picture

Version: 8.x-1.6 » 8.x-1.x-dev
Component: Views integration » Database backend
basvredeling’s picture

Patch #3 is still relevant on D8.7.10

pyxio’s picture

patch does not work for drupal 8.8.1

amccune’s picture

I was able to apply the patch manually in core 8.8.6 and search_api 8.x-1.16. Working great.

upchuk’s picture

Category: Bug report » Feature request
Status: Needs work » Needs review
StatusFileSize
new2.48 KB
new6.12 KB

Alright, took a stab at this, @drunken monkey. I don't have experience with Postgres and SQLite so maybe someone can help with that.

I also moved things to the DatabaseCompatibility classes (hope it's what you meant) and wrote a test.

Changed also the status to Feature request as it was simply something that was not covered, not a bug per se.

The last submitted patch, 10: 2944371-10-test-only.patch, failed testing. View results

Status: Needs review » Needs work

The last submitted patch, 10: 2944371-10.patch, failed testing. View results

upchuk’s picture

Status: Needs work » Needs review
StatusFileSize
new2.48 KB
new6.12 KB

Ah, seems I missed the whole "partial" search configuration, corrected the patches. However, I do see the tests here run with SQLite so in the CI they might not work. Locally with Mysql it works.

The last submitted patch, 13: 2944371-13-test-only.patch, failed testing. View results

Status: Needs review » Needs work

The last submitted patch, 13: 2944371-13.patch, failed testing. View results

drunken monkey’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests
StatusFileSize
new4.65 KB
new5.33 KB

Thanks a lot for your work on this, great job! Yes, moving this code to the DatabaseCompatibilityclasses was exactly what I meant.

I also verified that it works with MySQL, but supporting SQLite and Postgres, too, shouldn’t actually be too hard. Patch attached, please test/review.
(Also moved the $this->searchWithRandom() call to the region where partial search isn’t enabled. Why complicate it with that?)

upchuk’s picture

Looks good to me. I see you added something to the Generic class, I believe that covers SQLLite. I see postgres also seem to use random() for the expression. So it's ok I guess? Test is green so clearly it works for SQLLite.

I'm not gonna RTBC as it's my initial patch, I guess someone else can take a look?

  • drunken monkey committed 64baa96 on 8.x-1.x authored by Upchuk
    Issue #2944371 by Upchuk, jasonschweb, drunken monkey: Fixed random sort...
drunken monkey’s picture

Status: Needs review » Fixed

Tests passing for all three environments and you giving your OK is good enough for me. (Especially since no-one else wanted to review over the last month.)
So: committed.
Thanks a lot again, everyone!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.