RediSearch / redisearch-py

RediSearch python client
https://redisearch.io
BSD 2-Clause "Simplified" License
221 stars 63 forks source link

How to configure the redisearch in flask? #111

Closed cnvsuresh closed 3 years ago

cnvsuresh commented 3 years ago

I've used the example which is available in the readme file. I've missed the configuration setup. Because of no configuration, it's showing the below error:

redis.exceptions.ResponseError redis.exceptions.ResponseError: unknown command FT.SEARCH, with args beginning with: myIndex, search engine, LIMIT, 0, 10,

Can you please help me on this?

gkorland commented 3 years ago

How did you start the RediSearch server?

cnvsuresh commented 3 years ago

I've installed the redisearch-py by using pip in flask. And then added the example in code.

from flask_restful import Resource
from redisearch import Client, TextField, IndexDefinition, Query

class RedisClass(Resource):
    @staticmethod
    def post():
        # Creating a client with a given index name
        client = Client("myIndex")

        # IndexDefinition is available for RediSearch 2.0+
        definition = IndexDefinition(prefix=['doc:', 'article:'])

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

        # Indexing a document for RediSearch 2.0+
        client.redis.hset('doc:1',
                          mapping={
                              'title': 'RediSearch',
                              'body': 'Redisearch impements a search engine on top of redis'
                          })

        # Indexing a document for RediSearch 1.x
        client.add_document(
            "doc:2",
            title="RediSearch",
            body="Redisearch implements a search engine on top of redis",
        )
        return True

    @staticmethod
    def get():
        # Creating a client with a given index name
        client = Client("myIndex")

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

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

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

After that i'm trying to call the get/post method from API level.

And started the docker image by using the below command docker run -p 6379:6379 redislabs/redisearch:latest

abrookins commented 3 years ago

Usually that error means that the Redis server you're trying to use does not have the redisearch module installed. @cnvsuresh How did you start the Redis server, or where is it running?

cnvsureshg commented 3 years ago

I've added the Redis Caching by using the Flask-Caching in the project. There only I've added Redis configuration to connect. But related to redisearch I didn't mention any configuration to connect.

How can I provide Redisearch configuration?

cnvsuresh commented 3 years ago

Can I use this library with enterprise Redis without using the docker image which is provided from this library?

abrookins commented 3 years ago

@cnvsuresh You can, yes. If you're using Redis Enterprise Cloud, then these docs will help you turn on RediSearch: https://docs.redislabs.com/latest/modules/modules-quickstart/

You should be able to use the Docker image in the way you mentioned earlier for local testing: start it with the command you used and connect to it with the code in your example, so if that's not working, your Flask code may be connecting to the wrong Redis instance -- not the one you started with our RediSearch Docker image.

cnvsuresh commented 3 years ago

Thanks, @abrookins Now all set. I'm able to use the enterprise account instead of docker. Thanks for the help.

In the future, if is there any case to store the array of objects in redisearch. let us know it'll really help.