Problem/Motivation

I had issues with random sort functionality in different projects.
Some results where shown twice on views with pager and views_random_seed module.

Steps to reproduce

Install search_api and views_random_seed module.
Add an search api server and index and configure a view.
Add random sort with seed to this view.

Proposed resolution

It seems, that the orderByRandom function do not allow seed values.

Issue fork search_api-3495139

Command icon 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

tobias märz created an issue. See original summary.

tobias märz’s picture

Status: Active » Needs review

tobias märz’s picture

drunken monkey made their first commit to this issue’s fork.

drunken monkey’s picture

Title: Random sort with seed not working » Support seeds for random sorting in DB backend
Version: 8.x-1.37 » 8.x-1.x-dev
Assigned: tobias märz » Unassigned
Category: Bug report » Feature request
Status: Needs review » Needs work
Issue tags: +Needs tests

Thanks for suggesting this improvement!
As the test fails show, though, your implementation is a bit rough. Instead of forcing a new property on the database query object we can just use its addMetaData() method. I updated the MR accordingly, please test/review.
I also added support for Postgres. SQLite, however, doesn’t seem to support random seeds (yet), see here.

Before I can merge this it still needs tests, though. Would you be able to add some to \Drupal\Tests\search_api_db\Kernel\BackendTest::searchWithRandom()? (We’ll just have to make sure to skip them for SQLite.)

tobias märz’s picture

I don't have much experience in the code of the SearchAPI module. I tried it, but couldn't find a way to add the metadata to the query. Can you implement the test?

tobias märz’s picture

Status: Needs work » Needs review

Thought a bit wrong, added test for random sort with seed.

drunken monkey’s picture

Status: Needs review » Fixed
Issue tags: -Needs tests

Great job, thanks a lot!
Unfortunately it’s not that easy anymore to test against other databases (after the switch to GitLab CI) but tests against MySQL pass and any problems with Postgres or SQLite should be reported on the weekend when the scheduled pipelines run.
I just did a bit of code cleanup and will now merge.
Thanks again!

drunken monkey’s picture

Status: Fixed » Closed (fixed)

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