Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allowing for a user-defined custom seed value to be passed to the RandomSortField #328

Merged
merged 2 commits into from
Dec 9, 2012

Conversation

zsiec
Copy link
Contributor

@zsiec zsiec commented Dec 9, 2012

Recently I was working on a paginated collection that had be configured to return a random sort order. I realized that because sunspot always sends Solr a random seed value, I would intermittently receive duplicate results between pages. In order to fix this, I thought it useful to be able to configure the seed value while searching.

The implementation I thought would make the most sense would retain the existing syntax specifications while also allowing a new paramater type to the order_by function.

E.g.

Existing syntax will continue to work

Model.search do
  order_by :random, :asc
end

or

Model.search do
  order_by_random
end

Proposed alternative

Model.search do
  order_by :random, :seed => 12345, :direction => :asc 
end

In response to a question, I included...

When moving through a paginated collection, you will need to retain the same seed for the length of that collection. If you are changing the seed value for every new page of data, you will get duplicates.

The way I implemented this went like this. A call to my backend API would return the first page of results and a random seed value that the server generated. The client then keeps that seed value around and sends it as part of the request for subsequent pages as long as that collection is still being paged through. The API looks to see if the user has sent a random seed value and will use it if when searching if it exists.

P.S. The only gotcha with all of this is that the seed sent back to solr will only return the same data each time as long as the version of the index does not change.

…e ability to provide a custom seed value. Integration specs and api tests have been added
alindeman added a commit that referenced this pull request Dec 9, 2012
Allowing for a user-defined custom seed value to be passed to the RandomSortField
@alindeman alindeman merged commit 980e6af into sunspot:master Dec 9, 2012
@mladeny
Copy link

mladeny commented Dec 11, 2012

how can I update my current version to use this bug fix?

this is the gem version I'm using right now.

gem 'sunspot_rails', '2.0.0.pre.120925'

@zsiec
Copy link
Contributor Author

zsiec commented Dec 11, 2012

Does bundle update sunspot_rails work for you? It may require your gem definition to look like:

gem 'sunspot_rails', git: 'git@github.com:sunspot/sunspot.git'

@mladeny
Copy link

mladeny commented Dec 11, 2012

I got the correct version by specifying the git reference (commit #), however this fix doesn't seem to resolve the duplicate results between pages.
search_seed = rand(10000)
when order_by :random, :seed => search_seed, :direction => :desc

I still get duplicate results between pages, not on all random numbers though. If I use 12345 for the :seed parameter, I don't get duplicate results between pages, but I need to be able to generate a random seed otherwise the results will not be random if the seed is always the same...

@zsiec
Copy link
Contributor Author

zsiec commented Dec 11, 2012

Perhaps I should have included more information on how this works.

When moving through a paginated collection, you will need to retain the same seed for the length of that collection. If you are changing the seed value for every new page of data, you will get duplicates.

The way I implemented this went like this. A call to my backend API would return the first page of results and a random seed value that the server generated. The client then keeps that seed value around and sends it as part of the request for subsequent pages as long as that collection is still being paged through. The API looks to see if the user has sent a random seed value and will use it if when searching if it exists.

I've had success with this approach, let me know if it makes sense.

P.S. The only gotcha with all of this is that the seed sent back to solr will only return the same data each time as long as the version of the index does not change.

@mladeny
Copy link

mladeny commented Dec 11, 2012

Thanks for the clarification. I think I figured out what was wrong with my code.

Edit:

Yup, I wasn't passing the seed value back when loading the next page!

Thanks for the fix, I've been waiting for this for a few weeks now.

Cheers!

@zsiec zsiec deleted the custom_random_seeding branch February 10, 2014 15:36
@kalinchuk
Copy link

P.S. The only gotcha with all of this is that the seed sent back to solr will only return the same data each time as long as the version of the index does not change.

Any way to prevent that from happening?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants