2

I want to search inside the payload of a value in Redis using Flask.

This is the code that I have written

# to fetch value:
localhost/suggestions?prefix=<name>
@app.route('/get')
def get():
    try:
        prefix = request.args.get('prefix')
        n = prefix.strip()
        result= ac.get_suggestions(n, fuzzy = True,with_payloads=True) 
        return(json.dumps([{'id': item.string, 'score': item.score,'payload':item.payload} for item in result]))
    except Exception as e:
        print(e)

I tried to search for a value present in the payload, but an empty list was returned.

I went through the following things:

EDIT:

imports:

import redis
import redisearch
import json
import string
from flask import Flask
import logging
#flask to expose api's to outside world
from flask import Flask,request,jsonify,render_template
from flask_restx import Resource
from redis import Redis, ConnectionPool
from redisearch.auto_complete import AutoCompleter,Suggestion
from _util import to_string
ac = AutoCompleter('ac')
app = Flask("autocomplete")

New value add:

# localhost/add?name=<name>
def add():
    try:
        name = request.args.get('name')
        n = name.strip()
        payload = {
                "data": {
                    "value": "1",
                }
            }
        ac.add_suggestions(Suggestion(n,1.0,json.dumps(payload )))

        return "Added Successfully"
    except Exception as e:
        print(e)
        return "Addition Failed"

I have even tried searching on the web but was unable to find a suitable solution.

Any help is appreciated.

Rahul
  • 39
  • 1
  • 5
  • This question would be better with the Flask aspect taken out, as it's irrelevant, and an example of how to *insert your sample data to redis*, so that people can re-create the problem where the empty list appears. That would make the question a [Minimal Reproducable Example](https://stackoverflow.com/help/minimal-reproducible-example) – v25 Sep 22 '21 at 19:09
  • Please back the `import` statements so we know what `ac` is. Thank you. – Mark Setchell Sep 22 '21 at 19:37
  • @MarkSetchell done with that – Rahul Sep 23 '21 at 13:20

1 Answers1

0

The only fault I can see in your code is that you reference the variable n in two places, when perhaps you mean prefix and name. For example in the function add:

ac.add_suggestions(Suggestion(n,1.0,json.dumps(payload )))

Looks like it should actually use name as submitted by the user:

ac.add_suggestions(Suggestion(name ,1.0,json.dumps(payload )))

Then in your get function, use prefix:

result = ac.get_suggestions(prefix, fuzzy = True,with_payloads=True) 

I'm confused as to why this doesn't throw an exception (unless you have n declared as a global var somewhere else in your code).


I tried some code in my environment with: RediSearch version 2.0.12...

>>> from redisearch.auto_complete import AutoCompleter,Suggestion
>>> ac = AutoCompleter('ac')
>>> payload = {
...         "data": {
...             "value": "1",
...         }
...     }

>>> words = ['aoo','boo','soo','rep','mep']
>>> for word in words:
...     ac.add_suggestions(Suggestion(word, 1.0, json.dumps(payload )))
... 

>>> res = ac.get_suggestions('oo', fuzzy = True,with_payloads=True) 
>>> res
[soo, boo, aoo]

Now for your list comprehension:

>>> from pprint import pprint
>>> pprint([{'id': item.string, 'score': item.score,'payload':item.payload} for item in res])
[{'id': 'soo', 'payload': '{"data": {"value": "1"}}', 'score': 1.0},
 {'id': 'boo', 'payload': '{"data": {"value": "1"}}', 'score': 1.0},
 {'id': 'aoo', 'payload': '{"data": {"value": "1"}}', 'score': 1.0},]

This appears to return the payload for every hit.

v25
  • 7,096
  • 2
  • 20
  • 36
  • I was getting the payload , the issue with n I think is that when I copied that code , that particular line got skipped. What i actually want is that to be able to search inside the payload data – Rahul Sep 24 '21 at 08:45
  • Ex: if I enter the prefix as 'data' then I want all the items which have 'data' inside the payload to be reflected. – Rahul Sep 24 '21 at 09:21