# Using the wildcard plus partial searched

**URL:** <https://www.ruby-forum.com/t/using-the-wildcard-plus-partial-searched/69104>\
**Category:** Ferret\
**Created:** [September 18, 2006, 6:06pm UTC](https://www.ruby-forum.com/t/using-the-wildcard-plus-partial-searched/69104 "2006-09-18T18:06:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![richard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f0a364/32.png) [@richard](https://www.ruby-forum.com/u/richard)\
**Post date:** [September 18, 2006, 6:06pm UTC](https://www.ruby-forum.com/t/using-the-wildcard-plus-partial-searched/69104/1 "2006-09-18T18:06:00Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![richard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f0a364/32.png) [@richard](https://www.ruby-forum.com/u/richard)\
**Post date:** [September 18, 2006, 7:58pm UTC](https://www.ruby-forum.com/t/using-the-wildcard-plus-partial-searched/69104/2 "2006-09-18T19:58:42Z")

</div>

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](http://www.webit.de)  
Dipl.-Wirtschaftsingenieur Jens Krämer removed\_email\_address@domain.invalid  
Schnorrstraße 76 Tel +49 351 46766 0  
D-01069 Dresden Fax +49 351 46766 66

---

<div class="post-metadata">

**Author:** ![richard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f0a364/32.png) [@richard](https://www.ruby-forum.com/u/richard)\
**Post date:** [October 14, 2006, 6:49pm UTC](https://www.ruby-forum.com/t/using-the-wildcard-plus-partial-searched/69104/3 "2006-10-14T18:49:10Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![richard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f0a364/32.png) [@richard](https://www.ruby-forum.com/u/richard)\
**Post date:** [July 10, 2008, 11:20am UTC](https://www.ruby-forum.com/t/using-the-wildcard-plus-partial-searched/69104/4 "2008-07-10T11:20:56Z")

</div>

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.
