Question

I'm using pg-search to search in a model. The problem I'm having is if I search for keywords that are in one field or the other it works but if I enter keywords that I know are in the 2 fields I'm using it's not returning anything.

Here is my search

@providers = Provider.text_search(params[:query])

and here is the helper

def self.text_search(query)
  if query.present?
    where("address @@ :q or conditions @@ :q", q: "%#{query}%")
  else
    scoped
  end
end

To summarize... if I enter 2 keywords for address (ie: zipcode and street name) or 2 keywords for condition (ie: brand new) it works but if I enter a zip code and new in the search field it does not return anything

here is the query

Provider Load (1.3ms)  SELECT "providers".* FROM "providers"  WHERE (address @@ '%new rancho%' or conditions @@ '%new rancho%')

EDIT I forgot to post the pg_search_scope

pg_search_scope :search, against: [:address, :conditions], using: { tsearch: { dictionary: "english", prefix: true, any_word: true} }
Was it helpful?

Solution

Try something like this in your model

  include PgSearch

 pg_search_scope :search,  against: [:zipcode, :streetname], 
using: {tsearch: {prefix: true, any_word: true}}

  def self.text_search(query)
    if query.present?
      search(query)
    else
      scoped
    end
  end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top