Redis®*: Exemple : Utilisation avec Python
Cette documentation fait partie du guide Recherche et JSON. Consultez le guide complet ici : Stockez des documents JSON et interrogez-les efficacement avec le moteur de requêtes Redis.
👋 Bienvenue dans la documentation Stackhero !
Stackhero propose une solution Redis cloud clé en main qui offre de nombreux avantages, notamment :
- Interface web Redis Commander incluse.
- Taille et transferts de messages illimités.
- Mises à jour faciles en un seul clic.
- Performance optimale et sécurité renforcée grâce à une infrastructure privée et dédiée.
Gagnez du temps et simplifiez-vous la vie : il ne vous faut que 5 minutes pour essayer la solution d'hébergement Redis cloud de Stackhero !
import os
import redis
from redis.commands.search.field import TextField, TagField, NumericField
from redis.commands.search.index_definition import IndexDefinition, IndexType
from redis.commands.search.query import Query
r = redis.from_url(os.environ['STACKHERO_REDIS_URL_TLS'], decode_responses=True)
# Création de l'index au démarrage
try:
r.ft('productsIndex').create_index(
(
TextField('$.name', as_name='name'),
TagField('$.brand', as_name='brand'),
NumericField('$.price', as_name='price', sortable=True),
),
definition=IndexDefinition(prefix=['product:'], index_type=IndexType.JSON),
)
except redis.ResponseError as error:
if 'Index already exists' not in str(error):
raise
r.json().set('product:1', '$', {
'name': 'Espresso machine',
'brand': 'Bianca',
'price': 459,
})
results = r.ft('productsIndex').search(Query('@name:(espresso) @price:[0 500]'))
print(results.total, results.docs)