Nicolas
Nicolas

Reputation: 2367

Search for user query string for address in database

I have a Rails app on a Postgres database and I need to have a search field for the user to enter a string and look up in the database for possible address matches (within a city). In the database I have a column with full addresses.

I cannot make assumptions on the input, so I am thinking that I should first try to directly look up the address on the database somehow (using a LIKE query maybe?), and if that fails, request to a Geocoding API (i.e. Google) to return a well formatted addresses list matching the query and search those in my database.

I would appreciate any guidance on how to do this.

Upvotes: 0

Views: 529

Answers (2)

Gerry Shaw
Gerry Shaw

Reputation: 9378

I don't think FTS (full text search) is what you want. You'll have to use an address API that can match addresses.

I've successfully and easily used SmartyStreets for something like this. They have a free account you can use. http://smartystreets.com

Also if you did want to try going down the FTS route here is a Gist that explains how to do it. https://gist.github.com/4365593

Upvotes: 1

charlysisto
charlysisto

Reputation: 3700

You may know it already, but postresql has a fulltext search engine integrated so it's a great time to take advantage of it. I suggest watching thats excellent railscast.

Then once implemented :

class Place < AR
  def search_db_or_geokit(query)
    res = db_search()
    if res.empty?
      res = geokit_search(query)
    else
      res
    end
  end

  def geokit_search(query)
    # ...
  end

  def db_search(query)
    # ...
  end
end

For the geocoding google search api there's probably a good gem out there like geokit

Upvotes: 0

Related Questions