# Address search algorithms

**URL:** <https://www.ruby-forum.com/t/address-search-algorithms/139087>\
**Category:** Ruby\
**Created:** [May 30, 2008, 5:45pm UTC](https://www.ruby-forum.com/t/address-search-algorithms/139087 "2008-05-30T17:45:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mikel\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/2bfe46/32.png) [@Mikel\_L](https://www.ruby-forum.com/u/Mikel_L)\
**Post date:** [May 30, 2008, 5:45pm UTC](https://www.ruby-forum.com/t/address-search-algorithms/139087/1 "2008-05-30T17:45:44Z")

</div>

Heya all,  
Got an interesting little problem, not specifically Ruby, but I hope to  
solve it with some nice ruby 🙂

I need to be able to do searches on a data set of a street address.

The address has three fields, what is needed is an efficient way to  
search  
this address with partial data.

The addresses are in a database, what I am thinking is going through and  
pre  
generating a search string based on the three concatenated address  
values  
and then index this search column and search off that using the output  
from  
the same algorithm on the entered search values.

Would full text search be the best solution here? The DB is on postgres  
by  
the well.

The problem is that someone might want to search for “back st” and  
expect to  
match “123 back street”, or worse, “123 back lane” and get “123 back  
street”

Anyone else run into these sorts of problems, any feed back?

Mikel

---

<div class="post-metadata">

**Author:** ![Mikel\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/2bfe46/32.png) [@Mikel\_L](https://www.ruby-forum.com/u/Mikel_L)\
**Post date:** [May 30, 2008, 6:28pm UTC](https://www.ruby-forum.com/t/address-search-algorithms/139087/2 "2008-05-30T18:28:16Z")

</div>

This gem might help:

ziplookup (1.0.0)  
Classes for standardizing addresses via the USPS web site.

---

<div class="post-metadata">

**Author:** ![Mikel\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/2bfe46/32.png) [@Mikel\_L](https://www.ruby-forum.com/u/Mikel_L)\
**Post date:** [May 30, 2008, 6:08pm UTC](https://www.ruby-forum.com/t/address-search-algorithms/139087/3 "2008-05-30T18:08:50Z")

</div>

On 30 May 2008, at 10:09, Mikel L. wrote:

> Heya all,  
> Got an interesting little problem, not specifically Ruby, but I hope  
> to  
> solve it with some nice ruby 🙂
> 
> I need to be able to do searches on a data set of a street address.
> 
> The address has three fields, what is needed is an efficient way to  
> search  
> this address with partial data.

Assuming these are UK-only addresses, at the very least you should  
have four fields:

- building name/number;
- street;
- town;
- postcode

and the national postcode database allows for seven if I remember  
correctly - although it’s usual for a couple of those to be blank.  
Depending on how well normalised you want the database it can be a  
good idea to split out the last three into their own tables and only  
store keys in your main address table. You’ll get the biggest gain  
from doing that with towns, but streets are normally a good gain as  
well. Oh, and obviously use indexes.

> the well.
> 
> The problem is that someone might want to search for “back st” and  
> expect to  
> match “123 back street”, or worse, “123 back lane” and get “123 back  
> street”

- Keep a dictionary of well known abbreviations and massage the query  
to expand these;
- store the street number in its own column (which won’t necessarily  
be numeric thanks to all the Flat 1’s and 22B’s lurking in the system,  
not to mention named properties without a number etc.);
- store the postcode in its own column and filter by postcode whenever  
possible;
- use a soundex algorithm to store all words as phonetic  
approximations to deal with simple misspellings (bock vs. back, bak  
vs. back);
- AND MOST IMPORTANTLY: constrain the way the user can enter a query  
so that it keeps the query as simple as possible;
- use whatever full text search facility postgres has

> Anyone else run into these sorts of problems, any feed back?

Many many years ago 🙂  
Out of interest, are you using the postcode database to generate your  
address information, or is it user-submitted data? If the latter, make  
sure you constrain how data can be submitted. And either way, do as  
much pre-processing as possible to get the addresses into a normalised  
form.

Ellie

## Eleanor McHugh Games With Brains [http://slides.games-with-brains.net](http://slides.games-with-brains.net)

raise ArgumentError unless @reality.responds\_to? :reason

---

<div class="post-metadata">

**Author:** ![Mikel\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/2bfe46/32.png) [@Mikel\_L](https://www.ruby-forum.com/u/Mikel_L)\
**Post date:** [May 30, 2008, 8:31pm UTC](https://www.ruby-forum.com/t/address-search-algorithms/139087/4 "2008-05-30T20:31:28Z")

</div>

On 30 May 2008, at 13:08, Mikel L. wrote:

> This is pre-existing data… the worst kind 🙂

You have my sympathy 😉

Ellie

## Eleanor McHugh Games With Brains [http://slides.games-with-brains.net](http://slides.games-with-brains.net)

raise ArgumentError unless @reality.responds\_to? :reason

---

<div class="post-metadata">

**Author:** ![Mikel\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/2bfe46/32.png) [@Mikel\_L](https://www.ruby-forum.com/u/Mikel_L)\
**Post date:** [May 30, 2008, 8:12pm UTC](https://www.ruby-forum.com/t/address-search-algorithms/139087/5 "2008-05-30T20:12:41Z")

</div>

> Many many years ago 🙂

Thanks all for your tips!

I have already implemented most of them… but some of them are also  
good  
ideas.

> Out of interest, are you using the postcode database to generate your  
> address  
> information, or is it user-submitted data? If the latter, make sure you  
> constrain  
> how data can be submitted. And either way, do as much pre-processing as  
> possible to get the addresses into a normalised form.

This is pre-existing data… the worst kind 🙂

Mikel
