Hello, I am doing a simple dictionary for the purpose of practising ruby on rails 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 Regards --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Sebastian On Oct 4, 2:10 am, Shuaib85 <shuaib.za...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hello, > > I am doing a simple dictionary for the purpose of practising ruby on > rails > 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 > > Regards--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Regards On Oct 4, 11:16 pm, Sebastian Probst Eide <sebastian.probst.e...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 > Sebastian > > On Oct 4, 2:10 am, Shuaib85 <shuaib.za...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hello, > > > I am doing a simple dictionary for the purpose of practising ruby on > > rails > > 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 > > > Regards--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Well... for the single letter case you could create something like this: 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 Sebastian On Oct 5, 1:17 am, Shuaib85 <shuaib.za...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 > > Regards > > On Oct 4, 11:16 pm, Sebastian Probst Eide > > <sebastian.probst.e...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > 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 > > Sebastian > > > On Oct 4, 2:10 am, Shuaib85 <shuaib.za...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Hello, > > > > I am doing a simple dictionary for the purpose of practising ruby on > > > rails > > > 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 > > > > Regards--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
My mistake, the above code should probably read: Source.find(:all, :conditions => [''field like ? or field like ? ... or field like ?'', ''%apale%'', ''%apble%'', ... ''%apzle%'']) On Oct 6, 12:08 pm, Sebastian Probst Eide <sebastian.probst.e...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Well... for the single letter case you could create something like > this: > > 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 > Sebastian > > On Oct 5, 1:17 am, Shuaib85 <shuaib.za...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > 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 > > > Regards > > > On Oct 4, 11:16 pm, Sebastian Probst Eide > > > <sebastian.probst.e...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > 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 > > > Sebastian > > > > On Oct 4, 2:10 am, Shuaib85 <shuaib.za...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > Hello, > > > > > I am doing a simple dictionary for the purpose of practising ruby on > > > > rails > > > > 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 > > > > > Regards--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
If you are using Mysql, an underscore matches one char, a percent hack matches any number of characters so your mapping is pretty simple, * -> _, ? -> %. -Bill Sebastian Probst Eide wrote:> My mistake, the above code should probably read: > Source.find(:all, :conditions => [''field like ? or field like ? ... or > field like ?'', ''%apale%'', ''%apble%'', ... ''%apzle%'']) > > On Oct 6, 12:08 pm, Sebastian Probst Eide > <sebastian.probst.e...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> Well... for the single letter case you could create something like >> this: >> >> 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 >> Sebastian >> >> On Oct 5, 1:17 am, Shuaib85 <shuaib.za...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> >> >>> 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 >>> >>> Regards >>> >>> On Oct 4, 11:16 pm, Sebastian Probst Eide >>> >>> <sebastian.probst.e...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >>> >>>> 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 >>>> Sebastian >>>> >>>> On Oct 4, 2:10 am, Shuaib85 <shuaib.za...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >>>> >>>>> Hello, >>>>> >>>>> I am doing a simple dictionary for the purpose of practising ruby on >>>>> rails >>>>> 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 >>>>> >>>>> Regards >>>>> > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 else @words = Word.find(:all, :conditions => [''word like ?'', @phrase + ''%'']) end end regards On Oct 7, 12:39 am, William Pratt <bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote:> If you are using Mysql, an underscore matches one char, a percent hack > matches any number of characters so your mapping is pretty simple, * -> > _, ? -> %. > > -Bill > > Sebastian Probst Eide wrote: > > My mistake, the above code should probably read: > > Source.find(:all, :conditions => [''field like ? or field like ? ... or > > field like ?'', ''%apale%'', ''%apble%'', ... ''%apzle%'']) > > > On Oct 6, 12:08 pm, Sebastian Probst Eide > > <sebastian.probst.e...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > >> Well... for the single letter case you could create something like > >> this: > > >> 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 > >> Sebastian > > >> On Oct 5, 1:17 am, Shuaib85 <shuaib.za...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > >>> 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 > > >>> Regards > > >>> On Oct 4, 11:16 pm, Sebastian Probst Eide > > >>> <sebastian.probst.e...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > >>>> 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 > >>>> Sebastian > > >>>> On Oct 4, 2:10 am, Shuaib85 <shuaib.za...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > >>>>> Hello, > > >>>>> I am doing a simple dictionary for the purpose of practising ruby on > >>>>> rails > >>>>> 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 > > >>>>> Regards--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> -----Original Message----- > From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org [mailto:rubyonrails- > talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Shuaib85 > Subject: [Rails] Re: Wildcard searchJust 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 > else > @words = Word.find(:all, :conditions => [''word like ?'', @phrase > + ''%'']) > end > end///ark --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi 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... else @words = Word.find(:all, :conditions => [''word like ?'', @phrase + ''%'']) You could also write: @phrase.gsub!("?","_") @phrase.gsub!("*","%") to make it shorter and sweeter :) Best regards Sebastian On Oct 8, 2:36 pm, "Mark Wilden" <m...-OCn100epQuBBDgjK7y7TUQ@public.gmane.org> wrote:> > -----Original Message----- > > From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org [mailto:rubyonrails- > > talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] 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 > > else > > @words = Word.find(:all, :conditions => [''word like ?'', @phrase > > + ''%'']) > > end > > end > > ///ark--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Shuaib On Oct 9, 1:36 am, "Mark Wilden" <m...-OCn100epQuBBDgjK7y7TUQ@public.gmane.org> wrote:> > -----Original Message----- > > From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org [mailto:rubyonrails- > > talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Shuaib85 > > Subject: [Rails] Re:Wildcardsearch > > Just a few minor suggestions: > > > defsearch > > @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 thewildcardsearchto 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 > > else > > @words = Word.find(:all, :conditions => [''word like ?'', @phrase > > + ''%'']) > > end > > end > > ///ark--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---