-
-
Notifications
You must be signed in to change notification settings - Fork 795
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
multi term search in one field #218
Comments
Why don't you take the parameter from the form and split it in the controller as you do in the first example? |
@radar |
@AnSo: Do you have an example app that I could try and reproduce this problem in? That would help me understand what's going wrong here and probably fix your problem. |
@radar : Ryan I have no example app. But the problem is easy to reproduce. (I paste the basic code here http://pastebin.com/JCruwhhp) Two simplistic models User and Profile and a one-to-one relation between both. (User has_one Profile, Profile belongs_to User) and a users/index view, with search form in it and the users listing under the search form Given you have 4 users (IDs 1, 2, 3, 4) ID /profile.last-name / profile.first_name If I type 'DOE' in the search field it returns users 1 & 3 If I type 'Jane DOE' it should returns
but it returns nothing as the "where" query generated by ransack is WHERE ((("profiles"."last_name" ILIKE '%jane doe%') OR ("profiles"."first_name" ILIKE '%jane doe%'))) Cheers |
I am not sure if "search any" feature is supported in Ransack but in your case as a quick workaround you can split request with |
@rilian : I've seen this post but did not understand it. It is said : "I catch the params[:q] before passing it to the ransack engine and do some manipulations on it " ... How ? When ? Where ? |
@AnSo, your controller fills in your form use here is a workaround for # a bit dirty code
params[:q][:combinator] = 'or'
params[:q][:groupings] = []
custom_words = params.delete('profile_last_name_or_profile_first_name_or_email_cont_any')
custom_words.split(' ').each_with_index do |word, index|
params[:q][:groupings][index] = {profile_last_name_or_profile_first_name_or_email_cont: word}
end
@q = User.joins(:profile).search(params[:q])
@users = @q.result.paginate(:per_page =>10, :page => params[:page])
... |
@rilian : Aaaaaaah ok ! All is clear now ! Thanks a lot rilian you made my day better (and me a bit less idiot)! |
Thanks @rilian :) |
very nice. thanks |
For query performance and simplicity, you can also store the various terms you want to search on in a combined search text column in your database (I use a citex field in postgres and remove accents and punctuation). For example, first name + last name + email + phone stored as "JohnDoejohndoegmailcom1234567890" Thus there is only one field to search on, and you can add a database index on the search query. |
good idea. does this solution suffer from some 'false positives'? i.e. if you search with 'ilco' or 'hnDo' it would most likely come back with a result (which may or may not be ok) |
I have posted a relevant question on stackoverflow http://stackoverflow.com/questions/19405454/mutliterm-search-in-ransack |
i like @jonatack 's idea @dimitrisdovinos you should decide yourself how to combine text data. it may be separated differently |
Just curious if this was resolved. I'm also noticing a disparity between how the form method generates SQL for In my example, the expected behavior (console):
The behavior when using
I followed something similar to the instructions above that alter the params, but I'm running into difficulties getting the altered params through will_paginate. I can get it to work by using |
In this case, the form with |
Mine worked with |
Not an issue I suppose but a lack of information/documentation on this...
I wiould like to do something like that :
the SQL generated is the following
which returns the expected results... But how to implement this request in my search form (in my view) and/or in my controller ?
My search form is :
As it is the SQL returned is
but how to implement the split(' ') to have the expected SQL ?
Any help (and a bit of explanation) would be great, documentation is a bit light on the subject...
Cheers
The text was updated successfully, but these errors were encountered: