Ruby Forum Ferret > Using the wildcard plus partial searched

Posted by Richard (Guest)
on 18.09.2006 18:06
I've seen no definitive answer to this question.

I've noticed that typing "t" in the search box will return no results 
however if I type "t*" it brings up all results beginning with t.

I would like this behaviour on by default without having to type the 
wildcard. Is there a way to do this?
Posted by Jens Kraemer (Guest)
on 18.09.2006 19:58
(Received via mailing list)
On Mon, Sep 18, 2006 at 06:06:00PM +0200, Richard wrote:
> I've seen no definitive answer to this question.
> 
> I've noticed that typing "t" in the search box will return no results 
> however if I type "t*" it brings up all results beginning with t.
> 
> I would like this behaviour on by default without having to type the 
> wildcard. Is there a way to do this?

Manually append a wild card to the query terms before giving it to the
parser. I once did something like this for a customer. The problem is
that this can get complex with more complex queries, i.e. you won't want
to append a '*' to the word AND, since that's part of the query 
language.

Would be better to have a special query parser for this, or even an
option in the stock QueryParser to force it to use wild card queries all
the time.


Jens



--
webit! Gesellschaft für neue Medien mbH          www.webit.de
Dipl.-Wirtschaftsingenieur Jens Krämer       kraemer@webit.de
Schnorrstraße 76                         Tel +49 351 46766  0
D-01069 Dresden                          Fax +49 351 46766 66
Posted by Brian Keats (bfkeats)
on 14.10.2006 18:49
> I've noticed that typing "t" in the search box will return no results 
> however if I type "t*" it brings up all results beginning with t.
> 
> I would like this behaviour on by default without having to type the 
> wildcard. Is there a way to do this?

Add the following search method to your model

  def self.search(query)
    criteria = query.split
    wild_criteria = []
    criteria.each do |criterion|
      if criterion == "AND" || criterion == "OR"
        wild_criteria.push(criterion)
      else
        wild_criterion = "*"+criterion+"*"
        wild_criteria.push(wild_criterion)
      end
    end
    wild_criteria.pop if wild_criteria.last == "AND" || 
wild_criteria.last == "OR"
    wild_query = wild_criteria.join(" ")
    self.find_by_contents(wild_query)
  end

This also removes trailing logical operators, which will prevent it from 
screwing up a live search.
Posted by Joost Baaij (joost)
on 10.07.2008 11:20
Hi ferret users/devs,

I found this post on ruby-forum which does exactly what I want.

However it feels kinda patchy and since it was posted two years ago, I 
am wondering if this behaviour is now built-in?


Brian Keats wrote:
>> I've noticed that typing "t" in the search box will return no results 
>> however if I type "t*" it brings up all results beginning with t.
>> 
>> I would like this behaviour on by default without having to type the 
>> wildcard. Is there a way to do this?
> 
> Add the following search method to your model
> 
>   def self.search(query)
>     criteria = query.split
>     wild_criteria = []
>     criteria.each do |criterion|
>       if criterion == "AND" || criterion == "OR"
>         wild_criteria.push(criterion)
>       else
>         wild_criterion = "*"+criterion+"*"
>         wild_criteria.push(wild_criterion)
>       end
>     end
>     wild_criteria.pop if wild_criteria.last == "AND" || 
> wild_criteria.last == "OR"
>     wild_query = wild_criteria.join(" ")
>     self.find_by_contents(wild_query)
>   end
> 
> This also removes trailing logical operators, which will prevent it from 
> screwing up a live search.