Redis®*: 例:Pythonでの利用

このドキュメントは検索とJSONガイドの一部です。完全なガイドはこちらからご覧いただけます:RedisクエリエンジンでJSONドキュメントを保存し、効率的にクエリを実行する

👋 Stackhero ドキュメントへようこそ!

Stackhero では、すぐに使える Redis cloud ソリューションをご提供しています。主な特長は以下の通りです:

  • Redis Commander Web UI を標準搭載。
  • メッセージサイズ・転送量が無制限
  • ワンクリックで簡単にアップデート可能。
  • プライベートかつ専用インフラによる最適なパフォーマンスと強固なセキュリティ

時間を節約し、運用をシンプルに:Stackhero の Redis cloud hosting ソリューションは、わずか5分でお試しいただけます!

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)

# 起動時にインデックスを作成
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)