Commit 92ebf11d authored by lixiaofang's avatar lixiaofang

add

parent 9653ed9f
...@@ -38,8 +38,6 @@ def get_suggest_tips(query, lat, lng, offset=0, size=50): ...@@ -38,8 +38,6 @@ def get_suggest_tips(query, lat, lng, offset=0, size=50):
# #
# logging.info("get whole_query:%s" % whole_query) # logging.info("get whole_query:%s" % whole_query)
# hits = whole_query["hits"]["hits"] # hits = whole_query["hits"]["hits"]
# #
# for item in hits: # for item in hits:
......
...@@ -152,42 +152,37 @@ class WordResemble(object): ...@@ -152,42 +152,37 @@ class WordResemble(object):
@classmethod @classmethod
def set_data_to_redis(cls, instance): def set_data_to_redis(cls, instance):
try: try:
keyword_value = []
QUERY_KEY = "query:{}:search_tip" QUERY_KEY = "query:search_tip"
tips_num_redis_key_prefix = "search_tips:tips_mapping_num" tips_num_redis_key_prefix = "search_tips:tips_mapping_num"
query_sql_item = wordresemble.WordRel.objects.filter(keyword=instance.keyword) query_sql_item = wordresemble.WordRel.objects.filter(keyword=instance.keyword)
for sql_obj in query_sql_item: for sql_obj in query_sql_item:
count = 0
words = list(sql_obj.all_resembles.all().values_list('word', flat=True)) words = list(sql_obj.all_resembles.all().values_list('word', flat=True))
query_base64 = base64.b64encode(instance.keyword.encode('utf8')).decode('utf8')
for items in words: for items in words:
count += 1
wordresemble_value = []
# 先存储本体词 # 先存储本体词
query_base64 = base64.b64encode(instance.keyword.encode('utf8')).decode('utf8') item_name = base64.b64encode(items.encode('utf8')).decode('utf8')
key = QUERY_KEY.format(query_base64) # 获取该同义词的结果
value_name = base64.b64encode(items.encode('utf8')).decode('utf8')
# 获取本体词的结果
redis_data = redis_client.hget(tips_num_redis_key_prefix, items) redis_data = redis_client.hget(tips_num_redis_key_prefix, items)
logging.info("get redis_data:%s" % redis_data) logging.info("get redis_data:%s" % redis_data)
redis_val_dict = json.loads(str(redis_data, encoding="utf-8")) if redis_data else {} redis_val_dict = json.loads(str(redis_data, encoding="utf-8")) if redis_data else {}
total_count = 0 total_count = 0
if 't' in redis_val_dict: if 't' in redis_val_dict:
total_count = int(redis_val_dict['t']) total_count = int(redis_val_dict['t'])
# 存储到Redis value = {item_name: total_count}
value = value_name + str(":" + str(total_count)) keyword_value.append(value)
redis_client.sadd(key, value)
# 存储近义词
# query_base64 = base64.b64encode(items.encode('utf8')).decode('utf8')
key = QUERY_KEY.format(value_name)
# value_name = base64.b64encode(wordrm["keyword"].encode('utf8')).decode('utf8')
value_names = query_base64
# 获取结果
redis_data = redis_client.hget(tips_num_redis_key_prefix, instance.keyword) redis_data = redis_client.hget(tips_num_redis_key_prefix, instance.keyword)
redis_val_dict = json.loads(str(redis_data, encoding="utf-8")) if redis_data else {} redis_val_dict = json.loads(str(redis_data, encoding="utf-8")) if redis_data else {}
total_count = 0 total_count = 0
if 't' in redis_val_dict: if 't' in redis_val_dict:
total_count = int(redis_val_dict['t']) total_count = int(redis_val_dict['t'])
# 存储结果 value = {query_base64: total_count}
value = value_names + str(":" + str(total_count)) wordresemble_value.append(value)
redis_client.sadd(key, value) if count == 1:
keyword_value.append(value)
if words: if words:
for w in words: for w in words:
value_name_w = base64.b64encode(w.encode('utf8')).decode('utf8') value_name_w = base64.b64encode(w.encode('utf8')).decode('utf8')
...@@ -196,8 +191,11 @@ class WordResemble(object): ...@@ -196,8 +191,11 @@ class WordResemble(object):
total_count = 0 total_count = 0
if 't' in redis_val_dict: if 't' in redis_val_dict:
total_count = int(redis_val_dict['t']) total_count = int(redis_val_dict['t'])
value = value_name_w + str(":" + str(total_count)) value = {value_name_w: total_count}
redis_client.sadd(key, value) wordresemble_value.append(value)
if len(wordresemble_value) > 0:
redis_client.hset(QUERY_KEY, item_name, json.dumps(wordresemble_value))
if len(keyword_value) > 0:
redis_client.hset(QUERY_KEY, query_base64, json.dumps(keyword_value))
except: except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc()) logging.error("catch exception,err_msg:%s" % traceback.format_exc())
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