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
Owner
Giap Le
Full-stack dev, Solution Architect in SaaS, PaaS, ML, DS, Mobile App, IoT, Autonomous
Giap Le
Strawberry-django-plus - Enhanced Strawberry GraphQL integration with Django

strawberry-django-plus Enhanced Strawberry integration with Django. Built on top

BLB Ventures 138 Dec 28, 2022
The friendly PIL fork (Python Imaging Library)

Pillow Python Imaging Library (Fork) Pillow is the friendly PIL fork by Alex Clark and Contributors. PIL is the Python Imaging Library by Fredrik Lund

Pillow 10.4k Jan 03, 2023
A Django app to initialize Sentry client for your Django applications

Dj_sentry This Django application intialize Sentry SDK to your Django application. How to install You can install this packaging by using: pip install

Gandi 1 Dec 09, 2021
Inject an ID into every log message from a Django request. ASGI compatible, integrates with Sentry, and works with Celery

Django GUID Now with ASGI support! Django GUID attaches a unique correlation ID/request ID to all your log outputs for every request. In other words,

snok 300 Dec 29, 2022
Django URL Shortener is a Django app to to include URL Shortening feature in your Django Project

Django URL Shortener Django URL Shortener is a Django app to to include URL Shortening feature in your Django Project Install this package to your Dja

Rishav Sinha 4 Nov 18, 2021
Bootstrap 3 integration with Django.

django-bootstrap3 Bootstrap 3 integration for Django. Goal The goal of this project is to seamlessly blend Django and Bootstrap 3. Want to use Bootstr

Zostera B.V. 2.3k Jan 03, 2023
A Django app that allows visitors to interact with your site as a guest user without requiring registration.

django-guest-user A Django app that allows visitors to interact with your site as a guest user without requiring registration. Largely inspired by dja

Julian Wachholz 21 Dec 17, 2022
Automatic class scheduler for Texas A&M written with Python+Django and React+Typescript

Rev Registration Description Rev Registration is an automatic class scheduler for Texas A&M, aimed at easing the process of course registration by gen

Aggie Coding Club 21 Nov 15, 2022
A simple djagno music website.

Mrock A simple djagno music website. I used this template and I translated it to eng. Also some changes commited. My Live Domo : https://mrock.pythona

Hesam N 1 Nov 30, 2021
The new Python SDK for Sentry.io

Bad software is everywhere, and we're tired of it. Sentry is on a mission to help developers write better software faster, so we can get back to enjoy

Sentry 1.4k Jan 05, 2023
A handy tool for generating Django-based backend projects without coding. On the other hand, it is a code generator of the Django framework.

Django Sage Painless The django-sage-painless is a valuable package based on Django Web Framework & Django Rest Framework for high-level and rapid web

sageteam 51 Sep 15, 2022
Automatic caching and invalidation for Django models through the ORM.

Cache Machine Cache Machine provides automatic caching and invalidation for Django models through the ORM. For full docs, see https://cache-machine.re

846 Nov 26, 2022
Simple tagging for django

django-taggit This is a Jazzband project. By contributing you agree to abide by the Contributor Code of Conduct and follow the guidelines. django-tagg

Jazzband 3k Jan 02, 2023
A fresh approach to autocomplete implementations, specially for Django.

A fresh approach to autocomplete implementations, specially for Django. Status: v3 stable, 2.x.x stable, 1.x.x deprecated. Please DO regularely ping us with your link at #yourlabs IRC channel

YourLabs 1.6k Dec 22, 2022
Declarative model lifecycle hooks, an alternative to Signals.

Django Lifecycle Hooks This project provides a @hook decorator as well as a base model and mixin to add lifecycle hooks to your Django models. Django'

Robert Singer 1k Dec 31, 2022
Wrapping Raml around Django rest-api's

Ramlwrap is a toolkit for Django which allows a combination of rapid server prototyping as well as enforcement of API definition from the RAML api. R

Jmons 8 Dec 27, 2021
Django-pwned - A collection of django password validators

Django Pwned A collection of django password validators. Compatibility Python: 3

Quera 22 Jun 27, 2022
Login System Django

Login-System-Django Login System Using Django Tech Used Django Python Html Run Locally Clone project git clone https://link-to-project Get project for

Nandini Chhajed 6 Dec 12, 2021
Django Persistent Filters is a Python package which provide a django middleware that take care to persist the querystring in the browser cookies.

Django Persistent Filters Django Persistent Filters is a Python package which provide a django middleware that take care to persist the querystring in

Lorenzo Prodon 2 Aug 05, 2022
A prettier way to see Django requests while developing

A prettier way to see Django requests while developing

Adam Hill 35 Dec 02, 2022