Commit d8dba5ae authored by 李小芳's avatar 李小芳

Merge branch 'new_change' into 'master'

New change

See merge request !47
parents 2d063776 dfff527d
...@@ -58,7 +58,7 @@ class ESPerform(object): ...@@ -58,7 +58,7 @@ class ESPerform(object):
os.path.dirname(__file__), os.path.dirname(__file__),
'..', 'trans2es', 'mapping', '%s.json' % (doc_type,)) '..', 'trans2es', 'mapping', '%s.json' % (doc_type,))
if doc_type == "associate_tag" or doc_type == "associate_tag_brand" or doc_type == "associate_tag_doctor" \ if doc_type == "associate_tag" or doc_type == "associate_tag_brand" or doc_type == "associate_tag_doctor" \
or doc_type == "associate_tag_instrument" or doc_type == "associate_tag_hospital" or doc_type == "associate_tag_project"\ or doc_type == "associate_tag_instrument" or doc_type == "associate_tag_hospital" or doc_type == "associate_tag_project" \
or doc_type == "associate_tag_position": or doc_type == "associate_tag_position":
mapping_file_path = os.path.join( mapping_file_path = os.path.join(
os.path.dirname(__file__), os.path.dirname(__file__),
......
...@@ -5,7 +5,7 @@ from __future__ import unicode_literals, absolute_import, print_function ...@@ -5,7 +5,7 @@ from __future__ import unicode_literals, absolute_import, print_function
import logging import logging
import traceback import traceback
import json import json
import base64 import base64, hashlib
from libs.es import ESPerform from libs.es import ESPerform
from libs.cache import redis_client from libs.cache import redis_client
from libs.tools import g_hospital_pos_dict from libs.tools import g_hospital_pos_dict
...@@ -15,12 +15,54 @@ from trans2es.commons.words_utils import QueryWordAttr, get_tips_word_type ...@@ -15,12 +15,54 @@ from trans2es.commons.words_utils import QueryWordAttr, get_tips_word_type
from pypinyin import pinyin, lazy_pinyin from pypinyin import pinyin, lazy_pinyin
def get_suggest_tips(query, lat, lng, offset=0, size=50): def get_suggest_tips(query, lat, lng, offset=0, size=50, device_id=None):
"""
这个地方目前是有三个逻辑 分两个灰度验证
50%前段强加权后根据tab转化率和词频进行排序 索引 suggest gray_number=1 尾号["0", "1", "2", "3", "c", "d", "e", "f"]
25%去掉前段强加权后 直接根据tag转化率和词频进行排序 suggest-v1 gray_number=2 尾号["4", "5", "6", "a"]
25%去掉前段强加权后 根据tag转化率和词频进行排序 再根据前段加权展示排序 suggest-v1 gray_number=3 尾号["9", "8", "7", "b"]
:param query:
:param lat:
:param lng:
:param offset:
:param size:
:param device_id:
:return:
"""
try: try:
###在原来的逻辑上加两层灰度
gray_number = recommed_service_category_device_id(device_id)
if gray_number == 1:
sub_index_name = "suggest"
else:
sub_index_name = "suggest-v1"
have_read_tips_set, ret_list, result_dict = get_query_by_es(query=str(query).lower(), lat=lat, lng=lng, have_read_tips_set, ret_list, result_dict = get_query_by_es(query=str(query).lower(), lat=lat, lng=lng,
offset=offset, offset=offset,
size=size, highlight_query=query, size=size, highlight_query=query,
have_read_tips_set=set()) have_read_tips_set=set(),
sub_index_name=sub_index_name)
if gray_number == 3:
# 在去掉强加权的逻辑上根据词频和tag转化率排序后再前段强加权
front_data = []
end_data = []
equal_data = []
need_change_sort = ret_list[:30]
for item in need_change_sort:
ori_name = item.get("ori_name", None)
if query == ori_name:
equal_data.append(item)
elif query == ori_name[:len(query)]:
front_data.append(item)
else:
end_data.append(item)
equal_data.extend(front_data)
equal_data.extend(end_data)
equal_data.extend(ret_list[30:])
ret_list = equal_data
if len(ret_list) >= 50: if len(ret_list) >= 50:
return ret_list return ret_list
...@@ -92,7 +134,8 @@ def get_suggest_tips(query, lat, lng, offset=0, size=50): ...@@ -92,7 +134,8 @@ def get_suggest_tips(query, lat, lng, offset=0, size=50):
offset=offset, offset=offset,
size=size - len(ret_list), size=size - len(ret_list),
highlight_query=query, highlight_query=query,
have_read_tips_set=have_read_tips_set) have_read_tips_set=have_read_tips_set,
sub_index_name=sub_index_name)
ret_list.extend(pinyin_ret_list) ret_list.extend(pinyin_ret_list)
logging.info("get ret_list:%s" % ret_list) logging.info("get ret_list:%s" % ret_list)
...@@ -123,7 +166,7 @@ def set_highlihgt(query=None, ori_name=None): ...@@ -123,7 +166,7 @@ def set_highlihgt(query=None, ori_name=None):
def get_query_by_es(query='', lat=0, lng=0, size=0, offset=0, highlight_query=None, def get_query_by_es(query='', lat=0, lng=0, size=0, offset=0, highlight_query=None,
have_read_tips_set=None): have_read_tips_set=None, sub_index_name="suggest"):
try: try:
query = query.replace("\u2006", '') query = query.replace("\u2006", '')
...@@ -152,7 +195,7 @@ def get_query_by_es(query='', lat=0, lng=0, size=0, offset=0, highlight_query=No ...@@ -152,7 +195,7 @@ def get_query_by_es(query='', lat=0, lng=0, size=0, offset=0, highlight_query=No
ret_list = list() ret_list = list()
doctor_hospital_equal_query = list() doctor_hospital_equal_query = list()
tag_equal_query = list() tag_equal_query = list()
result_dict = ESPerform.get_search_results(ESPerform.get_cli(), sub_index_name="suggest", query_body=q, result_dict = ESPerform.get_search_results(ESPerform.get_cli(), sub_index_name=sub_index_name, query_body=q,
offset=offset, size=size, is_suggest_request=True) offset=offset, size=size, is_suggest_request=True)
for tips_item in result_dict["suggest"]["tips-suggest"]: for tips_item in result_dict["suggest"]["tips-suggest"]:
...@@ -218,3 +261,36 @@ def get_query_by_es(query='', lat=0, lng=0, size=0, offset=0, highlight_query=No ...@@ -218,3 +261,36 @@ def get_query_by_es(query='', lat=0, lng=0, size=0, offset=0, highlight_query=No
return have_read_tips_set, ret_list, result_dict return have_read_tips_set, ret_list, result_dict
except: except:
return set(), list(), list() return set(), list(), list()
def recommed_service_category_device_id(device_id):
try:
if device_id == "868771031984211":
return 2
elif device_id == "867961030707277":
return 3
else:
return 1
'''
设备品类显示, 是否命中灰度
'''
# categroy_select_cary1 = ["0", "1", "2", "3", "c", "d", "e", "f"]
# categroy_select_cary2 = ["4", "5", "6", "a"]
# categroy_select_cary3 = ["9", "8", "7", "b"]
#
# if not device_id:
# return 1
#
# hd_id = hashlib.md5(str(device_id).encode()).hexdigest()
# is_gray = hd_id[-1]
#
# if is_gray in categroy_select_cary2:
# return 2
# elif is_gray in categroy_select_cary3:
# return 3
# else:
# return 1
except:
return 1
...@@ -13,7 +13,7 @@ from search.utils.auto_tips import get_suggest_tips ...@@ -13,7 +13,7 @@ from search.utils.auto_tips import get_suggest_tips
@bind("search_tips/search/auto_complete_query") @bind("search_tips/search/auto_complete_query")
def auto_complete_query(q, lat, lng): def auto_complete_query(q, lat, lng, device_id=None):
try: try:
"""auto complate words/tags/doctors etc. """auto complate words/tags/doctors etc.
...@@ -28,7 +28,7 @@ def auto_complete_query(q, lat, lng): ...@@ -28,7 +28,7 @@ def auto_complete_query(q, lat, lng):
# if not q: # if not q:
# return json_http_response({'error': 0, 'data': []}) # return json_http_response({'error': 0, 'data': []})
data = get_suggest_tips(q, float(lat), float(lng)) data = get_suggest_tips(q, float(lat), float(lng), device_id=device_id)
result = { result = {
'error': 0, 'error': 0,
......
No preview for this file type
...@@ -115,3 +115,101 @@ def get_tips_suggest_list(instance_cn_name): ...@@ -115,3 +115,101 @@ def get_tips_suggest_list(instance_cn_name):
except: except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc()) logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return list() return list()
def get_tips_suggest_list_v1(instance_cn_name):
try:
# ch_full_weight = 6.0 * 1000
# py_full_weight = 3.0 * 1000
full_weight = 1
py_acronym_full_weight = 1
py_acronym_prefix_weight = 1
ch_prefix_weight = 1
py_prefix_weight = 1.0
# 命中开始部分加权
begin_prefix_weight = 1
ch_full_word = instance_cn_name.strip()
py_full_word = ''.join(lazy_pinyin(ch_full_word))
py_acronym_full_word = ''.join(lazy_pinyin(ch_full_word, style=pypinyin.FIRST_LETTER))
suggest_dict = dict()
cur_index = 0
# 中文
for i in range(len(ch_full_word)):
ch_name_term = ch_full_word[i:].strip()
if ch_name_term and ch_full_word[i] != "(" and ch_full_word[i] != ")":
prefix_weight = ch_prefix_weight if len(ch_name_term) != len(ch_full_word) else full_weight
suggest_type = 0 if len(ch_name_term) != len(ch_full_word) else 1
term_begin_prefix_weight = begin_prefix_weight if i == 0 else 1.0
suggest_item = {
"input": [ch_name_term],
"word_weight": (1.0 * len(ch_name_term) / len(
(ch_full_word))) * prefix_weight * term_begin_prefix_weight,
"suggest_type": suggest_type
}
if ch_name_term[0] not in suggest_dict:
cur_index += 1
suggest_item["cur_index"] = cur_index
suggest_dict[ch_name_term[0]] = suggest_item
else:
suggest_dict[ch_name_term[0]]["input"].append(ch_name_term)
if suggest_item["word_weight"] > suggest_dict[ch_name_term[0]]["word_weight"]:
suggest_dict[ch_name_term[0]]["word_weight"] = suggest_item["word_weight"]
suggest_dict[ch_name_term[0]]["suggest_type"] = suggest_item["suggest_type"]
# 拼音
if py_full_word != ch_full_word:
for i in range(len(py_full_word)):
py_name_term = py_full_word[i:].strip()
if py_name_term and py_full_word[i] != "(" and py_full_word[i] != ")":
prefix_weight = py_prefix_weight if len(py_name_term) != len(py_full_word) else full_weight
suggest_type = 2 if len(py_name_term) != len(py_full_word) else 3
term_begin_prefix_weight = begin_prefix_weight if i == 0 else 1.0
suggest_item = {
"input": [py_name_term],
"word_weight": (1.0 * len(py_name_term) / len(
py_full_word)) * prefix_weight * term_begin_prefix_weight,
"suggest_type": suggest_type
}
if py_name_term[0] not in suggest_dict:
cur_index += 1
suggest_item["cur_index"] = cur_index
suggest_dict[py_name_term[0]] = suggest_item
else:
suggest_dict[py_name_term[0]]["input"].append(py_name_term)
if suggest_item["word_weight"] > suggest_dict[py_name_term[0]]["word_weight"]:
suggest_dict[py_name_term[0]]["word_weight"] = suggest_item["word_weight"]
suggest_dict[py_name_term[0]]["suggest_type"] = suggest_item["suggest_type"]
# 简写
if py_acronym_full_word != py_full_word:
for i in range(len(py_acronym_full_word)):
py_acronym_term = py_acronym_full_word[i:].strip()
if py_acronym_term and py_acronym_full_word[i] != "(" and py_acronym_full_word[i] != ")":
prefix_weight = py_acronym_prefix_weight if len(py_acronym_term) != len(
py_acronym_full_word) else py_acronym_full_weight
suggest_type = 4 if len(py_acronym_term) != len(py_acronym_full_word) else 5
term_begin_prefix_weight = begin_prefix_weight if i == 0 else 1.0
suggest_item = {
"input": [py_acronym_term],
"word_weight": (1.0 * len(py_acronym_term) / len(
py_acronym_full_word)) * prefix_weight * term_begin_prefix_weight,
"suggest_type": suggest_type
}
if py_acronym_term[0] not in suggest_dict:
cur_index += 1
suggest_item["cur_index"] = cur_index
suggest_dict[py_acronym_term[0]] = suggest_item
else:
suggest_dict[py_acronym_term[0]]["input"].append(py_acronym_term)
if suggest_item["word_weight"] > suggest_dict[py_acronym_term[0]]["word_weight"]:
suggest_dict[py_acronym_term[0]]["word_weight"] = suggest_item["word_weight"]
suggest_dict[py_acronym_term[0]]["suggest_type"] = suggest_item["suggest_type"]
return suggest_dict.values()
except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return list()
...@@ -29,9 +29,10 @@ class WordRel(models.Model): ...@@ -29,9 +29,10 @@ class WordRel(models.Model):
class WordRelResemble(models.Model): class WordRelResemble(models.Model):
"""近义词数据表 """近义词数据表
""" """
class Meta: class Meta:
app_label = 'api' app_label = 'api'
db_table = 'api_wordrelresemble' db_table = 'api_wordrelresemble'
wordrel = models.ForeignKey(WordRel, related_name='all_resembles') wordrel = models.ForeignKey(WordRel, related_name='all_resembles')
word = models.CharField(u'近义词', max_length=50, db_index=True) word = models.CharField(u'近义词', max_length=50, db_index=True)
\ No newline at end of file
...@@ -14,14 +14,14 @@ import sys ...@@ -14,14 +14,14 @@ import sys
import copy import copy
from trans2es.models import doctor, itemwiki, collectwiki, brandwiki, productwiki, tag, wordresemble from trans2es.models import doctor, itemwiki, collectwiki, brandwiki, productwiki, tag, wordresemble
from trans2es.utils.doctor_transfer import DoctorTransfer from trans2es.utils.doctor_transfer import DoctorTransfer, DoctorTransferV1
from trans2es.utils.hospital_transfer import HospitalTransfer from trans2es.utils.hospital_transfer import HospitalTransfer, HospitalTransferV1
from trans2es.utils.itemwiki_transfer import ItemWikiTransfer from trans2es.utils.itemwiki_transfer import ItemWikiTransfer, ItemWikiTransferV1
from trans2es.utils.collectwiki_transfer import CollectWikiTransfer from trans2es.utils.collectwiki_transfer import CollectWikiTransfer, CollectWikiTransferV1
from trans2es.utils.brandwiki_transfer import BrandWikiTransfer from trans2es.utils.brandwiki_transfer import BrandWikiTransfer, BrandWikiTransferV1
from trans2es.utils.productwiki_transfer import ProduceWikiTransfer from trans2es.utils.productwiki_transfer import ProduceWikiTransfer, ProduceWikiTransferV1
from trans2es.utils.tag_transfer import TagTransfer from trans2es.utils.tag_transfer import TagTransfer, TagTransferV1
from trans2es.utils.wordresemble import WordResemble from trans2es.utils.wordresemble import WordResemble, WordResembleV1
from libs.es import ESPerform from libs.es import ESPerform
from libs.tools import tzlc, getMd5Digest from libs.tools import tzlc, getMd5Digest
from trans2es.commons.words_utils import QueryWordAttr from trans2es.commons.words_utils import QueryWordAttr
...@@ -286,7 +286,8 @@ def get_type_info_map(): ...@@ -286,7 +286,8 @@ def get_type_info_map():
name='suggest', name='suggest',
type='doctor_tips', # doctor type='doctor_tips', # doctor
model=doctor.Doctor, model=doctor.Doctor,
query_deferred=lambda: doctor.Doctor.objects.all().filter(doctor_type__in=[DOCTOR_TYPE.DOCTOR,DOCTOR_TYPE.OFFICER]).query, query_deferred=lambda: doctor.Doctor.objects.all().filter(
doctor_type__in=[DOCTOR_TYPE.DOCTOR, DOCTOR_TYPE.OFFICER]).query,
get_data_func=DoctorTransfer.get_doctor_suggest_data_list, get_data_func=DoctorTransfer.get_doctor_suggest_data_list,
bulk_insert_chunk_size=100, bulk_insert_chunk_size=100,
round_insert_chunk_size=5, round_insert_chunk_size=5,
...@@ -365,89 +366,90 @@ def get_type_info_map(): ...@@ -365,89 +366,90 @@ def get_type_info_map():
round_insert_chunk_size=5, round_insert_chunk_size=5,
round_insert_period=2, round_insert_period=2,
), ),
# TypeInfo( TypeInfo(
# name='suggest-v1', name='suggest-v1',
# type='doctor_tips', # doctor type='doctor_tips-v1', # doctor
# model=doctor.Doctor, model=doctor.Doctor,
# query_deferred=lambda: doctor.Doctor.objects.all().filter(doctor_type=DOCTOR_TYPE.DOCTOR).query, query_deferred=lambda: doctor.Doctor.objects.all().filter(
# get_data_func=DoctorTransfer.get_doctor_suggest_data_list, doctor_type__in=[DOCTOR_TYPE.DOCTOR, DOCTOR_TYPE.OFFICER]).query,
# bulk_insert_chunk_size=100, get_data_func=DoctorTransferV1.get_doctor_suggest_data_list,
# round_insert_chunk_size=5, bulk_insert_chunk_size=100,
# round_insert_period=2, round_insert_chunk_size=5,
# ), round_insert_period=2,
# TypeInfo( ),
# name='suggest-v1', TypeInfo(
# type='hospital_tips', # hospital name='suggest-v1',
# model=doctor.Hospital, type='hospital_tips-v1', # hospital
# # query_deferred=lambda: doctor.Doctor.objects.all().filter(doctor_type=DOCTOR_TYPE.OFFICER).query, model=doctor.Hospital,
# query_deferred=lambda: doctor.Hospital.objects.all().query, # query_deferred=lambda: doctor.Doctor.objects.all().filter(doctor_type=DOCTOR_TYPE.OFFICER).query,
# get_data_func=HospitalTransfer.get_hospital_suggest_data_list, query_deferred=lambda: doctor.Hospital.objects.all().query,
# bulk_insert_chunk_size=100, get_data_func=HospitalTransferV1.get_hospital_suggest_data_list,
# round_insert_chunk_size=5, bulk_insert_chunk_size=100,
# round_insert_period=2, round_insert_chunk_size=5,
# ), round_insert_period=2,
# TypeInfo( ),
# name='suggest-v1', TypeInfo(
# type='itemwiki_tips', # itemwiki name='suggest-v1',
# model=itemwiki.ItemWiki, type='itemwiki_tips-v1', # itemwiki
# query_deferred=lambda: itemwiki.ItemWiki.objects.all().query, model=itemwiki.ItemWiki,
# get_data_func=ItemWikiTransfer.get_itemwiki_suggest_data_list, query_deferred=lambda: itemwiki.ItemWiki.objects.all().query,
# bulk_insert_chunk_size=100, get_data_func=ItemWikiTransferV1.get_itemwiki_suggest_data_list,
# round_insert_chunk_size=5, bulk_insert_chunk_size=100,
# round_insert_period=2, round_insert_chunk_size=5,
# ), round_insert_period=2,
# TypeInfo( ),
# name='suggest-v1', TypeInfo(
# type='collectwiki_tips', # collectwiki name='suggest-v1',
# model=collectwiki.CollectWiki, type='collectwiki_tips-v1', # collectwiki
# query_deferred=lambda: collectwiki.CollectWiki.objects.all().query, model=collectwiki.CollectWiki,
# get_data_func=CollectWikiTransfer.get_collectwiki_suggest_data_list, query_deferred=lambda: collectwiki.CollectWiki.objects.all().query,
# bulk_insert_chunk_size=100, get_data_func=CollectWikiTransferV1.get_collectwiki_suggest_data_list,
# round_insert_chunk_size=5, bulk_insert_chunk_size=100,
# round_insert_period=2, round_insert_chunk_size=5,
# ), round_insert_period=2,
# TypeInfo( ),
# name='suggest-v1', TypeInfo(
# type='brandwiki_tips', # brandwiki name='suggest-v1',
# model=brandwiki.BrandWiki, type='brandwiki_tips-v1', # brandwiki
# query_deferred=lambda: brandwiki.BrandWiki.objects.all().query, model=brandwiki.BrandWiki,
# get_data_func=BrandWikiTransfer.get_brandwiki_suggest_data_list, query_deferred=lambda: brandwiki.BrandWiki.objects.all().query,
# bulk_insert_chunk_size=100, get_data_func=BrandWikiTransferV1.get_brandwiki_suggest_data_list,
# round_insert_chunk_size=5, bulk_insert_chunk_size=100,
# round_insert_period=2, round_insert_chunk_size=5,
# ), round_insert_period=2,
# TypeInfo( ),
# name='suggest-v1', TypeInfo(
# type='productwiki_tips', # productwiki name='suggest-v1',
# model=productwiki.ProductWiki, type='productwiki_tips-v1', # productwiki
# query_deferred=lambda: productwiki.ProductWiki.objects.all().query, model=productwiki.ProductWiki,
# get_data_func=ProduceWikiTransfer.get_productwiki_suggest_data_list, query_deferred=lambda: productwiki.ProductWiki.objects.all().query,
# bulk_insert_chunk_size=100, get_data_func=ProduceWikiTransferV1.get_productwiki_suggest_data_list,
# round_insert_chunk_size=5, bulk_insert_chunk_size=100,
# round_insert_period=2, round_insert_chunk_size=5,
# ), round_insert_period=2,
# TypeInfo( ),
# name='suggest-v1', TypeInfo(
# type='tag_tips', # tag name='suggest-v1',
# model=tag.Tag, type='tag_tips-v1', # tag
# query_deferred=lambda: tag.Tag.objects.all().filter( model=tag.Tag,
# tag_type__in=[TAG_TYPE.BODY_PART, TAG_TYPE.BODY_PART_SUB_ITEM, TAG_TYPE.ITEM_WIKI]).query, query_deferred=lambda: tag.Tag.objects.all().filter(
# get_data_func=TagTransfer.get_tag_suggest_data_list, tag_type__in=[TAG_TYPE.BODY_PART, TAG_TYPE.BODY_PART_SUB_ITEM, TAG_TYPE.ITEM_WIKI]).query,
# bulk_insert_chunk_size=100, get_data_func=TagTransferV1.get_tag_suggest_data_list,
# round_insert_chunk_size=5, bulk_insert_chunk_size=100,
# round_insert_period=2, round_insert_chunk_size=5,
# ), round_insert_period=2,
# TypeInfo( ),
# name='suggest-v1', TypeInfo(
# type='wordrel_tips', # tag name='suggest-v1',
# model=wordresemble.WordRel, type='wordrel_tips-v1', # tag
# query_deferred=lambda: wordresemble.WordRel.objects.filter( model=wordresemble.WordRel,
# category__in=[13, 12, 11, 9, 1]).query, query_deferred=lambda: wordresemble.WordRel.objects.filter(
# get_data_func=WordResemble.get_resemble_list, category__in=[13, 12, 11, 9, 1]).query,
# bulk_insert_chunk_size=100, get_data_func=WordResembleV1.get_resemble_list,
# round_insert_chunk_size=5, bulk_insert_chunk_size=100,
# round_insert_period=2, round_insert_chunk_size=5,
# ) round_insert_period=2,
)
] ]
type_info_map = { type_info_map = {
...@@ -550,7 +552,7 @@ def tips_attr_sync_to_redis_type_info_map(): ...@@ -550,7 +552,7 @@ def tips_attr_sync_to_redis_type_info_map():
TypeInfo( TypeInfo(
name='suggest', name='suggest',
type='wordresemble_results_num', # api_wordrelresemble type='wordresemble_results_num', # api_wordrelresemble
model= wordresemble.WordRel, model=wordresemble.WordRel,
query_deferred=lambda: wordresemble.WordRel.objects.filter( query_deferred=lambda: wordresemble.WordRel.objects.filter(
category__in=[13, 12, 11, 9, 1]).query, category__in=[13, 12, 11, 9, 1]).query,
get_data_func=WordResemble.set_data_to_redis, get_data_func=WordResemble.set_data_to_redis,
......
...@@ -4,13 +4,13 @@ import os ...@@ -4,13 +4,13 @@ import os
import sys import sys
import logging import logging
import traceback import traceback
from libs.tools import tzlc,getMd5Digest from libs.tools import tzlc, getMd5Digest
from libs.es import ESPerform from libs.es import ESPerform
from libs.cache import redis_client from libs.cache import redis_client
import json import json
from trans2es.commons.commons import get_tips_suggest_list from trans2es.commons.commons import get_tips_suggest_list, get_tips_suggest_list_v1
from trans2es.commons.words_utils import QueryWordAttr,get_tips_word_type from trans2es.commons.words_utils import QueryWordAttr, get_tips_word_type
class BrandWikiTransfer(object): class BrandWikiTransfer(object):
...@@ -35,7 +35,35 @@ class BrandWikiTransfer(object): ...@@ -35,7 +35,35 @@ class BrandWikiTransfer(object):
suggest_list = get_tips_suggest_list(str(instance.name).lower()) suggest_list = get_tips_suggest_list(str(instance.name).lower())
return (item_dict,suggest_list) return (item_dict, suggest_list)
except: except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc()) logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return ([],[]) return ([], [])
\ No newline at end of file
class BrandWikiTransferV1(object):
@classmethod
def get_brandwiki_suggest_data_list(cls, instance):
try:
ret_list = list()
item_dict = dict()
item_dict["id"] = getMd5Digest(str(instance.name))
item_dict["ori_name"] = instance.name
item_dict["is_online"] = instance.is_online
item_dict["order_weight"] = QueryWordAttr.get_project_query_word_weight(instance.name)
item_dict["results_num"] = QueryWordAttr.get_query_results_num(instance.name)
item_dict["type_flag"] = get_tips_word_type(instance.name)
item_dict["offline_score"] = 0.0
item_dict["tips_name_type"] = 3
ret_list.append(item_dict)
suggest_list = get_tips_suggest_list_v1(str(instance.name).lower())
return (item_dict, suggest_list)
except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return ([], [])
...@@ -4,13 +4,13 @@ import os ...@@ -4,13 +4,13 @@ import os
import sys import sys
import logging import logging
import traceback import traceback
from libs.tools import tzlc,getMd5Digest from libs.tools import tzlc, getMd5Digest
from libs.es import ESPerform from libs.es import ESPerform
from libs.cache import redis_client from libs.cache import redis_client
import json import json
from trans2es.commons.commons import get_tips_suggest_list from trans2es.commons.commons import get_tips_suggest_list, get_tips_suggest_list_v1
from trans2es.commons.words_utils import QueryWordAttr,get_tips_word_type from trans2es.commons.words_utils import QueryWordAttr, get_tips_word_type
class CollectWikiTransfer(object): class CollectWikiTransfer(object):
...@@ -35,7 +35,35 @@ class CollectWikiTransfer(object): ...@@ -35,7 +35,35 @@ class CollectWikiTransfer(object):
suggest_list = get_tips_suggest_list(str(instance.name).lower()) suggest_list = get_tips_suggest_list(str(instance.name).lower())
return (item_dict,suggest_list) return (item_dict, suggest_list)
except: except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc()) logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return ([],[]) return ([], [])
\ No newline at end of file
class CollectWikiTransferV1(object):
@classmethod
def get_collectwiki_suggest_data_list(cls, instance):
try:
ret_list = list()
item_dict = dict()
item_dict["id"] = getMd5Digest(str(instance.name))
item_dict["ori_name"] = instance.name
item_dict["is_online"] = instance.is_online
item_dict["order_weight"] = QueryWordAttr.get_project_query_word_weight(instance.name)
item_dict["results_num"] = QueryWordAttr.get_query_results_num(instance.name)
item_dict["type_flag"] = get_tips_word_type(instance.name)
item_dict["offline_score"] = 0.0
item_dict["tips_name_type"] = 3
ret_list.append(item_dict)
suggest_list = get_tips_suggest_list_v1(str(instance.name).lower())
return (item_dict, suggest_list)
except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return ([], [])
...@@ -10,7 +10,7 @@ from libs.cache import redis_client ...@@ -10,7 +10,7 @@ from libs.cache import redis_client
import json import json
from django.conf import settings from django.conf import settings
from trans2es.commons.commons import get_tips_suggest_list from trans2es.commons.commons import get_tips_suggest_list, get_tips_suggest_list_v1
from trans2es.commons.words_utils import QueryWordAttr, get_tips_word_type from trans2es.commons.words_utils import QueryWordAttr, get_tips_word_type
...@@ -109,3 +109,100 @@ class DoctorTransfer(object): ...@@ -109,3 +109,100 @@ class DoctorTransfer(object):
except: except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc()) logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return (dict(), list()) return (dict(), list())
class DoctorTransferV1(object):
@classmethod
def get_doctor_data_name_mapping_results_to_redis(cls, instance):
try:
doctor_name = instance.name.strip()
if doctor_name:
multi_fields = {
'hospital.city_province_name': 1,
'hospital.name': 1,
'name': 1,
'hospital.city_name': 1,
'hospital.officer_name': 1,
'service_closure_tags': 1
}
query_fields = ['^'.join((k, str(v))) for (k, v) in multi_fields.items()]
multi_match = {
'query': doctor_name,
'type': 'cross_fields',
'operator': 'and',
'fields': query_fields,
}
q = {
"size": 0,
"query": {
"bool": {
"should": [
{"multi_match": multi_match}
],
"must": [
{"term": {"is_online": True}}
],
"minimum_should_match": 1
}
}
}
result_dict = ESPerform.get_search_results(ESPerform.get_cli(settings.GM_ORI_ES_INFO_LIST),
sub_index_name="doctor", doc_type="doctor", query_body=q,
offset=0, size=0)
doctor_results = result_dict["total_count"]
redis_key_prefix = "search_tips:tips_mapping_num"
redis_data = redis_client.hget(redis_key_prefix, doctor_name)
redis_val_dict = json.loads(str(redis_data, encoding="utf-8")) if redis_data else {}
total_count = doctor_results
if 't' in redis_val_dict:
total_count += int(redis_val_dict['t'])
redis_val_dict['t'] = total_count
redis_val_dict['d'] = doctor_results
logging.info(
"duan add,redis_key_prefix:%s,redis_val_dict:%s" % (str(redis_key_prefix), str(redis_val_dict)))
redis_client.hset(redis_key_prefix, doctor_name, json.dumps(redis_val_dict))
except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc())
@classmethod
def get_doctor_suggest_data_list(cls, instance):
try:
ret_list = list()
keyword = instance.name
cut_bool = False
cut_word = ["下线", "停用", "已经下线", "账号停用"]
for i in cut_word:
if keyword.find(i) >= 0:
cut_bool = True
item_dict = dict()
suggest_list = list()
if cut_bool == False:
item_dict["id"] = getMd5Digest(str(instance.name))
item_dict["ori_name"] = instance.name
item_dict["is_online"] = instance.is_online
item_dict["order_weight"] = QueryWordAttr.get_doctor_query_word_weight(instance.name)
item_dict["results_num"] = QueryWordAttr.get_query_results_num(instance.name)
item_dict["type_flag"] = get_tips_word_type(instance.name)
item_dict["offline_score"] = 0.0
item_dict["tips_name_type"] = 2
ret_list.append(item_dict)
suggest_list = get_tips_suggest_list_v1(instance.name)
return (item_dict, suggest_list)
except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return (dict(), list())
...@@ -9,7 +9,7 @@ from libs.es import ESPerform ...@@ -9,7 +9,7 @@ from libs.es import ESPerform
from libs.cache import redis_client from libs.cache import redis_client
import json import json
from trans2es.commons.commons import get_tips_suggest_list from trans2es.commons.commons import get_tips_suggest_list, get_tips_suggest_list_v1
from trans2es.commons.words_utils import QueryWordAttr, get_tips_word_type from trans2es.commons.words_utils import QueryWordAttr, get_tips_word_type
...@@ -62,3 +62,54 @@ class HospitalTransfer(object): ...@@ -62,3 +62,54 @@ class HospitalTransfer(object):
redis_client.hset(redis_key_prefix, name, pos_list) redis_client.hset(redis_key_prefix, name, pos_list)
except: except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc()) logging.error("catch exception,err_msg:%s" % traceback.format_exc())
class HospitalTransferV1(object):
@classmethod
def get_hospital_suggest_data_list(cls, instance):
try:
ret_list = list()
name = instance.name.strip()
cut_bool = False
cut_word = ["下线", "停用", "已经下线", "账号停用"]
for i in cut_word:
if name.find(i) >= 0:
cut_bool = True
item_dict = dict()
suggest_list = list()
if cut_bool == False:
item_dict = dict()
item_dict["id"] = getMd5Digest(str(name))
item_dict["ori_name"] = name
item_dict["is_online"] = instance.is_online
item_dict["order_weight"] = QueryWordAttr.get_hospital_query_word_weight(name)
item_dict["results_num"] = QueryWordAttr.get_query_results_num(name)
item_dict["type_flag"] = get_tips_word_type(name)
item_dict["offline_score"] = 0.0
item_dict["tips_name_type"] = 1
ret_list.append(item_dict)
suggest_list = get_tips_suggest_list_v1(str(name).lower())
return (item_dict, suggest_list)
except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return (dict(), list())
@classmethod
def get_hospital_lat_lng_info_to_redis(cls, instance):
try:
redis_key_prefix = "search_tips:hospital_latlng_info"
name = instance.name.strip()
lng = instance.baidu_loc_lng
lat = instance.baidu_loc_lat
pos_list = [lng, lat]
redis_client.hset(redis_key_prefix, name, pos_list)
except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc())
...@@ -10,7 +10,7 @@ from libs.cache import redis_client ...@@ -10,7 +10,7 @@ from libs.cache import redis_client
import json import json
from django.conf import settings from django.conf import settings
from trans2es.commons.commons import get_tips_suggest_list from trans2es.commons.commons import get_tips_suggest_list,get_tips_suggest_list_v1
from trans2es.commons.words_utils import QueryWordAttr, get_tips_word_type from trans2es.commons.words_utils import QueryWordAttr, get_tips_word_type
...@@ -104,3 +104,94 @@ class ItemWikiTransfer(object): ...@@ -104,3 +104,94 @@ class ItemWikiTransfer(object):
except: except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc()) logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return ([], []) return ([], [])
class ItemWikiTransferV1(object):
@classmethod
def get_wiki_data_name_mapping_results_to_redis(cls, instance):
try:
wiki_name = instance.name.strip()
if wiki_name:
multi_fields = {
'name': 1,
'association_tags': 1,
"description": 1,
"use_result": 1,
"treatment_method": 1,
"effect": 1,
"ingredient": 1,
"nature_type": 1,
"shape": 1,
}
query_fields = ['^'.join((k, str(v))) for (k, v) in multi_fields.items()]
multi_match = {
'query': wiki_name,
'type': 'cross_fields',
'operator': 'or',
'fields': query_fields,
}
q = {
"size": 0,
"query": {
"bool": {
"should": [
{"multi_match": multi_match}
],
"must": [
{"term": {"is_online": True}}
],
"minimum_should_match": 1
}
}
}
result_dict = ESPerform.get_search_results(ESPerform.get_cli(settings.GM_ORI_ES_INFO_LIST),
sub_index_name="newwiki", doc_type="newwiki", query_body=q,
offset=0, size=0)
doctor_results = result_dict["total_count"]
redis_key_prefix = "search_tips:tips_mapping_num"
redis_data = redis_client.hget(redis_key_prefix, wiki_name)
redis_val_dict = json.loads(str(redis_data, encoding="utf-8")) if redis_data else {}
total_count = doctor_results
if 't' in redis_val_dict:
total_count += int(redis_val_dict['t'])
redis_val_dict['t'] = total_count
redis_val_dict['w'] = doctor_results
redis_client.hset(redis_key_prefix, wiki_name, json.dumps(redis_val_dict))
except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc())
@classmethod
def get_itemwiki_suggest_data_list(cls, instance):
try:
ret_list = list()
item_dict = dict()
item_dict["id"] = getMd5Digest(str(instance.name))
item_dict["ori_name"] = instance.name
item_dict["is_online"] = instance.is_online
item_dict["order_weight"] = QueryWordAttr.get_project_query_word_weight(instance.name)
item_dict["results_num"] = QueryWordAttr.get_query_results_num(instance.name)
item_dict["type_flag"] = get_tips_word_type(instance.name)
item_dict["offline_score"] = 0.0
item_dict["tips_name_type"] = 3
ret_list.append(item_dict)
suggest_list = get_tips_suggest_list_v1(str(instance.name).lower())
return (item_dict, suggest_list)
except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return ([], [])
...@@ -9,7 +9,7 @@ from libs.es import ESPerform ...@@ -9,7 +9,7 @@ from libs.es import ESPerform
from libs.cache import redis_client from libs.cache import redis_client
import json import json
from trans2es.commons.commons import get_tips_suggest_list from trans2es.commons.commons import get_tips_suggest_list, get_tips_suggest_list_v1
from trans2es.commons.words_utils import QueryWordAttr, get_tips_word_type from trans2es.commons.words_utils import QueryWordAttr, get_tips_word_type
...@@ -39,3 +39,31 @@ class ProduceWikiTransfer(object): ...@@ -39,3 +39,31 @@ class ProduceWikiTransfer(object):
except: except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc()) logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return ([], []) return ([], [])
class ProduceWikiTransferV1(object):
@classmethod
def get_productwiki_suggest_data_list(cls, instance):
try:
ret_list = list()
item_dict = dict()
item_dict["id"] = getMd5Digest(str(instance.name))
item_dict["ori_name"] = instance.name
item_dict["is_online"] = instance.is_online
item_dict["order_weight"] = QueryWordAttr.get_project_query_word_weight(instance.name)
item_dict["results_num"] = QueryWordAttr.get_query_results_num(instance.name)
item_dict["type_flag"] = get_tips_word_type(instance.name)
item_dict["offline_score"] = 0.0
item_dict["tips_name_type"] = 3
ret_list.append(item_dict)
suggest_list = get_tips_suggest_list_v1(str(instance.name).lower())
return (item_dict, suggest_list)
except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return ([], [])
...@@ -10,7 +10,7 @@ from libs.cache import redis_client ...@@ -10,7 +10,7 @@ from libs.cache import redis_client
import json import json
from django.conf import settings from django.conf import settings
from trans2es.commons.commons import get_tips_suggest_list from trans2es.commons.commons import get_tips_suggest_list, get_tips_suggest_list_v1
from trans2es.commons.words_utils import QueryWordAttr, get_tips_word_type from trans2es.commons.words_utils import QueryWordAttr, get_tips_word_type
...@@ -97,8 +97,8 @@ class TagTransfer(object): ...@@ -97,8 +97,8 @@ class TagTransfer(object):
for i in cut_word: for i in cut_word:
if keyword.find(i) >= 0: if keyword.find(i) >= 0:
cut_bool = True cut_bool = True
item_dict = dict()
if cut_bool == False: if cut_bool == False:
item_dict = dict()
item_dict["id"] = getMd5Digest(str(instance.name)) item_dict["id"] = getMd5Digest(str(instance.name))
item_dict["ori_name"] = instance.name item_dict["ori_name"] = instance.name
item_dict["is_online"] = instance.is_online item_dict["is_online"] = instance.is_online
...@@ -117,3 +117,108 @@ class TagTransfer(object): ...@@ -117,3 +117,108 @@ class TagTransfer(object):
except: except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc()) logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return ([], []) return ([], [])
class TagTransferV1(object):
tips_num_redis_key_prefix = "search_tips:tips_mapping_num"
@classmethod
def get_tag_data_name_mapping_results_to_redis(cls, instance):
try:
tag_name = instance.name.strip()
q = dict()
if tag_name:
multi_fields = {
'short_description': 1,
'doctor.name': 1,
'doctor.hospital.name': 1,
'doctor.hospital.city_name': 1,
'doctor.hospital.city_province_name': 1,
'closure_tags': 1, # 5.9版 搜索所有tag
'doctor.hospital.officer_name': 1 # 搜索机构管理者
}
query_fields = ['^'.join((k, str(v))) for (k, v) in multi_fields.items()]
multi_match = {
'query': tag_name,
'type': 'cross_fields',
'operator': 'and',
'fields': query_fields,
}
sku_query = {
"nested": {
"path": "sku_list",
"query": {
"multi_match": {
"query": tag_name,
"fields": ["sku_list.name^2"],
'operator': 'and',
'type': 'cross_fields'
}
}
}
}
q['query'] = {
'bool': {
"should": [
{'multi_match': multi_match},
sku_query
],
"must": [
{"term": {"is_online": True}}
],
"minimum_should_match": 1
}
}
result_dict = ESPerform.get_search_results(ESPerform.get_cli(settings.GM_ORI_ES_INFO_LIST),
sub_index_name="service", doc_type="service", query_body=q,
offset=0, size=0)
doctor_results = result_dict["total_count"]
redis_data = redis_client.hget(cls.tips_num_redis_key_prefix, tag_name)
redis_val_dict = json.loads(str(redis_data, encoding="utf-8")) if redis_data else {}
total_count = doctor_results
if 't' in redis_val_dict:
total_count += int(redis_val_dict['t'])
redis_val_dict['t'] = total_count
redis_val_dict['s'] = doctor_results
redis_client.hset(cls.tips_num_redis_key_prefix, tag_name, json.dumps(redis_val_dict))
except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc())
@classmethod
def get_tag_suggest_data_list(cls, instance):
try:
ret_list = list()
keyword = instance.name
cut_bool = False
cut_word = ["下线", "停用", "已经下线", "账号停用"]
for i in cut_word:
if keyword.find(i) >= 0:
cut_bool = True
item_dict = dict()
if cut_bool == False:
item_dict["id"] = getMd5Digest(str(instance.name))
item_dict["ori_name"] = instance.name
item_dict["is_online"] = instance.is_online
item_dict["order_weight"] = QueryWordAttr.get_project_query_word_weight(instance.name)
item_dict["results_num"] = QueryWordAttr.get_query_results_num(instance.name)
item_dict["type_flag"] = get_tips_word_type(instance.name)
item_dict["offline_score"] = 0.0
item_dict["tips_name_type"] = 0
ret_list.append(item_dict)
suggest_list = get_tips_suggest_list_v1(str(instance.name).lower())
return (item_dict, suggest_list)
except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return ([], [])
...@@ -12,7 +12,7 @@ import json ...@@ -12,7 +12,7 @@ import json
from django.conf import settings from django.conf import settings
from trans2es.models import wordresemble from trans2es.models import wordresemble
from trans2es.commons.words_utils import QueryWordAttr, get_tips_word_type from trans2es.commons.words_utils import QueryWordAttr, get_tips_word_type
from trans2es.commons.commons import get_tips_suggest_list from trans2es.commons.commons import get_tips_suggest_list, get_tips_suggest_list_v1
from trans2es.utils.doctor_transfer import DoctorTransfer from trans2es.utils.doctor_transfer import DoctorTransfer
from trans2es.utils.itemwiki_transfer import ItemWikiTransfer from trans2es.utils.itemwiki_transfer import ItemWikiTransfer
from trans2es.utils.tag_transfer import TagTransfer from trans2es.utils.tag_transfer import TagTransfer
...@@ -201,3 +201,188 @@ class WordResemble(object): ...@@ -201,3 +201,188 @@ class WordResemble(object):
redis_client.hset(QUERY_KEY, query_base64, json.dumps(keyword_value)) 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())
class WordResembleV1(object):
@classmethod
def get_word_resemble_list(cls, keyword):
try:
query_sql_item = wordresemble.WordRel.objects.filter(keyword=keyword)
temp_list = list()
for sql_obj in query_sql_item:
temp_list.extend(list(sql_obj.all_resembles.all().values_list('word', flat=True)))
resemble_list = list()
for item in temp_list:
resemble_list.extend(item.split("、"))
return resemble_list
except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return list()
@classmethod
def get_resemble_list(cls, instance):
try:
try:
ret_list = list()
item_dict = dict()
keyword = instance.keyword
cut_bool = False
cut_word = ["下线", "停用", "已经下线", "账号停用"]
for i in cut_word:
if keyword.find(i) >= 0:
cut_bool = True
if cut_bool == False:
item_dict["id"] = getMd5Digest(str(instance.keyword))
item_dict["ori_name"] = instance.keyword
item_dict["is_online"] = True
item_dict["order_weight"] = QueryWordAttr.get_project_query_word_weight(instance.keyword)
item_dict["results_num"] = QueryWordAttr.get_query_results_num(instance.keyword)
item_dict["type_flag"] = get_tips_word_type(instance.keyword)
item_dict["offline_score"] = 0.0
item_dict["tips_name_type"] = 4
ret_list.append(item_dict)
suggest_list = get_tips_suggest_list_v1(str(instance.keyword).lower())
return (item_dict, suggest_list)
except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return ([], [])
except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return list()
@classmethod
def get_all_data_name_mapping_results_to_redis(cls, instance):
try:
total_count = 0
instance.name = instance.keyword
# 获取百科的
ItemWikiTransfer.get_wiki_data_name_mapping_results_to_redis(instance)
# 获取美购的
TagTransfer.get_tag_data_name_mapping_results_to_redis(instance)
# 获取医生医院的
DoctorTransfer.get_doctor_data_name_mapping_results_to_redis(instance)
# 获取日记的
WordResemble.get_diary_data_name_mapping_results_to_redis(instance)
except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc())
@classmethod
def get_diary_data_name_mapping_results_to_redis(cls, instance):
try:
tips_num_redis_key_prefix = "search_tips:tips_mapping_num"
tag_name = instance.name.strip()
q = dict()
if tag_name:
multi_fields = {
'tags': 8,
'doctor.name': 4,
'doctor.hospital.name': 3,
'doctor.hospital.officer_name': 3,
'user.last_name': 2,
'service.name': 1,
"title": 2}
query_fields = ['^'.join((k, str(v))) for (k, v) in multi_fields.items()]
multi_match = {
'query': tag_name,
'type': 'cross_fields',
'operator': 'and',
'fields': query_fields,
}
q['query'] = {
'bool': {
"should": [
{'multi_match': multi_match}
],
"must": [
{"term": {"is_online": True}}
],
"minimum_should_match": 1
}
}
result_dict = ESPerform.get_search_results(ESPerform.get_cli(settings.GM_ORI_ES_INFO_LIST),
sub_index_name="diary", doc_type="diary", query_body=q,
offset=0, size=0)
doctor_results = result_dict["total_count"]
redis_data = redis_client.hget(tips_num_redis_key_prefix, tag_name)
redis_val_dict = json.loads(str(redis_data, encoding="utf-8")) if redis_data else {}
total_count = doctor_results
if 't' in redis_val_dict:
total_count += int(redis_val_dict['t'])
redis_val_dict['t'] = total_count
redis_val_dict['r'] = doctor_results
redis_client.hset(tips_num_redis_key_prefix, tag_name, json.dumps(redis_val_dict))
except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc())
@classmethod
def set_data_to_redis(cls, instance):
try:
keyword_value = []
QUERY_KEY = "query:search_tip"
tips_num_redis_key_prefix = "search_tips:tips_mapping_num"
query_sql_item = wordresemble.WordRel.objects.filter(keyword=instance.keyword)
for sql_obj in query_sql_item:
count = 0
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:
count += 1
wordresemble_value = []
# 先存储本体词
item_name = base64.b64encode(items.encode('utf8')).decode('utf8')
# 获取该同义词的结果
redis_data = redis_client.hget(tips_num_redis_key_prefix, items)
logging.info("get redis_data:%s" % redis_data)
redis_val_dict = json.loads(str(redis_data, encoding="utf-8")) if redis_data else {}
total_count = 0
if 't' in redis_val_dict:
total_count = int(redis_val_dict['t'])
value = {item_name: total_count}
keyword_value.append(value)
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 {}
total_count = 0
if 't' in redis_val_dict:
total_count = int(redis_val_dict['t'])
value = {query_base64: total_count}
wordresemble_value.append(value)
if count == 1:
keyword_value.append(value)
if words:
for w in words:
value_name_w = base64.b64encode(w.encode('utf8')).decode('utf8')
redis_data = redis_client.hget(tips_num_redis_key_prefix, w)
redis_val_dict = json.loads(str(redis_data, encoding="utf-8")) if redis_data else {}
total_count = 0
if 't' in redis_val_dict:
total_count = int(redis_val_dict['t'])
value = {value_name_w: total_count}
wordresemble_value.append(value)
if len(wordresemble_value) > 0:
logging.info("get type wordresemble_value:%s" % type(json.dumps(wordresemble_value)))
redis_client.hset(QUERY_KEY, item_name, json.dumps(wordresemble_value))
if len(keyword_value) > 0:
logging.info("get type keyword_value:%s" % type(json.dumps(keyword_value)))
redis_client.hset(QUERY_KEY, query_base64, json.dumps(keyword_value))
except:
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