Search code examples
ruby-on-railsrubypg-search

pg_search regex search


Does gem pg_serch supports search by regex, if yes could someone give an example of using regular expressions with pg_search. I could not find it in the documentation. I am looking for something like this

Model.regex_search(/.*find_string$/) is it possible with pg_search?


Solution

  • No it doesn't, nor should it.

    pg_search utilizes Postgres' full text search extension, so it is already, in a way, doing regex searches on indexed data.

    If you want to do a pure regex search against text data in your table, you should use PG's pattern matching. You can use the operators ~ (case sensitive) and ~* (case insensitive) within WHERE clauses to match against a POSIX regular expression.

    Model.where("column ~* ?", "regex")
    

    If you intend on doing this, I highly recommend that you create indexes on those columns.