Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


Smarter search using regular expressions

Hi there.

Now I'm trying to make search of my app a liitle bit smarter (not very sensitive to misspellings).

It sends SQL request to POSTGRE SQL, and if initial query returns empty results, uses POSTGRE regexp search.

At the moment basically it just replaces 2 charachters of initial seach with any-charachter regex symbol in all combinations.

if query[i]=~/[\p{L}]/
for k in 0...query.length
if query[k]=~/[\p{L}]/ && i!=k
if k<i
possible_queries.push(query[0...k]+'[^\s!\n\\,\.`""''?:;<=>_\)\(-]'+query[k+1...i]+'[^\s!\n\\,\.`""''?:;<=>_\)\(-   ]'+query[i+1...query.length])
possible_queries.push(query[0...i]+'[^\s!\n\\,\.`""''?:;<=>_\)\(-]' +query[i+1...k]+ '[^\s!\n\\,\.`""''?:;<=>_\)\(-]' +query[k+1...query.length])
 possible_queries.each do |p|
 if where("title ~* '#{p}' or text_for_test~*'#{p}'  and state @@ :state", state:'saved').length!=0
 output=output+where("title ~* '#{p}'or text_for_test~*'#{p}' and state @@ :state", state:'saved')
 return output.uniq

But, it is obviosly not the best approach. How can I improve it?

1 Answer

Jason Seifer
Jason Seifer
Treehouse Guest Teacher

Hi Slava Zimogorov what are you trying to do exactly? Can you give more context with your code?

For example, there is object in database named "object". With strict search, if user searches for "obkect" (he made a misspelling) he will find nothing. But with regex and this method, after founding nothing for "obkect", app will send regexp request to SQL [\w][\w]kect, [\w]b[\w]ect (and all other combinations, this will capture object).

But. 1) This doesn't catch mispelling with additional letter ("objkect" will find nothing) 2) I want to do number of any-letters in regexp dymanic, depending on query length 3) And I feel unsure is this approach is good idea.

Jason Seifer
Jason Seifer
Treehouse Guest Teacher

A better approach might be to use a spellcheck library and find the possible misspellings of the words that the user submits then do a single, combined search on all of those.

Thanks... any example of such library?