Commit 59730e1f authored by lixiaofang's avatar lixiaofang

add

parent e448f56f
......@@ -208,3 +208,27 @@ class ESPerform(object):
except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return {"total_count": 0, "hits": []}
@classmethod
def es_indices_analyze(cls, doc_type, body, es):
if es is None:
es = ESPerform.get_cli(settings.GM_ORI_ES_INFO_LIST)
index = ESPerform.es_index_adapt(
index_prefix=settings.ES_INDEX_PREFIX,
doc_type=doc_type,
rw='read'
)
res = es.indices.analyze(index=index, body=body)
return res
@classmethod
def es_index_adapt(cls, index_prefix, doc_type, rw=None):
"""get the adapted index name
"""
assert rw in [None, 'read', 'write']
index = '-'.join((index_prefix, doc_type))
if rw:
index = '-'.join((index, rw))
return index
......@@ -101,13 +101,22 @@ def get_suggest_tips(query, lat, lng, offset=0, size=50):
for i in labels:
ori = i.split(":")[0]
result_num = i.split(":")[1]
ori_name = base64.b64decode(ori.encode('utf8')).decode('utf8')
ret_list.append(
{"results_num": result_num, "ori_name": ori_name, "id": None, "is_online": True, "offline_score": 0,
"type_flag": get_tips_word_type(ori_name), "highlight_name": ori_name, "describe": ""})
if ori not in have_read_tips_set:
result_num = i.split(":")[1]
ori_name = base64.b64decode(ori.encode('utf8')).decode('utf8')
ret_list.append(
{"results_num": result_num, "ori_name": ori_name, "id": None, "is_online": True,
"offline_score": 0,
"type_flag": get_tips_word_type(ori_name), "highlight_name": ori_name, "describe": ""})
return ret_list
except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return list()
......@@ -77,25 +77,25 @@ CELERYBEAT_SCHEDULE = {
# }
# ]
#
# GM_ORI_ES_INFO_LIST = [
# {
# "host": "101.200.54.249",
# "port": 9200
# }
# ]
ES_INFO_LIST = [
GM_ORI_ES_INFO_LIST = [
{
"host": "127.0.0.1",
"host": "101.200.54.249",
"port": 9200
}
]
GM_ORI_ES_INFO_LIST = [
ES_INFO_LIST = [
{
"host": "127.0.0.1",
"port": 9200
}
]
#
# GM_ORI_ES_INFO_LIST = [
# {
# "host": "127.0.0.1",
# "port": 9200
# }
# ]
ES_INDEX_PREFIX = "gm_test"
# 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
......@@ -205,56 +204,3 @@ 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