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

[WIP] Records order preservation ActiveRecord POC #639

Closed
wants to merge 2 commits into from

Conversation

tyler-boyd
Copy link
Contributor

Fixes #463

As you know, currently the records method is not ordered by Elasticsearch's hit ordering. Taking heavy inspiration from order_as_specified gem, this preserves the order of the search results.

It should be noted that this is quite slow to do, and should only be done if you really need an ActiveRecord::Relation. Otherwise, it's possibly better to just use the existing in-memory representation.

Todo

  • Ensure compatibility with all ActiveRecord DBs (MySQL, PostgreSQL, SQLite, etc)
  • Ensure that the existing results_query preserves order for Mongoid/NoBrainer

Let me know if you have any thoughts.

also, cc @abevoelker @milushov

@jimmybaker
Copy link

I've also made a pull request to solve this problem using a different and more performant way. #672

Of course milage may vary but I was using a method similar to one you are proposing here and the query time to bring by 25 records was ~100ms which is slow as you mentioned in the description. With what I'm proposing in my PR I was able to get the same 25 records in 0.7ms.

image 2016-05-06 at 1 23 44 pm

Check it out and let me know if you have any thoughts on it.

@ankane
Copy link
Owner

ankane commented Nov 8, 2016

Hey @Mavvie, thanks for the PR. Here's how Elasticsearch Rails handles it.

It should be a more performant approach.

@jimmybaker
Copy link

@ankane - Would the way ElasticSearch Rails handles it work for us when we paginate search results?

@jimmybaker
Copy link

This is an interesting problem. I'm working up a PR now and when writing the test I noticed that calling normal enumerable methods like map, each, or collect on .records doesn't call to_a. Should we also override these methods to ensure that things are done in the proper order?

@jimmybaker
Copy link

Since we're returning an active record relation with .records I still think the best way to handle this is to ensure the ordering is done on the db side based on the ordering of hits from es.

Anyone else following me here?

@tyler-boyd
Copy link
Contributor Author

I think something similar is possible, but I'm not sure exactly how it would be implemented. I don't really know how pagination libraries work.

It could make sense to do the ordering in Ruby-land if we're only loading per_page records from the db. However, the db might still do it better in that case as well. Not sure really.

@khiav223577
Copy link

khiav223577 commented Jun 9, 2017

Maybe you could try the order method le0pard shared here: khiav223577/find_with_order#5
(no need to define a db function)

image

@tyler-boyd
Copy link
Contributor Author

@ankane maybe we could add that gem as a dependency? Or should we copy what we need from it?

@ankane
Copy link
Owner

ankane commented Sep 9, 2017

Hey, the plan is to remove the records method in Searchkick 3.0, so going to close this PR. Feel free to create another gem with this functionality that can tie into Searchkick.

@ankane ankane closed this Sep 9, 2017
@lock lock bot locked as resolved and limited conversation to collaborators Jan 18, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants