What's the easiest way to do a quick wildcard search on a field from the console? I don't care against guarding against SQL injection.
I'm using PostgreSQL.
Searching for title with strings containing "emerging'
This works but is somewhat cumbersome. Curious if there was a shorthand?
Product.where("title ## :q", q: "emerging")
Equally cumbersome but no longer appear to work for me in Rails 4:
Product.where("title ILIKE ?", "emerging")
Product.where("title ilike :q", q: "emerging")
I guess I'm looking for something like Product.where(title: "*emerging*")
This should do it:
word = 'emerging'
Product.where('title ILIKE ?', "%#{word}%")
The ILIKE makes the search not sensitive to the case (PostgreSQL feature!)
the "%" wildcard makes the search match every product having a title containing "word" inside with (or without) stuff before and/or after.
Use LIKE, like this:
Product.where('title LIKE ?', '%emerging%')
Related
How to use ILIKE for multiple values in single column.
This is the solution for multiple values without using like condition:
Project.where(name: ["Arvind Oasis", "Prestige Jindal City"])
In this example, it is taking the exact matches in my database. but i want to take similar matches for all the values.
How to use ILIKE for multiple values ["Arvind Oasis", "Rajkumar Jindal City"]
For single values i can use like this,
Project.where("name ILIKE ?", "%Prestige Kumar%")
Use:
Project.where("name ILIKE ANY (array[?])", ["%Arvind Oasis%", "%Prestige Jindal City%", "%XXXX%"])
You can try postgres SIMILAR TO operator like below
Project.where("name similar to
'%(Arvind\sOasis|Rajkumar\sJindal\sCity)%'")
I am having trouble searching a string by the first letters. For example, if I have "animal", "ant", and "abs" in my database, I would like a query of "an" to return "animal" and "ant" (case insensitive). I got this working just fine with sqlite3:
Thing.where("name LIKE ?", params[:query] + '%')
However, this does not work in PostgreSQL (database used in pushing to Heroku). What is the correct query using PostgreSQL?
In PostgreSQL, you have to use ILIKE instead of LIKE to do a case-insensitive pattern match.
Your query will be:
Thing.where("name LIKE '?%'", params[:query])
This makes the query case insensitive:
Thing.where("lower(name) LIKE '?%'", params[:query].to_s.downcase)
If your production uses Postgres, I recommend development does it also, to prevent a further error like this!
So let's say I search for 'Blerg'. And I have a item with the name SomethingblergSomething.
If I do an ILIKE search in postgres (and rails) like this:
where("name ILIKE ?", "%#{ 'Blerg' }%")
It will return the result 'SomethingBlergSomething' because it contains Blerg.
Is there a way to make the faster tsvector do a similar style of searching inside a word:
where("(to_tsvector('english', name) ## to_tsquery(?))", ('Blerg' + ':*'))
The above query will not return 'SomethingBlergSomething'.
So how do I make tsvector act like ILIKE when searching inside words.
Are you aware of trigram search, provided by the additional module pg_trgm? That seems more appropriate for your use case than text search.
With a trigram index in place (GIN or GiST) you can use your original ILIKE predicate and get index support for it. You need Postgres 9.1+ for that.
Details:
PostgreSQL LIKE query performance variations
Pattern matching with LIKE, SIMILAR TO or regular expressions in PostgreSQL
I'm experimenting with a few concepts (actually playing and learning by building a RoR version of the 1978 database WHATSIT?).
It basically is a has_many :through structure with Subject -> Tags <- Value. I've tried to replicate a little of the command line structure by using a query text field to enter the commands. Basically things like: What's steve's phone.
Anyhow, with that interface most of the searches use ILIKE. I though about enhancing it by allowing OR conditions using some form of an array. Something like What's steve's [son,daugher]. I got it working by creating the ILIKE clause directly, but not with string replacement.
def bracket_to_ilike(arrel,name,bracket)
bracket_array = bracket.match(/\[([^\]]+)\]/)[1].split(',')
like_clause = bracket_array.map {|i| "#{name} ILiKE '#{i}' "}.join(" OR ")
arrel.where(like_clause)
end
bracket_to_ilike(tags,'tags.name','[son,daughter]') produces the like clause tags.name ILiKE 'son' OR tags.name ILiKE 'daughter'
And it get the relations, but with all the talk about using the form ("tags.name ILiKE ? OR tags.name ? ",v1,v2,vN..)., I though I'd ask if anyone has any ideas on how to do that.
Creating variables on the fly is doable from what I've searched, but not in favor. I just wondered if anyone has tried creating a method that can add a where clause that has a variable number parameters.I tried sending the where clause to the relation, but it didn't like that.
Steve
Couple of things to watch out for in your code...
What will happen when one of the elements of bracket_array contains a single quote?
What will happen if I take it step farther and set an element to say "'; drop tables..."?
My first stab at refactoring your code would be to see if Arel can do it. Or Sequeel, or whatever they call the "metawhere" gem these days. My second stab would be something like this:
arrel.where( [ bracket_array.size.times.map{"#{name} ILIKE ?"}.join(' OR '), *bracket_array ])
I didn't test it, but the idea is to use the size of bracket_array to generate a string of OR'd conditions, then use the splat operator to pass in all the values.
Thanks to Phillip for pointing me in the right direction.
I didn't know you could pass an array to a where clause - that opened up some options
I had used the splat operator a few times, but it didn't hit me that it actually creates an object(variable)
The [son,daughter] stuff was just a console exercise to see what I could do, but not sure what I was going to do with it. I ended up taking the model association and creating the array out of the picture and implemented OR searches.
def array_to_ilike(col_name,keys)
ilike = [keys.map {|i| "#{col_name} ILiKE ? "}.join(" OR "), *keys ]
#ilike = [keys.size.times.map{"#{col_name} ILIKE ?"}.join(' OR '), *keys ]
#both work, guess its just what you are use to.
end
I then allowed a pipe(|) character in my subject,tag,values searches, so a WHATSIT style question
What's Steve's Phone Home|Work => displays home and work phone
steve phone home|work The 's stuff is just for show
steve son|daughter => displays children
phone james%|lori% => displays phone number for anyone who's name starts with james or lori
james%|lori% => dumps all information on anyone who's name starts with james or lori
The query then parses the command and if it encounters a | in any of the words, it will do things like:
t_ilike = array_to_ilike('tags.name',name.split("|"))
# or I actually stored it off on the inital parse
t_ilike = #tuple[:tag][:ilike] ||= ['tags.name ilike ?',tag]
Again this is just a learning exercise in creating a non-CRUD class to deal with the parsing and searching.
Steve
I want my code to do two things that is currently not doing
#students = Student.where(["first_name = ? OR middle_name = ? OR last_name = ?", params[:query].split])
Work. (it says im supposed to pass 4 parameters but I want the user to be able to type words and find by those words on each of those fields and return whatever matches)
Actually use Like clause instead of rigid equal clause.
Please Help.
This looks like a problem that would be better suited to using search rather than SQL. Have you considered something like thinking sphinx or act as ferret (solr would probably be overkill).
...if you must do this in sql, you could build a query something like this:
cols = ['first_name', 'last_name', 'middle_name']
query = 'John Smith'
sql_query = cols.map{|c| query.split.map{|q| "#{c} like '?'"}}.join(' OR ')
sql_query_array = query.split * cols.count
Student.where(sql_query, sql_query_array)
I agree with the previous advice that if you need to do search you should look at something like Solr or Sphinx.
Anyhow, this should help you out.
def search
query = params[:query].split.map {|term| "%#{term}%" }
sql = "first_name LIKE ? OR middle_name LIKE ? OR last_name LIKE ?"
#students = Student.where([sql, *query])
end
The answer to step 1 is using Ruby's awesome little feature called the "splat operator" which allows you to take an array and evaluate it as a list of arguments.
The answer to step 2 is to just massage the query string you get back from the params and turn it into something you can use with the LIKE operator. I basically stole this from Railscasts Simple Search Form episode.
I'm not sure if you want all fields to search the same term if that is the case then you can do this:
where("first_name LIKE :term OR middle_name LIKE :term OR last_name LIKE :term", { term: "%#{params[:term]}%"})
No need for any crazy split or map or anything else, this is just straight ActiveRecord