pythonredisredis-py

Redis-py not returning consistent results for match all query


I'm attempting to add 100 documents to a Redis index, and then retrieving them with a match all query:

import uuid
import redis
from redis.commands.json.path import Path
import redis.commands.search.aggregation as aggregations
import redis.commands.search.reducers as reducers
from redis.commands.search.field import TextField, NumericField, TagField
from redis.commands.search.indexDefinition import IndexDefinition, IndexType
from redis.commands.search.query import NumericFilter, Query


r = redis.Redis(host="localhost", port=6379)
user4 = {
    "user": {
        "name": "Sarah Zamir",
        "email": "sarah.zamir@example.com",
        "age": 30,
        "city": "Paris",
    }
}

with r.pipeline(transaction=True) as pipe:
    for i, u in enumerate([user4] * 100):
        u["user"]["text"] = str(uuid.uuid4()) * 50
        r.json().set(f"user:{i}", Path.root_path(), u)
    pipe.execute()

schema = (
    TextField("$.user.name", as_name="name"),
    TagField("$.user.city", as_name="city"),
    NumericField("$.user.age", as_name="age"),
)
r.ft().create_index(
    schema, definition=IndexDefinition(prefix=["user:"], index_type=IndexType.JSON)
)

result = r.ft().search(Query("*").paging(0, 100))
print(result.total)

keys = r.keys("*")
print(len(keys))

r.flushall()
r.close()

I expect result.total to return 100, but it always returns inconsistent results less than 100. Am I doing something wrong? I checked the number of keys in redis and I get the correct count there.


Solution

  • Try to use pipe and add some time.sleep()

    import uuid
    import time
    import redis
    from redis.commands.json.path import Path
    from redis.commands.search.field import TextField, NumericField, TagField
    from redis.commands.search.indexDefinition import IndexDefinition, IndexType
    from redis.commands.search.query import NumericFilter, Query
    
    r = redis.Redis(host="localhost", port=6379)
    
    user4 = {
        "user": {
            "name": "Sarah Zamir",
            "email": "sarah.zamir@example.com",
            "age": 30,
            "city": "Paris",
        }
    }
    
    with r.pipeline(transaction=True) as pipe:
        for i, u in enumerate([user4] * 100):
            u["user"]["text"] = str(uuid.uuid4()) * 50
            pipe.json().set(f"user:{i}", Path.root_path(), u)
        pipe.execute()
    
    schema = (
        TextField("$.user.name", as_name="name"),
        TagField("$.user.city", as_name="city"),
        NumericField("$.user.age", as_name="age"),
    )
    
    r.ft().create_index(
        schema, definition=IndexDefinition(prefix=["user:"], index_type=IndexType.JSON)
    )
    
    while True:
        result = r.ft().search(Query("*"))
        if result.total == 100:
            break
        time.sleep(0.1)
    
    print(result.total)
    
    r.flushall()
    r.close()