RediSearch / redisearch-py

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

Flask search query giving another index results. #117

Open cnvsuresh opened 3 years ago

cnvsuresh commented 3 years ago

Hi,

I'm trying to use this library. When i was searching for a key in the stored docs it's giving correctly. After that, I've tried to create another index from the same code. Without trying to store any docs in the new index if I'm trying to run, it's giving the previously created index results with this new index name surprisingly. Can I get help on that to solve this issue?

First index details `from flask import request from flask_restful import Resource from redisearch import Client, IndexDefinition, Query, TextField, TagField

index_name = 'myIndexQuery!' host = 'redis-xxxxxxxx.redislabs.com' port = 12906 password = 'xxxxxx'

class RedisClass(Resource):

@staticmethod
def put():
    client = Client(index_name, host=host, port=port, password=password)
    # Creating a client with a given index name
    # IndexDefinition is available for RediSearch 2.0+
    definition = IndexDefinition()

    # Creating the index definition and schema
    client.create_index((TextField("title", weight=5.0), TextField("body"), TagField("services")),
                        definition=definition)
    # client.alter_schema_add([TagField("description")])
    return True

@staticmethod
def post():
    client = Client(index_name, host=host, port=port, password=password)

    # Indexing a document for RediSearch 2.0+
    payload = request.get_json()
    doc_id = payload['doc_id']
    return client.redis.hset(doc_id, mapping=payload)

@staticmethod
def get():
    client = Client(index_name, host=host, port=port, password=password)
    print("Connected to %s" % client.__dict__)
    # add_document(title="meh, lol", tags="python,C")
    # Query("@title:meh @tags:{java}")
    query = request.args.get('query') if not ('type' in request.args) else "@description:{%s}" % request.args.get(
        'query')
    q = Query(query).with_scores().paging(0, 5)
    # .no_content()
    # .verbatim()
    res = client.search(q)
    # print("%s %s" % (query, json.dumps(res)))
    print("%s %s" % (query, res))
    # print(res.docs[0].id)
    for doc in res.docs:
        print("Ids %s" % doc.id)
    return True`

Second index details:

`from flask import request from flask_restful import Resource from redisearch import Client, IndexDefinition, Query, TextField, TagField

index_name = 'Gupta_myIndexQuery' host = 'redis-xxxxxx.redislabs.com' port = 12906 password = 'xxxxx'

class RedisSearchClass(Resource):

@staticmethod
def put():
    client = Client(index_name, host=host, port=port, password=password)
    # Creating a client with a given index name
    # IndexDefinition is available for RediSearch 2.0+
    definition = IndexDefinition()

    # Creating the index definition and schema
    client.create_index((TextField("title", weight=5.0), TextField("body"), TagField("services")),
                        definition=definition)
    # client.alter_schema_add([TagField("description")])
    return True

@staticmethod
def post():
    client = Client(index_name, host=host, port=port, password=password)

    # Indexing a document for RediSearch 2.0+
    payload = request.get_json()
    doc_id = payload['doc_id']
    return client.redis.hset(doc_id, mapping=payload)

@staticmethod
def get():
    client = Client(index_name, host=host, port=port, password=password)
    print("Connected to %s" % client.__dict__)
    # add_document(title="meh, lol", tags="python,C")
    # Query("@title:meh @tags:{java}")
    query = request.args.get('query') if not ('type' in request.args) else "@description:{%s}" % request.args.get(
        'query')
    q = Query(query).with_scores().verbatim().paging(0, 5)
    # .no_content()
    # .verbatim()
    res = client.search(q)
    # print("%s %s" % (query, json.dumps(res)))
    print("%s %s" % (query, res))
    # print(res.docs[0].id)
    for doc in res.docs:
        print("Ids %s" % doc.id)
    return True

`

cnvsuresh commented 3 years ago

I've found the Redis insight library. Installed and checked what are all the values exist. Surprisingly, I've seen all the docs without index-based. It's storing all the keys plainly in DB.

Is there any index name concept? If yes what I've done wrongly in this. As per the insight, the library looks like not exists.