Commit ca4163aa authored by 段英荣's avatar 段英荣

modify

parent 28a384c1
...@@ -26,13 +26,10 @@ def auto_complete(request): ...@@ -26,13 +26,10 @@ def auto_complete(request):
""" """
q = request.GET.get('q', '').strip() q = request.GET.get('q', '').strip()
logging.info("duan add,q is:%s" % str(q).encode("utf-8"))
if not q: if not q:
return json_http_response({'error': 0, 'data': []}) return json_http_response({'error': 0, 'data': []})
logging.info("duan add,q is:%s" % str(q).encode("utf-8"))
data = get_suggest_tips(q) data = get_suggest_tips(q)
result = { result = {
......
...@@ -12,7 +12,7 @@ import sys ...@@ -12,7 +12,7 @@ import sys
from libs.es import ESPerform from libs.es import ESPerform
import trans2es.models as md import trans2es.models as md
from libs.table_scan import TableSlicer,TableSlicerChunk from libs.table_scan import TableSlicer,TableSlicerChunk
from trans2es.type_info import get_type_info_map,TypeInfo from trans2es.type_info import get_type_info_map,TypeInfo,tips_attr_sync_to_redis_type_info_map
from libs.cache import redis_client from libs.cache import redis_client
import json import json
...@@ -39,6 +39,18 @@ class Command(BaseCommand): ...@@ -39,6 +39,18 @@ class Command(BaseCommand):
def handle(self, *args, **options): def handle(self, *args, **options):
try: try:
logging.info("test") type_name_list = get_type_info_map().keys()
for type_name in type_name_list:
if len(options["type"]):
if options["type"] == "all" or type_name==options["type"]:
logging.info("begin sync [%s] data to es!" % type_name)
type_info = tips_attr_sync_to_redis_type_info_map()[type_name]
query_set = type_info.queryset
slicer = TableSlicer(queryset=query_set, chunk_size=type_info.bulk_insert_chunk_size)
for chunk in slicer.chunks():
for instance in list(chunk):
type_info.get_data_func(instance)
except: except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc()) logging.error("catch exception,err_msg:%s" % traceback.format_exc())
...@@ -256,3 +256,29 @@ def get_type_info_map(): ...@@ -256,3 +256,29 @@ def get_type_info_map():
_get_type_info_map_result = type_info_map _get_type_info_map_result = type_info_map
return type_info_map return type_info_map
def tips_attr_sync_to_redis_type_info_map():
global _get_type_info_map_result
if _get_type_info_map_result:
return _get_type_info_map_result
type_info_list = [
TypeInfo(
name='suggest',
type='doctor_results_num',# doctor结果数
model=doctor.Doctor,
query_deferred=lambda: doctor.Doctor.objects.all().query,
get_data_func=DoctorTransfer.get_doctor_data_name_mapping_results_to_redis,
bulk_insert_chunk_size=100,
round_insert_chunk_size=5,
round_insert_period=2,
)
]
type_info_map = {
type_info.type: type_info
for type_info in type_info_list
}
_get_type_info_map_result = type_info_map
return type_info_map
\ No newline at end of file
...@@ -5,6 +5,8 @@ import sys ...@@ -5,6 +5,8 @@ import sys
import logging import logging
import traceback import traceback
from libs.tools import tzlc from libs.tools import tzlc
from libs.es import ESPerform
from libs.cache import redis_client
import pypinyin import pypinyin
from pypinyin import lazy_pinyin from pypinyin import lazy_pinyin
...@@ -43,13 +45,41 @@ class DoctorTransfer(object): ...@@ -43,13 +45,41 @@ class DoctorTransfer(object):
'fields': query_fields, 'fields': query_fields,
} }
q = {
"size": 0,
"query":{
"bool":{
"should":[
{"multi_match": multi_match}
],
"must":[
{"term": {"is_online": True}},
{"term": {"is_deleted": False}}
]
}
}
}
result_dict = ESPerform.get_search_results(ESPerform.get_cli(), sub_index_name="suggest", query_body=q)
doctor_results = result_dict["total_count"]
redis_key_prefix = "search_tips:tips_mapping_num"
redis_val_dict = redis_client.hgetall(redis_key_prefix)
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
redis_client.hmset(redis_key_prefix, redis_val_dict)
except: except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc()) logging.error("catch exception,err_msg:%s" % traceback.format_exc())
@classmethod @classmethod
def get_doctor_suggest_data_list(cls,instance): def get_doctor_suggest_data_list(cls, instance):
try: try:
ret_list = list() ret_list = list()
......
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