Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Companion issue with #1197538: Random sort in Views, adding support for a random sort in views.
Comment | File | Size | Author |
---|---|---|---|
#20 | 2313591-20_random_sort_d8.patch | 2.09 KB | drunken monkey |
Comments
Comment #1
nadavoid CreditAttribution: nadavoid commentedReroll of https://www.drupal.org/files/issues/search_api_solr-random-sort-1197538.... from #1197538-37: Random sort in Views
Comment #2
drunken monkeySince we now use common configurations for all Drupal Solr modules, the config changes would first have to be approved there.
And also, of course, we'd first need a viable patch in the Search API issue. But in principle it looks good.
Comment #3
drunken monkeyComment #4
thePanz CreditAttribution: thePanz at Liip for FREITAG lab. AG commentedPatch attached (see related issue and comments here: #1197538-63: Random sort in Views).
Removed store="false" edits from schema.xml, the field is never used to STORE data: it is used as a "virtual field" just at query time.
Comment #5
drunken monkeyHere, too: thanks, good work! Apart from needing a feature, as said in the other issue, I see these minor style problems:
Line length, capitalization ("Solr") and grammar. Also, our schema provides the field, not Solr itself.
I'd use
mt_rand()
and a higher maximum value.Comment #6
drunken monkeyComment #7
thePanz CreditAttribution: thePanz at Liip for FREITAG lab. AG commentedUpdated patch.
Regarding the "feature" implementation, could you guide me to such implementation? (a few guidelines on IRC could be enough ) :)
Comment #8
thePanz CreditAttribution: thePanz at Liip for FREITAG lab. AG commentedAdding the "Random sort" as a SearchAPI feature.
Comment #9
thePanz CreditAttribution: thePanz at Liip for FREITAG lab. AG commentedComment #10
drunken monkeyVery good, thanks!
As soon as the other issue is resolved, I think we can commit this (after adapting it, if necessary, of course).
Just one question: Why do we want to keep the field name length below 10?
Comment #11
thePanz CreditAttribution: thePanz at Liip for FREITAG lab. AG commentedOk, thanks!
The "keep the field name under 10" is just to avoid the Solr query length to reach the maximum limit when multiple fields and parameters are used (I had some experiences of that in other projects, but not related to Drupal).
I know that it could be a rare case, but I also think that 1k different seeds (1-999) provides good randomness! :)
Comment #12
drunken monkeyFor most sites, surely. But for sites with, e.g., a million nodes, this will mean that some nodes will never be shown – and with enough visits, that might be a realistic problem.
Also, even with a much lower node count, too few different values means that some nodes will be shown much more than others – and when I, e.g., think of advertisements, that also might be a problem.
In conclusion, saving a few characters towards the total query maximum is no sufficient reason to be stingy here. Please use a higher value, or even a call without parameters (it seems that the number will then have at most ten digits, which is also no danger for the query length).
Comment #13
thePanz CreditAttribution: thePanz at Liip for FREITAG lab. AG commentedSure, let's then use the mt_rand() without parameters to get the full-length random value.
New patch attached.
Comment #14
thePanz CreditAttribution: thePanz at Liip for FREITAG lab. AG commentedNew patch attached, as aligned with #1197538-80: Random sort in Views
Comment #15
joshtaylor CreditAttribution: joshtaylor at Josh Taylor Development for Bonsai Bookings commentedVery cool, thePanz - this adds random sorting!
Also nice that you can add something like:
$query->setOption('search_api_random_sort', ['seed' => 42]);
For the seed.
Comment #16
joshtaylor CreditAttribution: joshtaylor at Josh Taylor Development for Bonsai Bookings commentedSeems I had a crucial bit of code commented out, woops:
SearchApiException: Trying to sort on unknown field search_api_random. in SearchApiQuery->sort() (line 590 of search_api/includes/query.inc).
The website encountered an unexpected error. Please try again later.
Comment #18
drunken monkey@ joshtaylor: Did you also apply the patch #1197538-80: Random sort in Views to the Search API? For me, everything works fine.
Anyways, looks good now, thanks again!
Committed.
Comment #20
drunken monkeyPorted to Drupal 8, please test/review (in conjunction with #1197538-89: Random sort in Views)!
Comment #22
drunken monkeyCommitted.