Commit 6d400c45 authored by Nicolas Gelot's avatar Nicolas Gelot

Unify the redis key generation

SearchData is composed by SearchQuery field, so we use inheritance
in order to use the same field naming. In that way we can use the an
uniq way to build our redis key.
parent d8c97adf
......@@ -158,14 +158,14 @@ class RawTextQuery(object):
return ''.join(self.query_parts)
class SearchQuery(object):
class SearchQuery:
"""container for all the search parameters (query, language, etc...)"""
def __init__(self, query, engines, categories, lang, safesearch, pageno, time_range):
def __init__(self, query, engines, categories, language, safesearch, pageno, time_range):
self.query = query
self.engines = engines
self.categories = categories
self.lang = lang
self.language = language
self.safesearch = safesearch
self.pageno = pageno
self.time_range = time_range
......
......@@ -4,6 +4,7 @@ from operator import itemgetter
from threading import RLock
from searx.engines import engines
from searx.url_utils import urlparse, unquote
from searx.query import SearchQuery
CONTENT_LEN_IGNORED_CHARS_REGEX = re.compile(r'[,;:!?\./\\\\ ()-_]', re.M | re.U)
......@@ -319,16 +320,11 @@ class ResultContainer(object):
self.unresponsive_engines.add(engine_error)
class SearchData(object):
class SearchData(SearchQuery):
def __init__(self, search_query, results, paging,
results_number, answers, corrections, infoboxes, suggestions, unresponsive_engines):
self.categories = search_query.categories
self.query = search_query.query
self.pageno = search_query.pageno
self.safe_search = search_query.safesearch
self.language = search_query.lang
self.time_range = search_query.time_range
self.engines = search_query.engines
super().__init__(**search_query.__dict__)
self.results = results
self.paging = paging
self.results_number = results_number
......
......@@ -268,7 +268,7 @@ class Search(object):
if hasattr(engine, 'language') and engine.language:
request_params['language'] = engine.language
else:
request_params['language'] = search_query.lang
request_params['language'] = search_query.language
# 0 = None, 1 = Moderate, 2 = Strict
request_params['safesearch'] = search_query.safesearch
......
......@@ -12,18 +12,28 @@ from searx.results import SearchData
from searx import settings
def make_key(q):
if q.time_range is None:
q.time_range = ""
return "SEARCH_HISTORY:{}:{}:{}:{}:{}:{}:{}".format(
e(q.query),
je(q.engines),
q.categories[0],
q.language,
q.safesearch,
q.pageno,
q.time_range,
)
def _get_connection(decode_responses=True):
return redis.StrictRedis(host=settings['redis']['host'], decode_responses=decode_responses)
def read(q):
time_range = q.time_range
if q.time_range is None:
q.time_range = ""
conn = _get_connection()
key = "SEARCH_HISTORY:{}:{}:{}:{}:{}:{}:{}".format(
e(q.query), je(q.engines), q.categories[0], q.lang, q.safesearch, q.pageno, time_range)
key = make_key(q)
response = conn.hgetall(key)
if not response:
return None
......@@ -37,10 +47,9 @@ def read(q):
def save(d):
conn = _get_connection()
key = "SEARCH_HISTORY:{}:{}:{}:{}:{}:{}:{}".format(
e(d.query), je(d.engines), d.categories[0], d.language, d.safe_search, d.pageno, d.time_range)
key = make_key(d)
mapping = {
'query': e(d.query), 'category': d.categories[0], 'pageno': d.pageno, 'safe_search': d.safe_search,
'query': e(d.query), 'category': d.categories[0], 'pageno': d.pageno, 'safesearch': d.safesearch,
'language': d.language, 'time_range': d.time_range, 'engines': je(d.engines), 'results': je(d.results),
'paging': d.paging, 'results_number': d.results_number, 'answers': jes(d.answers),
'corrections': jes(d.corrections), 'infoboxes': je(d.infoboxes), 'suggestions': jes(d.suggestions),
......@@ -64,7 +73,7 @@ def get_twenty_queries(x):
output = pipe.execute()
for row in output:
result.append(SearchQuery(d(row['query']), jd(row['engines']), [row['category']], row['language'],
int(row['safe_search']), int(row['pageno']), row['time_range']))
int(row['safesearch']), int(row['pageno']), row['time_range']))
return result
......@@ -95,8 +104,7 @@ def jds(coded):
def update(d):
conn = _get_connection(decode_responses=False)
key = "SEARCH_HISTORY:{}:{}:{}:{}:{}:{}:{}".format(
e(d.query), je(d.engines), d.categories[0], d.language, d.safe_search, d.pageno, d.time_range)
key = make_key(d)
current = conn.hgetall(key)
current.update({
'results': je(d.results), 'paging': d.paging, 'results_number': d.results_number,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment