Py-instant-search-redis - Source code example for how to build an instant search with redis in python

Overview

Build Status License Python version

py-instant-search-redis

Source code example for how to build an instant search (or real-time search) function with Redis in python.

This short source code will helps you understand about Redis Search feature and how to implement it into your project ideas (eg: use real-time search function for any ecommerce website like Book/Movie/Product/etc..) helps the user can search a product by tite/name/price/origin/category/... very quickly. Especially, this function can combine multiple search condition together which helps the system return the result with high accuracy.

Technical stack:

  1. Programming language: Python
  2. Backend server: Flask
  3. Redis (use redis cli and Redis server + Redisearch lib):
  4. Data source: you can access data source from MongoDB, MySQL, Postgres, etc and then index it into redis memory (I don't demo it here with this version)

Deploy/Running environment

  1. OS: Linux (Ubuntu or CentOS or other)
  2. If you want to use/try it on Windows, you can use Docker (with a linux image)

Prerequisite

  1. Install python environment
  2. Install Flask (https://flask.palletsprojects.com/en/2.0.x/installation/)
  3. Install Docker Engine on your development host if you want to run code or redis server by Docker container
  • Withow Docker engine desktop (on MacOS):
# Install hyperkit and minikube
brew install hyperkit
brew install minikube

# Install Docker CLI
brew install docker
brew install docker-compose

# Start minikube
minikube start

# Tell Docker CLI to talk to minikube's VM
eval $(minikube docker-env)

# Save IP to a hostname
echo "`minikube ip` docker.local" | sudo tee -a /etc/hosts > /dev/null

# Test
docker run hello-world
  1. Install Redis client and Redis server:
  • Redis Server (includes RedisSearch library)

You can run Redis server in a Docker container instead of run in on your host (this method help you install Redis quickly) Redis server docker here (includes redis search library): docker run -p 6379:6379 redislabs/redisearch:latest

Example code after installed Redis Client library

More about redisearch API here More about redis command/synctax here

from redisearch import Client, TextField, NumericField, Query

# Creating a client with a given index name
client = Client('myIndex')  #you can set any index name you want

# Creating the index definition and schema
client.create_index([TextField('title', weight=5.0), TextField('body')])

# Indexing a document
client.add_document('doc1', title = 'RediSearch', body = 'Redisearch implements a search engine on top of redis')

# Simple search
res = client.search("search engine")

# the result has the total number of results, and a list of documents
print res.total # "1"
print res.docs[0].title

# Searching with snippets
res = client.search("search engine", snippet_sizes = {'body': 50})

# Searching with complex parameters:
q = Query("search engine").verbatim().no_content().paging(0,5)
res = client.search(q)

My development environment:

  1. Laptop/PC: MacOS BigSur
  2. Python version: python3.8
  3. Install Redis Cli for Mac
  • Redis cli helps we can connect and interact with Redis server from command-line

We can use FT._LIST to list all existing indexs on the Redis server (or create, delete, view an index information) Check more about redis search command here (https://oss.redis.com/redisearch/Commands/)

  • Install steps on MacOS's terminal
brew update
brew install redis

//To check
redis-cli ping
//if you are getting PONG Then you are good to go

Now is time to start to code

  • Getting started >>

Now is time to start the server

#Server running functions =================================================================>
if __name__ == "__main__":
    app.run(host='0.0.0.0', port=5000, debug=True, threaded=True)

Run server on your host:

python3 main.py
You might also like...
Build reusable components in Django without writing a single line of Python.

Build reusable components in Django without writing a single line of Python. {% #quote %} {% quote_photo src="/project-hail-mary.jpg" %} {% #quot

Forgot password functionality build in Python / Django Rest Framework

Password Recover Recover password functionality with e-mail sender usign Django Email Backend How to start project. Create a folder in your machine Cr

Django project starter on steroids: quickly create a Django app AND generate source code for data models + REST/GraphQL APIs (the generated code is auto-linted and has 100% test coverage).

Create Django App 💛 We're a Django project starter on steroids! One-line command to create a Django app with all the dependencies auto-installed AND

An API was build with Django to store and retrieve information about various musical instruments.

The project is meant to be a starting point, an experimentation or a basic example of a way to develop an API with Django. It is an exercise on using Django and various python technologies and design methodologies.

A modern looking portfolio build with Django.
A modern looking portfolio build with Django.

Django Portfolio A portfolio template using html/css/js in the frontend and Django as the backend framework. Cool features: smooth scrolling responsiv

Source code for Django for Beginners 3.2
Source code for Django for Beginners 3.2

The official source code for https://djangoforbeginners.com/. Available as an ebook or in Paperback. If you have the 3.1 version, please refer to this

Book search Django web project that uses requests python library and openlibrary API.
Book search Django web project that uses requests python library and openlibrary API.

Book Search API Developer: Vladimir Vojtenko Book search Django web project that uses requests python library and openlibrary API. #requests #openlibr

This Django app will be used to host Source.Python plugins, sub-plugins, and custom packages.

Source.Python Project Manager This Django app will be used to host Source.Python plugins, sub-plugins, and custom packages. Want to help develop this

Modular search for Django

Haystack Author: Daniel Lindsley Date: 2013/07/28 Haystack provides modular search for Django. It features a unified, familiar API that allows you to

Owner
Giap Le
Full-stack dev, Solution Architect in SaaS, PaaS, ML, DS, Mobile App, IoT, Autonomous
Giap Le
Show how the redis works with Python (Django).

Redis Leaderboard Python (Django) Show how the redis works with Python (Django). Try it out deploying on Heroku (See notes: How to run on Google Cloud

Tom Xu 4 Nov 16, 2021
This is raw connection between redis server and django python app

Django_Redis This repository contains the code for this blogpost. Running the Application Clone the repository git clone https://github.com/xxl4tomxu9

Tom Xu 1 Sep 15, 2022
Full featured redis cache backend for Django.

Redis cache backend for Django This is a Jazzband project. By contributing you agree to abide by the Contributor Code of Conduct and follow the guidel

Jazzband 2.5k Jan 3, 2023
A simple app that provides django integration for RQ (Redis Queue)

Django-RQ Django integration with RQ, a Redis based Python queuing library. Django-RQ is a simple app that allows you to configure your queues in djan

RQ 1.6k Jan 6, 2023
A Redis cache backend for django

Redis Django Cache Backend A Redis cache backend for Django Docs can be found at http://django-redis-cache.readthedocs.org/en/latest/. Changelog 3.0.0

Sean Bleier 1k Dec 15, 2022
A Django chatbot that is capable of doing math and searching Chinese poet online. Developed with django, channels, celery and redis.

Django Channels Websocket Chatbot A Django chatbot that is capable of doing math and searching Chinese poet online. Developed with django, channels, c

Yunbo Shi 8 Oct 28, 2022
Quick example of a todo list application using Django and HTMX

django-htmx-todo-list Quick example of a todo list application using Django and HTMX Background Modified & expanded from https://github.com/jaredlockh

Jack Linke 54 Dec 10, 2022
Example project demonstrating using Django’s test runner with Coverage.py

Example project demonstrating using Django’s test runner with Coverage.py Set up with: python -m venv --prompt . venv source venv/bin/activate python

Adam Johnson 5 Nov 29, 2021
An example of Django project with basic user functionality and account activation.

Simple Django Login and Registration An example of Django project with basic user functionality. Screenshots Log In Create an account Authorized page

Hussein Sarea 3 Oct 19, 2022
Django-shared-app-isolated-databases-example - Django - Shared App & Isolated Databases

Django - Shared App & Isolated Databases An app that demonstrates the implementa

Ajai Danial 5 Jun 27, 2022