I have a view showing Solr search results. When I add "Global: Random" to the sort criteria I get the following error:

Call to undefined method SearchApiViewsQuery::add_orderby() in /views/handlers/views_handler_sort_random.inc on line 8

I am running latest dev versions of Search API, Search API Solr, Entity API, Views and CTools.

Files: 
CommentFileSizeAuthor
#37 search_api_solr-random-sort-1197538.patch2.8 KBayalon
#37 search-api-random-sort-1197538.patch1.71 KBayalon
#19 1197538-19-search_api_solr-random_sort.patch1.01 KBbecw
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch 1197538-19-search_api_solr-random_sort.patch. Unable to apply patch. See the log in the details link for more information.
[ View ]
#19 1197538-19-search_api-random_sort.patch1.31 KBbecw
PASSED: [[SimpleTest]]: [MySQL] 268 pass(es).
[ View ]
#17 1197538-17-search_api_solr-random_sort.patch458 bytesbecw
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch 1197538-17-search_api_solr-random_sort.patch. Unable to apply patch. See the log in the details link for more information.
[ View ]
#17 1197538-17-search_api-random_sort.patch1.31 KBbecw
PASSED: [[SimpleTest]]: [MySQL] 268 pass(es).
[ View ]
#13 global_random-1197538-13.patch948 bytesk4v
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch global_random-1197538-13.patch. Unable to apply patch. See the log in the details link for more information.
[ View ]
#6 global_random-1197538-6.patch918 bytesk4v
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch global_random-1197538-6_0.patch. Unable to apply patch. See the log in the details link for more information.
[ View ]
#6 search_api_global_random_1197538-6.patch1.23 KBk4v
PASSED: [[SimpleTest]]: [MySQL] 268 pass(es).
[ View ]

Comments

Status:Active» Fixed

I'd call this a bug in Views, who incorrectly label some backend-specific components as "global". There is no way in the Search API (and, therefore, also in it's Views integration) to retrieve random results.

You could write an extension to the query class which allows this, though, by getting all results and then randomly loading the data of only some of them.

So, this should most probably fixed in Views, by removing the "Global: Random" sort for non-DB backends.

Status:Fixed» Closed (works as designed)

I'm having this issue in 7.x-3.x - i'd like to randomize my search results - but I am getting this error.

Status:Active» Closed (works as designed)

Status:Closed (works as designed)» Active

I found a solution to have a random sort order for results with the solr module.

In schema.xml, there is already the neccesary field type (name="rand").

I added the line

<dynamicField name="random*" type="rand" stored="true" />

as proposed in http://realize.be/random-results-apache-solr-and-drupal

then I implemented hook_search_api_solr_query_alter and added

    $seed = rand(1, 200);
    $call_args['params']['sort'] = 'random_' . $seed . ' asc';

works great, I limit the number of displayed nodes, on every page reload I get a random set of nodes.

Wouldn't it be somehow possible to add this feature to the solr-module, so that you can select it directly in views as a sort criterion? If you point me how to do it, i would write the patch.

Status:Closed (works as designed)» Active
StatusFileSize
new1.23 KB
PASSED: [[SimpleTest]]: [MySQL] 268 pass(es).
[ View ]
new918 bytes
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch global_random-1197538-6_0.patch. Unable to apply patch. See the log in the details link for more information.
[ View ]

Here are two patches to make Global:Random work for sorting in a random order.

The first patch is for the module search_api_solr, the second for search_api.

Status:Active» Needs review

Status:Needs review» Reviewed & tested by the community

it works fine for me! - Thanks k4v - This solution seem to be currently the single chance to get a random ordering on result side.

Status:Reviewed & tested by the community» Needs work

In the patch for schema.xml it would be better to write some documentation rather than link to the website where this is originating from. I am in favour for giving credit, but it should not be the sole documentation since URLs often change, web pages disappear etc.

Should we really be storing the dynamic value? I'm going to go dig up some docs but it seems like that would give you the same random sort every time right?

Ah okay so the random call in service.inc handles keeping the results unique for each call but this bit of documentation does stored="false": http://lucene.apache.org/solr/api/org/apache/solr/schema/RandomSortField...

hmm you are right "storing" is not necessary

StatusFileSize
new948 bytes
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch global_random-1197538-13.patch. Unable to apply patch. See the log in the details link for more information.
[ View ]

so heres a new patch without the url-comment and stored="false"

Status:Needs work» Needs review

I'm getting: '400' Status: Bad Request after applying the patches. Cleared and re-indexed. Ideas?

M

Hi,

it sound like your query is to long. See #1276970: Large queries break solr search

Title:Not possible to order results randomly in ViewsRandom sort in Views (patch)
StatusFileSize
new1.31 KB
PASSED: [[SimpleTest]]: [MySQL] 268 pass(es).
[ View ]
new458 bytes
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch 1197538-17-search_api_solr-random_sort.patch. Unable to apply patch. See the log in the details link for more information.
[ View ]

@MHilliker: are you using Sarnia? I found this too. Adding the same 'search_api_random' field to the Sarnia service's getFieldNames() fixed it--I've committed that to Sarnia, so if you apply the Search API patch from this issue, you should be able to get random sorts.

Here are re-rolled versions (updated comments, patch name format) of the other two patches from earlier in this thread, to Search API and Search API Solr. I find that these allow me to do random sorting with Search API, Search API Views, and Solr.

I applied the patches in #17 but get the same result as #15. Using only SearchAPI and SearchAPISolr.

StatusFileSize
new1.31 KB
PASSED: [[SimpleTest]]: [MySQL] 268 pass(es).
[ View ]
new1.01 KB
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch 1197538-19-search_api_solr-random_sort.patch. Unable to apply patch. See the log in the details link for more information.
[ View ]

Ah, the random_* field hasn't been added to search_api_solr's schema.xml file.

Here's an updated patch for search_api_solr that includes the schema.xml snippet from comment #5 above. You'll need to put this updated file in your Solr setup, too. I've included the search_api patch again for the sake of completeness.

Thanks. Will give it a try.

#19 Worked for me!

Will this be committed?

i think the first patch fails testing because it has to be applied to the module search_api_solr, not search_api. Should I open another issue for the separate patch?

None of these patches worked for me, I get FAILED in ssh.

These patches worked great for me. As becw mentioned, the first patch is for search_api_solr and that's why it fails testing in this issue queue.

Category:bug» feature
Status:Needs review» Needs work

I admit, the current patches are very appealing in their simplicity. However, we cannot just introduce a new special sort field that service classes will have to know. Especially since the module is now stable and should therefore keep not backwards-compatible API changes to the absolutely necessary minimum.
The current approach will break all backends except Solr. A better approach would probably be to define a new "search_api_sort_random" feature, or the like, which allows random sorting via an option. Though I admit that the current approach is cleaner and more functional – we sadly can't just add features anymore.

Also, this is definitely a feature request. (And as I'm currently busy solving accumulated bugs of over half a year, it'll probably take a while till I get to it again. Sorry!)

Maybe you could add it in the next version? Or do you have a good example how to do the "feature" the right way? Thanks for the fantastic modules ;).

Status:Needs work» Needs review

I have solved the problem.

the patch for solr_api_solr not work.

i have add this line:

'search_api_random' => 'random_' . rand(1, 200),

at line 480 of this file:

search_api/solr/includes/service.inc

In this way the services views can create random sort order.

it would be to create the patch for the search_api_solr module.

pippopeppe: see #27

Status:Needs review» Needs work

Maybe you could add it in the next version? Or do you have a good example how to do the "feature" the right way?

I don't think we should generally add this as a built-in feature, since some backends might have problems implementing it and it's by no means a must-have for searches. So in my opinion, it should stay an add-on feature in later versions (D8, for example), too.

I don't really have a good example for doing exactly this kind of feature, but it would work like other features – e.g., search_api_mlt, also defined in the Search API Views module. In the add_orderby() method you check whether the index's server supports the feature and, if it does, add the random sort. (Otherwise, you'll probably have to throw an exception or at least display a warning.) Documentation for the feature has to be added in the README.txt file, and also a short description to the handbook page (after the patch was committed, of course). As the format, since the query class doesn't allow arbitrary fields for sorting, I guess you'll have to use an option you pass with the query to override the normal sorting. In my opinion, this should also support having the random sort not as the primary one, so a bit of trickery will probably be necessary there.
(On second thought, since an option would really be complicated, you could also set the sort directly by using the reference returned by &getSort().)

For Solr, just adapt the patch code to the new way of passing the random sort, and add the feature's ID to supportsFeature().

When I try to apply the patch to search API, I get

"unknown field search_api_random." within views.

Status:Needs work» Needs review

Status:Needs review» Needs work

The last submitted patch, 19: 1197538-19-search_api_solr-random_sort.patch, failed testing.

I had not time to implement the proposed solution. Attached you find a rerolled patch against the latest version of search:api and search_api_solr

Version:7.x-1.x-dev» 8.x-1.x-dev

Since this is a feature request and drunkenmonkey made it clear that 7.x-1.x is stable and should not introduce API changes I'm moving this up to the D8 chain.
If someone would be able to change the summary so it represents the ask and why, that'd be awesome.

Version:8.x-1.x-dev» 7.x-1.x-dev

As long as it doesn't change any APIs but only introduces a new feature, I'd be fine with it. I'm just waiting for a patch that does that. I also don't think it should become a fixed part of the framework in D8 – this isn't useful enough in my opinion to force all backends to (at least try to) support it.