Api to search indian banks or branches.
- Django
- Django Rest framework
- PostgresDB
- Redis ( for caching)
- Full text search
- Postgres indexes ( for faster queries, No one hates indexes )
- Caching ( you know why 🔥🚀)
- Postgres triggers
- Pagination ( Your client (frontend) would love you for this)
The project uses native full text faetures provided by postgresql. There are two types of full text search
- using __search ( by django) for searching on single column.
- using Search Ranking, Search Vector and Search Query.
The second full text search ranks the results on the basis of the rank provided by postgres for each result. Greater the rank higher the result will be in result.
For optimizing search queries I have used GIN indexes over the search column. This made my queries run 2x faster. It's simple to create a GIN index.
CREATE INDEX INDEX_NAME ON TABLE_NAME USING GIN(COLUMN_NAME)
As per postgres docs gin indexes are 2-3x faster than other indexes available in postgres ( GIST and Btree) but uses more space.
Also I added a trigger to db which will automatically generate a search vetor on any insert or update query
- Set environment variables in a .env file in project root
SECRET_KEY
DB_NAME
DB_HOST
DB_USER
DB_PASSWORD
REDIS_HOST
REDIS_PASSWORD
DEBUG
- Add data to your database, get data from here
psql psql -h DB_HOST -p DB_PORT -U DB_USER -d DB_NAME -f FILENAME.sql
You can get the sql file from the above repo
- Migrate db ( fingers crossed 🤞🏻)
python mange.py makemigrations
python manage.py migrate
- Run the server
python manage.py runserver
- Run test (Optional)
python manage.py test
SetUp a local postgres instance for testing and add the credentials in settings.py
-
/api/branches/autocomplete/?q=<search_tern>&offset=<page_number>&limit=<page_size>
Autocomplete endpoint, searches over the branch column of branches table and returns the result. -
/api/branches/search/?q=<search_term>&city=<city_name>&offset=<page_number>&limit=<page_size>
Searches all the db columns in bracnes table in the given city with the given search term. -
/api/banks/:id
Returns bank with id :id from the banks table.
Freebie Stuff 💁🏻♀️🙋♀️
- Heroku ( free, deployed django project there)
- Clever Cloud ( free postgres add on)
- Redis labs redis cloud ( free redis instance)
All these things are included in AWS Free tier but i don't want to share my card details 😅 ( I don't have a card 🥲).
- Partial word search
Postgres full text search is OP ❤️🔥 but it dosen't perform well with partial words. E.g will give no result for eas but will give results for east.
Solution Use something advance like Elasticsearch for full text search or path or write some custom plugin to leverage postgres FTS for partial words.
- Add API docs ( maybe swagger ), due to time constraints I am considering this as future work.
I really enjoyed creating this project and learend lot's of new things while creating it.
P.S Today (21 May, 2021) is my bithday 🥳