Autocomplete using Redis and Python

#redis client for pythonimport redis
#flask to expose api's to outside world
from flask import Flask,request,jsonify
app = Flask("autocomplete")
#creating a redis connection
r = redis.StrictRedis(host='localhost', port=7001, db=0)
#route to add a value to autocomplete list
'''
FORMAT:
localhost:5000/add?name=<name>
'''
@app.route('/add')
def add_to_dict():
try:
name = request.args.get('name')
n = name.strip()
for l in range(1,len(n)):
prefix = n[0:l]
r.zadd('compl',{prefix:0})
r.zadd('compl',{n+"*":0})
return "Added"
except:
return "Addition failed"
#route to get the suggestiosn
'''
FORMAT:
localhost:5000/suggestions?prefix=<prefix_you want to match>
'''
@app.route('/suggestions')
def get_suggestions():
prefix = request.args.get('prefix')
results = []
rangelen = 50 # This is not random, try to get replies < MTU size
count=5
start = r.zrank('compl',prefix)
if not start:
return []
while (len(results) != count):
range = r.zrange('compl',start,start+rangelen-1)
start += rangelen
if not range or len(range) == 0:
break
for entry in range:
entry=entry.decode('utf-8')
minlen = min(len(entry),len(prefix))
if entry[0:minlen] != prefix[0:minlen]:
count = len(results)
break
if entry[-1] == "*" and len(results) != count:
results.append(entry[0:-1])

return jsonify(results)
'''
Start the Application through cmd:
export FLASK_APP=<path to python file>/auto-complete_redis.py
flask run
'''

--

--

--

Story Teller and Data enthusiast

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

[Leet Code] Average

Jupiter 2021 Recap

NEW RAFFLE FEATURE ALERT

Cocoapods integration in Kotlin Multiplatform project

Hackthebox Walkthrough Popcorn #4

Popcorn is Linux Based HTB Machine!! WE will be owning it!

Freedom of Choice with Flutter

Stack of phones and tablets of different sizes.

Low code serverless integration with Kfaka

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Japneet Singh Chawla

Japneet Singh Chawla

Story Teller and Data enthusiast

More from Medium

How to avoid file name collisions in pytest

GraphQL with Python — Part 1

Django Model implementation (Part II)

PAGINATION USING PYTHON, REST API, FLASK & MONGODB