Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Operator search #45

Closed
juancroca opened this Issue Jun 6, 2012 · 2 comments

Comments

3 participants

Is there a way to do a search with like operators? For example I have a model named city who has a name attribute if i search for "al" i would like to return all cities who contains "al" in its name attribute. Currently it will only return cities that i pass the complete name for example if a city's name is "alba" and i search by "al" this city is not returned only if i search "alba" i'll get a record returned.

city.rb

pg_search_scope :search, against: [:name, :raw_slug, :parametrized_slug],
  using: {tsearch: {dictionary: "english"}},
  ignoring: :accents
Collaborator

nertzy commented Jun 6, 2012

The built-in full-text search in PostgreSQL ("tsearch") doesn't support LIKE operators, but it does support prefix search. So you could search "al" and get "alba", but not get "tall" or "madrigal". Just add :prefix => true to the :tsearch options.

pg_search_scope :search, against: [:name, :raw_slug, :parametrized_slug],
  using: {
    tsearch: {dictionary: "english", prefix: true}
  },
  ignoring: :accents

Alternatively you could actually use ILIKE to perform the search. pg_search doesn't currently support ILIKE although it would be trivial to implement. See issue #3.

If I get a free moment soon I could whip it up. If you're feeling brave, this involves adding a new feature to the lib/features directory and hooking it up in all the correct places.

Hi fellows,

I need this feature pretty bad on a project I am working on, can I have a go on or will @jcroca like to?

@nertzy nertzy closed this Dec 21, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment