thecodecrate/city-state

Can we have search

Opened this issue · 2 comments

Can we have an api for search as well, where we get these things,

- when city name matches with the key word display city, state, country
- when state name alone matches with the key word display state, country
- when country name alone matches with the key word display country

Say, when I search for 'Ind' I expect the following results,

    - "Ananindeua,Para,Brazil"
    - "Olinda,Pernambuco,Brazil"
    - "Gindou,Midi-Pyrenees,France"
    - "India"
    - "Indore,Madhya Pradesh,India"
    - "Windsor,Connecticut,USA"
    - "Indiana,Pennsylvania,USA"
    - "Indiana,USA"

CS.search('Ind')
When no additional parameters is given it would search in all (cities, states and countries).

We can have additional parameters as well like,

CS.search('Ind', :country) will search only in countries
CS.search('Ind', :state) will search only in states
CS.search('Ind', :city) will search only in cities
CS.search('Ind', :country, :state) will search only in states and countries
CS.search('Ind', :country, :city) will search only in cities and countries
CS.search('Ind', :city, :state) will search only in states and cities

sorry for the long delay @ovamsikrishna. It's a great idea, it can be useful on autocomplete inputs. I'm just not sure on how to make this.

Currently we have about 87k cities around the world. So, let's say that we have an array with all these cities on memory, an array of strings like "Ananindeua,Para,Brazil" - this would cosume more than 8MB. On a simple implementation, the CS.search method could test each city against an regex, but this would spend around 87.000 regex calls each time, which can lead to performance issues - mainly if you want to use it with some "autocomplete ajax" feature.

One way to improve the results would be by installing the CityState on the user's database. Curently we are using a simple yml file as database (as there's no need to perform any complex query). But this would require one more step on the user perspective (to install the database) and also it will increase the script complexity.

Another way to implement this is to make a sort of FTS (full text search) engine inside the gem. It can be something like a txt file with all cities (eg: "Ananindeua,Para,Brazil", one per line). And another file indexing this (a graph tree, maybe).

For the simplicity sake, I think the simple "87k regex calls" would be the best choice - not the best, but the more feasible (unless you have a lots of spare time). Unfortunatelly I don't have much time to implement this (although I believe that it will be just an ".each" with 2 lines of code, plus some tests), but I'll try to implement as soon as possible (as I believe it's a good idea).

PS: feel free to suggest another approaches or to implement by yourself and send these changes as a PR.

@loureirorg Well, these were the options I had in my mind before asking this question. As of now we are doing the "k regex calls". We are having an array of strings as you said. If a gem using the open source like Maxmind can do this I thought I can avoid all these functionalities in the application level. If Maxmind can give such API then it would be damn easier. I may not go for a PR as I don't have much time to spend in this area. But, will definitely let you know if I have a better approach.