Commit e448f56f authored by lixiaofang's avatar lixiaofang

add

parent a955f0d8
# coding=utf8
from __future__ import unicode_literals, absolute_import, print_function
from libs.es import ESPerform
import logging
import traceback
import json
from django.conf import settings
from libs.cache import redis_client
from trans2es.models.query_word_conversion import TagConversion, DoctorConversion, HospitalConversion
......@@ -204,3 +205,56 @@ def get_tips_word_type(query=''):
labels.sort(key=label_key, reverse=True)
return labels[0]
def label_key(label):
return LABEL_VALUE.get(label)
def get_tips_word_type(query=''):
try:
labels = list()
query_base64 = base64.b64encode(query.encode('utf8')).decode('utf8')
key = QUERY_KEY.format(query_base64)
logging.info("get key:%s" % key)
labels = list(map(lambda x: x.decode("utf8"), list(redis_client.smembers(key))))
logging.info("get labels:%s" % labels)
if len(labels) == 0:
labels = list(get_synonym_query(query))
labels.append(MIND_TYPE.UNKNOWN)
if query in QUERY_WORD_LABEL_NEED_MODIFIED:
labels.append(MIND_TYPE.PROJECT)
labels.sort(key=label_key, reverse=True)
return {'label': labels[0]}
except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return {'label': MIND_TYPE.UNKNOWN}
def get_synonym_query(query=''):
try:
synonym_term_set = set()
synonym_term_set_labels = set()
body = {
'text': query,
'analyzer': "gm_default_index"
}
res = ESPerform.get_search_results(ESPerform.get_cli(settings.GM_ORI_ES_INFO_LIST),
sub_index_name="newwiki", doc_type="newwiki", query_body=body)
logging.info("get res:%s" % res)
for item in res["tokens"]:
if item["type"] == "SYNONYM":
synonym_term_set.add(item["token"])
for query in synonym_term_set:
query_base64 = base64.b64encode(query.encode('utf8')).decode('utf8')
key = QUERY_KEY.format(query_base64)
labels = list(map(lambda x: x.decode("utf8"), list(redis_client.smembers(key))))
logging.info("get labels synonym:%s" % labels)
for i in labels:
synonym_term_set_labels.add(i)
return synonym_term_set_labels
except:
logging.error("catch exception, query_sku:%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