Postgres / Rails multiple problem in requests

I have a problem with plurals in queries. I need a method in ruby ​​or sql that can help me with the search. For instance:

When I search for "arrays", I want it to return results for the "array".

thanks

+4
source share
3 answers

What you are looking for is word stemming .

You can't really create a language like English using a bunch of regular expressions, there are too many exceptions. You need a big dictionary.

Full-text search PostgreSQL offers such a dictionary, and I highly recommend that you use full-text search for this work:

regress=# WITH vals(a,b) AS (VALUES ('goose','geese'), ('query','queries'), ('arrays','array')) SELECT to_tsquery(a), to_tsvector(b), to_tsquery(a) @@ to_tsvector(b) FROM vals; to_tsquery | to_tsvector | ?column? ------------+-------------+---------- 'goos' | 'gees':1 | f 'queri' | 'queri':1 | t 'array' | 'array':1 | t (3 rows) 

although you will notice that the dictionary dictionary is not perfect; I would expect the search for "goose" to match "geese", but it is not. You may need to improve your vocabulary. The PostgreSQL dictionary can also be a little overpriced because it may occur from time to time .

An alternative is to use a tool that is more and more customizable, such as Apache Solr.

+3
source

You can change the dictionary when using Postgres full-text search, as described here and here .

But since mu is too briefly said, you are probably better off using a more advanced tool. I prefer Thinking Sphinx for this kind of thing.

+1
source
 Class.all(:conditions => ["attribute LIKE ?", "array%"]) 

This should do it, I have been working with Rails 2 for a while, so there is a better way, but it will do the trick.

0
source

Source: https://habr.com/ru/post/1439480/


All Articles