Searching inside the Payload of redis

Viewed 149

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.

1 Answers

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.

Related