Wildcard search


I am doing a simple dictionary for the purpose of practising ruby on
I have a search function and I want to enable wildcard search.
for example

search : apple --> gives apple
search : ap?le --> gives apple

I tried several ways but i could not get it right

any idea


Are you searching against a database or some sort of text file?

My first thought was using the 'like' clause in SQL like this:
SearchTable.find(:all, :conditions => ['content like ?', '%ap%le%'])
where you substitute the question mark in the query with % and also
enclose the whole query in %, but that would also return matches like
"my apartment is less than half a block from where I work" because
that string also contains ap(artment) and le(ss). If you search
against some sorf of string you could use a regular expression, and
that would probably be the easiest way to do it... but then again: I
am not a DB guru, nor a RoR or Ruby guru, so there might be a much
better way to accomplish the same that I don't know of.

Let me know what you end up using!

Best regards

Thanks for the reply.

I am using against the database. I will try this way which is not
really the way I want, but it still gives some result.

in fact
? is a substitution of a letter
* is a substitution of any number of letters --> this is your way
which is to substitute % instead of *

I would really appreciate your replies


Well... for the single letter case you could create something like

Source.find(:all, :conditions => ['field = ? or field = ? ... field
= ?', 'apale', 'apble', ... 'apzle'])

But it really doesn't look very nice... I can't think of a very good
solution though. And the solution above is really bad in cases where
somebody searches for f.ex "ap?le p?e". Wouldn't it be possible to get
the DB server to somehow do the heavy lifting? Have a stored procedure
that does the work?

Best regards

My mistake, the above code should probably read:
Source.find(:all, :conditions => ['field like ? or field like ? ... or
field like ?', '%apale%', '%apble%', ... '%apzle%'])

If you are using Mysql, an underscore matches one char, a percent hack
matches any number of characters so your mapping is pretty simple, *
-> _, ? -> %.


Sebastian Probst Eide wrote:

Thanks for the comments and help
It is working now

to share with you the code

def search
    @phrase = params[:phrase]
    @phrase = @phrase.gsub("?","_") #this is to replace the ? with _
for the wildcard search to replace one letter only.
    @phrase = @phrase.gsub("*","%") #this is to replace the * with %
for the wildcard search to replace unlimited number of letters.
    if @phrase.empty?
      flash[:notice] = "No word was inputed. please type a word!"
      redirect_to :action => :index
      @words = Word.find(:all, :conditions => ['word like ?', @phrase
+ '%'])


From: rubyonrails-talk@googlegroups.com [mailto:rubyonrails-
talk@googlegroups.com] On Behalf Of Shuaib85
Subject: [Rails] Re: Wildcard search

Just a few minor suggestions:

def search
    @phrase = params[:phrase]

Does @phrase need to be an attribute, or could it be a local? In general,
the smallest possible scope is best.

    @phrase = @phrase.gsub("?","_") #this is to replace the ? with _
for the wildcard search to replace one letter only.

I would advise not restating the code in comments.

      flash[:notice] = "No word was inputed. please type a word!"

I suggest: "Please enter a word"

      redirect_to :action => :index
      @words = Word.find(:all, :conditions => ['word like ?', @phrase
+ '%'])


Just to more things:

I haven't tried this, but wouldn't you need a % in front of the phrase
too? Or else you will only find words starting with what the phrase
is? Depends on the intended use ofcourse...

      @words = Word.find(:all, :conditions => ['word like ?', @phrase
+ '%'])

You could also write:
to make it shorter and sweeter :slight_smile:

Best regards

Thanks for the suggestion. In fact @phrase can be a local variable and
I will do the changes you commented.

I do not start the search by % because I assume the person knows the
word he is looking for. In terms of long words that he is confused
about the spelling or he wants to list down words that have middle
letter unknown, he then can use wildcards. for example c?t gives cat,
cut, etc.

Best regards