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
#50 search_api_random_sort-1197538-50.patch1.3 KBElvar
#50 search_api_solr-random-sort-1197538-50.patch1.48 KBElvar
#40 search_api_random_sort-1197538-40.patch1.48 KBnadavoid
PASSED: [[SimpleTest]]: [MySQL] 370 pass(es).
[ View ]
#37 search_api_solr-random-sort-1197538.patch2.8 KBayalon
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch search_api_solr-random-sort-1197538.patch. Unable to apply patch. See the log in the details link for more information.
[ View ]
#37 search-api-random-sort-1197538.patch1.71 KBayalon
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch search-api-random-sort-1197538.patch. Unable to apply patch. See the log in the details link for more information.
[ View ]
#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

drunken monkey’s picture

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.

drunken monkey’s picture

Status:Fixed» Closed (works as designed)
stopshinal’s picture

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

k4v’s picture

Status:Active» Closed (works as designed)
k4v’s picture

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.

k4v’s picture

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.

k4v’s picture

Status:Active» Needs review
Blackice2999’s picture

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.

pfrenssen’s picture

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.

drewish’s picture

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?

drewish’s picture

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...

Blackice2999’s picture

hmm you are right "storing" is not necessary

k4v’s picture

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"

k4v’s picture

Status:Needs work» Needs review
MHilliker’s picture

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

M

Blackice2999’s picture

Hi,

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

becw’s picture

Title:Not possible to order results randomly in Views» Random 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.

kevinquillen’s picture

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

becw’s picture

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.

kevinquillen’s picture

Thanks. Will give it a try.

kevinquillen’s picture

#19 Worked for me!

BeaPower’s picture

Will this be committed?

BeaPower’s picture

BeaPower’s picture

k4v’s picture

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?

BeaPower’s picture

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

JStarcher’s picture

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.

drunken monkey’s picture

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!)

k4v’s picture

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 ;).

pippopeppe’s picture

Status:Needs work» Needs review
pippopeppe’s picture

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.

k4v’s picture

pippopeppe: see #27

drunken monkey’s picture

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().

Exploratus’s picture

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

"unknown field search_api_random." within views.

alex_qwe’s picture

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.

ayalon’s picture

Issue summary:View changes
StatusFileSize
new1.71 KB
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch search-api-random-sort-1197538.patch. Unable to apply patch. See the log in the details link for more information.
[ View ]
new2.8 KB
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch search_api_solr-random-sort-1197538.patch. Unable to apply patch. See the log in the details link for more information.
[ View ]

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

Nick_vh’s picture

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.

drunken monkey’s picture

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.

nadavoid’s picture

Related issues:+#2313591: Random sort in views
StatusFileSize
new1.48 KB
PASSED: [[SimpleTest]]: [MySQL] 370 pass(es).
[ View ]

Rerolled the first patch from # 37. Posted a rerolled version the 2nd patch to a new issue in the search_api_solr queue. #2313591-1: Random sort in views

nadavoid’s picture

Status:Needs work» Needs review

The last submitted patch, 37: search-api-random-sort-1197538.patch, failed testing.

The last submitted patch, 37: search_api_solr-random-sort-1197538.patch, failed testing.

drunken monkey’s picture

Status:Needs review» Needs work
Related issues:-#2313591: Random sort in views

This should still use a feature, not a hard-coded field.
Also, #2313591: Random sort in views already references this issue as its parent, we don't need to add a reference back.

dragon658’s picture

NOTE FOR ORDINARY PEOPLE WHO JUST WANT TO ADD RANDROM SORT ORDER:

I have solved the problem and successfuly added random sort order for search api on my site.
My steps:

1. I have applied #37 patch to search_api_solr module.
2. I have applied #40 patch to search_api module.

jofdesign’s picture

Hi,

I have the same issue with Elastic Search ; I'm not using a community module as a connector.
How can I solve this ?

Thank you.

kervi’s picture

also, that about search_Api_db?

wutu’s picture

#45 working for me

drunken monkey’s picture

I have the same issue with Elastic Search ; I'm not using a community module as a connector.
How can I solve this ?

Apply the patch from #40 to the Search API, then adapt your service class code to look for sorts on the search_api_random field and add a random sort in that case. As to how to do that with Elastic Search: no idea, search on the web.

Elvar’s picture

I rerolled #40's patch against develop.

Also i rerolled #37 search_api_solr patch against 1.6

Nodles’s picture

Hi!

I patched the search api module with the #40 patch, but it didnt work for me :(
any suggestion? I not use the apache sorl module to patch it

drunken monkey’s picture

I not use the apache sorl module to patch it

Sorry, I don't understand that.
If you aren't using the Search API Solr Search module, this won't work for you, you'd have to implement it again for the backend you're using.
If you are using the Search API Solr Search module, then you need to patch it for this to work.

drunken monkey’s picture

Title:Random sort in Views (patch)» Random sort in Views
tasoss’s picture

What about search api random sorting with pager?
In case of a simple view, views_random_seed is used, but in this case it doesn't work.
So, if i have let's say 100 results in 5 pages, there are duplicates.

Thanx!

drunken monkey’s picture

Hm, you're right, I don't think this will work with a pager. The question would then be whether this is a valid/usual requirement, or randomly sorted views usually don't use a pager anyways. Solving the use case with pager would of course be even more difficult, as you would have to pass the random seed value along to the other pages.