Skip to content
This repository has been archived by the owner on Sep 27, 2022. It is now read-only.

Apply find_by_fuzzy on an ActiveRecord::Relation #37

Open
chrilleferna opened this issue Jan 25, 2014 · 5 comments
Open

Apply find_by_fuzzy on an ActiveRecord::Relation #37

chrilleferna opened this issue Jan 25, 2014 · 5 comments

Comments

@chrilleferna
Copy link

find_by_fuzzy can be applied to a class but not to an ActiveRecord::Relation (or a scope). A typical case when this would be very useful is when you want to make an exact match on one attribute followed by a fuzzy match on another attribute, like:

Person.where("country=?", theCountry).find_by_fuzzy_name(theName) 
@mezis
Copy link
Owner

mezis commented Jan 26, 2014

Hi @chrilleferna, noted. This is probably related to #18.
It's not stricly speaking a bug; I suppose it doesn't break for you, but instead ignores the scope and/or returns nil results.

The problem is that the current (efficient) implementation aggregates the trigrams table, then searches the source table. I've been playing with an alternate implementation that uses joins instead but ActiveRecord is very shaky when chaining scopes with JOIN and GROUP BY.

Could you provide more details about what you obtain & what you expect? Ideally in the form of a (failing) test case?

@chrilleferna
Copy link
Author

It is somewhat related but not entirely. It does indeed break: the example I gave above will generate an exception if not all the fuzzily found persons are in the country. The exception occurs in searchable.rb/_load_for_ids when you do find(ids). This is due to the fact that not all the ids found in the trigrams are in the scope.
I have made two set of changes:

  1. replaced the find(ids) by something else so it doesn't generate an exception. With this, things work, but the limit will be wrong
  2. moved the use of the limit param into a new version of _load_for_ids. Things now work exactly as they should, although maybe a bit less efficient, since the limit is only applied on the result of matches_for

I'll send a pull request so you can take a look

-- christer

@vrepsys
Copy link

vrepsys commented Apr 2, 2014

I've ran into exactly the same issue. Code breaks when using where(..).find_by_fuzzy..
Has this been fixed?

@malissa
Copy link

malissa commented Apr 7, 2015

I have a similar problem. I'm searching a db of books for a match on title & author. I would like to chain the fuzzy find:

Book.find_by_fuzzy_title('Title', :limit => 10).find_by_fuzzy_author('Author', :limit => 1)

The second fuzzy find fails. Is there a better way to go about this?

@yojimbosteel
Copy link

I would like to see the same thing, +1 for being able to chain scopes and feed them into find_by_fuzzy_

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

No branches or pull requests

5 participants