Commit 5b5e6032 authored by lixiaofang's avatar lixiaofang

add

parents 7c9943d3 5e5123c2
...@@ -24,5 +24,29 @@ ...@@ -24,5 +24,29 @@
<option name="ITERATION_ELEMENTS_WRAPPING" value="chop_down_if_not_single" /> <option name="ITERATION_ELEMENTS_WRAPPING" value="chop_down_if_not_single" />
</formatting-settings> </formatting-settings>
</DBN-SQL> </DBN-SQL>
<DBN-PSQL>
<case-options enabled="false">
<option name="KEYWORD_CASE" value="lower" />
<option name="FUNCTION_CASE" value="lower" />
<option name="PARAMETER_CASE" value="lower" />
<option name="DATATYPE_CASE" value="lower" />
<option name="OBJECT_CASE" value="preserve" />
</case-options>
<formatting-settings enabled="false" />
</DBN-PSQL>
<DBN-SQL>
<case-options enabled="false">
<option name="KEYWORD_CASE" value="lower" />
<option name="FUNCTION_CASE" value="lower" />
<option name="PARAMETER_CASE" value="lower" />
<option name="DATATYPE_CASE" value="lower" />
<option name="OBJECT_CASE" value="preserve" />
</case-options>
<formatting-settings enabled="false">
<option name="STATEMENT_SPACING" value="one_line" />
<option name="CLAUSE_CHOP_DOWN" value="chop_down_if_statement_long" />
<option name="ITERATION_ELEMENTS_WRAPPING" value="chop_down_if_not_single" />
</formatting-settings>
</DBN-SQL>
</code_scheme> </code_scheme>
</component> </component>
\ No newline at end of file
...@@ -17,5 +17,6 @@ ...@@ -17,5 +17,6 @@
<element value="search.views.contrast_similar"/> <element value="search.views.contrast_similar"/>
<element value="injection.data_sync.tasks"/> <element value="injection.data_sync.tasks"/>
<element value="search.views.contrast_similar"/> <element value="search.views.contrast_similar"/>
<element value="search.views.search_hotword"/>
</config> </config>
</gm_rpcd_config> </gm_rpcd_config>
...@@ -155,9 +155,9 @@ class ESPerform(object): ...@@ -155,9 +155,9 @@ class ESPerform(object):
bulk_actions = [] bulk_actions = []
if sub_index_name=="topic" or \ if sub_index_name == "topic" or \
sub_index_name=="topic-star-routing" or \ sub_index_name == "topic-star-routing" or \
sub_index_name=="topic-high-star": sub_index_name == "topic-high-star":
for data in data_list: for data in data_list:
if data: if data:
bulk_actions.append({ bulk_actions.append({
...@@ -232,7 +232,7 @@ class ESPerform(object): ...@@ -232,7 +232,7 @@ class ESPerform(object):
return {"total_count": 0, "hits": []} return {"total_count": 0, "hits": []}
@classmethod @classmethod
def get_analyze_results(cls,es_cli, sub_index_name, query_body): def get_analyze_results(cls, es_cli, sub_index_name, query_body):
try: try:
assert (es_cli is not None) assert (es_cli is not None)
...@@ -242,7 +242,7 @@ class ESPerform(object): ...@@ -242,7 +242,7 @@ class ESPerform(object):
logging.error("index:%s is not existing,get_search_results error!" % official_index_name) logging.error("index:%s is not existing,get_search_results error!" % official_index_name)
return None return None
res = es_cli.indices.analyze(index=official_index_name,body=query_body) res = es_cli.indices.analyze(index=official_index_name, body=query_body)
return res return res
except: except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc()) logging.error("catch exception,err_msg:%s" % traceback.format_exc())
...@@ -298,8 +298,8 @@ class ESPerform(object): ...@@ -298,8 +298,8 @@ class ESPerform(object):
functions_list += [ functions_list += [
{ {
"filter": { "filter": {
"constant_score":{ "constant_score": {
"filter":{ "filter": {
"term": {"content_level": 6}} "term": {"content_level": 6}}
} }
}, },
...@@ -307,8 +307,8 @@ class ESPerform(object): ...@@ -307,8 +307,8 @@ class ESPerform(object):
}, },
{ {
"filter": { "filter": {
"constant_score":{ "constant_score": {
"filter":{ "filter": {
"term": {"content_level": 5}} "term": {"content_level": 5}}
} }
}, },
...@@ -316,8 +316,8 @@ class ESPerform(object): ...@@ -316,8 +316,8 @@ class ESPerform(object):
}, },
{ {
"filter": { "filter": {
"constant_score":{ "constant_score": {
"filter":{ "filter": {
"term": {"content_level": 4}} "term": {"content_level": 4}}
} }
}, },
...@@ -411,7 +411,7 @@ class ESPerform(object): ...@@ -411,7 +411,7 @@ class ESPerform(object):
} }
}, },
"_source": { "_source": {
"include": ["id","user_id"] "include": ["id", "user_id"]
}, },
"sort": [ "sort": [
{"_score": {"order": "desc"}}, {"_score": {"order": "desc"}},
...@@ -429,7 +429,8 @@ class ESPerform(object): ...@@ -429,7 +429,8 @@ class ESPerform(object):
"id": have_read_topic_id_list "id": have_read_topic_id_list
} }
} }
result_dict = ESPerform.get_search_results(ESPerform.get_cli(), sub_index_name="topic-high-star", query_body=q, result_dict = ESPerform.get_search_results(ESPerform.get_cli(), sub_index_name="topic-high-star",
query_body=q,
offset=0, size=size, routing="6") offset=0, size=size, routing="6")
topic_id_list = [item["_source"]["id"] for item in result_dict["hits"]] topic_id_list = [item["_source"]["id"] for item in result_dict["hits"]]
...@@ -441,7 +442,7 @@ class ESPerform(object): ...@@ -441,7 +442,7 @@ class ESPerform(object):
logging.info("topic_id_list:%s" % str(topic_id_dict)) logging.info("topic_id_list:%s" % str(topic_id_dict))
return topic_id_list,topic_id_dict return topic_id_list, topic_id_dict
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()
...@@ -109,8 +109,6 @@ class GroupUtils(object): ...@@ -109,8 +109,6 @@ class GroupUtils(object):
"script": { "script": {
"lang": "expression", "lang": "expression",
"source": "_score+doc['offline_score']" "source": "_score+doc['offline_score']"
# "lang":"painless",
# "source":"_score+params._source.offline_score"
}, },
"order": "desc" "order": "desc"
} }
...@@ -214,3 +212,74 @@ class GroupUtils(object): ...@@ -214,3 +212,74 @@ class GroupUtils(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 []
@classmethod
def get_search_pictorial_topic(cls, query, offset, size):
try:
q = dict()
multi_fields = {
'name': 4,
'description': 4,
'tag_name': 4
}
query_fields = ['^'.join((k, str(v))) for (k, v) in multi_fields.items()]
multi_match = {
'query': query,
'type': 'cross_fields',
'operator': 'and',
'fields': query_fields,
}
q['query'] = {
'bool': {
"must": [
{"term": {"is_online": True}},
{"term": {"is_deleted": False}},
{"term": {"is_default": 0}},
{"range": {"topic_id_list": {"gte": 0}}},
{"term": {"is_cover": True}}
],
"should": [
{'multi_match': multi_match}
],
"minimum_should_match": 1
}
}
q["_source"] = {
"includes": ["id", "is_online", "is_deleted", "is_default", "topic_id_list", "is_cover", "offline_score",
"is_default"]
}
q["sort"] = [
{
"_script": {
"type": "number",
"script": {
"lang": "expression",
"source": "_score+doc['offline_score']"
},
"order": "desc"
}
},
{
"_score": {
"order": "desc"
}
}
]
q = {}
q["query"] = {
"term": {
"is_online": True
}
}
logging.info("get qqqqqqqq:%s" % q)
es_cli_obj = ESPerform.get_cli()
result_dict = ESPerform.get_search_results(es_cli_obj, "pictorial", q, offset, size)
logging.info("get result_dict:%s" % result_dict)
return result_dict
except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return None
...@@ -261,3 +261,24 @@ def pictorial_topic_sort(pictorial_id=-1, offset=0, size=10): ...@@ -261,3 +261,24 @@ def pictorial_topic_sort(pictorial_id=-1, offset=0, size=10):
except: except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc()) logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return {"pict_pictorial_ids_list": []} return {"pict_pictorial_ids_list": []}
@bind("physical/search/pictorial_topic_sort")
def search_physical(query="", offset=0, size=10):
"""
搜索页面搜索画报
:param query:
:param offset:
:param size:
:return:
1. query识别:允许模糊匹配
2. 召回部分:去掉无封面、无帖子、下线的、默认画报
3. rank部分:(和之前一样,下面只是粘贴过来)
"""
try:
result_dicts = GroupUtils.get_search_pictorial_topic(query, offset, size)
logging.info("result_dicts:%s" % result_dicts)
except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return {"search_topic_ids": []}
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from gm_rpcd.all import bind
import logging
import traceback
import json
from libs.cache import redis_client
from libs.es import ESPerform
from search.utils.group import GroupUtils
from search.utils.common import GroupSortTypes
from libs.es import ESPerform
from trans2es.models.pictorial import PictorialTopics
from trans2es.models.tag import RegisterShowTag, Tag
import random
@bind("physical/search/search_hotword")
def search_hotword(device_id=-1):
"""
:remark:搜索页的热门灵感
内容组成:搜索推荐热词register_show_tag
个性化标签physical:linucb:tag_recommend:device_id:
核心词,需要去重community_tag collection 1
①先从后台随机取最多6个搜索推荐热词,不够6个则有多少取多少
②然后随机取和推荐热词一样数量的linUCB标签,取不够数则取核心标签,linUCB+核心词去重后的数量要和搜索推荐热词的数量一样
③最后打乱顺序填入到热门灵感中,超过3行的标签弃掉,不够3行不用管
:param query:
:param offset:
:param size:
:return:
"""
try:
all_tag_name_list = set()
# 获取搜索推荐热词
results_registr_tag = RegisterShowTag.objects.filter(is_deleted=False, is_online=1).count()
results_tag = Tag.objects.filter(is_online=True, collection=1).count()
tag_id = random.sample(range(1, results_registr_tag), 1)
results_tag_chose = Tag.objects.filter(id__in=tag_id).values_list("name", flat=True)
for i in results_tag_chose:
all_tag_name_list.add(i)
# 获取个性化标签
linucb_recommend_redis_prefix = "physical:linucb:tag_recommend:device_id:"
tag_recommend_redis_key = linucb_recommend_redis_prefix + str(device_id)
linucb_recommend_tag_data = redis_client.get(tag_recommend_redis_key)
linucb_recommend_tag_list = json.loads(linucb_recommend_tag_data) if linucb_recommend_tag_data else []
for item in linucb_recommend_tag_list:
results_tag_recommend = Tag.objects.filter(id=item, is_online=True).values_list("name", flat=True)
all_tag_name_list.add(results_tag_recommend[0])
if len(all_tag_name_list) == 12:
return {"recommend_tag_name": list(all_tag_name_list)}
logging.info("get all_tag_name_list02:%s" % all_tag_name_list)
# 取不够数则取核心标签
if len(all_tag_name_list) < 12:
for i in range(0, 12):
tag_id = random.sample(range(1, results_tag), 12 - len(all_tag_name_list))
results_tag_hexin = Tag.objects.filter(id__in=tag_id).values_list("name", flat=True)
if results_tag_hexin not in all_tag_name_list:
all_tag_name_list.add(results_tag_hexin[0])
if len(all_tag_name_list) >= 12:
return {"recommend_tag_name": list(all_tag_name_list)}
except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return {"recommend_tag_name": []}
...@@ -15,7 +15,6 @@ from libs.es import ESPerform ...@@ -15,7 +15,6 @@ from libs.es import ESPerform
from django.conf import settings from django.conf import settings
def get_discover_page_topic_ids(user_id, device_id, size, query_type=TopicPageType.FIND_PAGE): def get_discover_page_topic_ids(user_id, device_id, size, query_type=TopicPageType.FIND_PAGE):
try: try:
if user_id == -1: if user_id == -1:
...@@ -30,10 +29,11 @@ def get_discover_page_topic_ids(user_id, device_id, size, query_type=TopicPageTy ...@@ -30,10 +29,11 @@ def get_discover_page_topic_ids(user_id, device_id, size, query_type=TopicPageTy
if have_read_topic_id_list == None: if have_read_topic_id_list == None:
have_read_topic_id_list = list() have_read_topic_id_list = list()
recommend_topic_ids = TopicUtils.get_recommend_topic_ids(user_id=user_id, tag_id=0, offset=0, size=size,single_size=size, recommend_topic_ids = TopicUtils.get_recommend_topic_ids(user_id=user_id, tag_id=0, offset=0, size=size,
single_size=size,
query_type=query_type, query_type=query_type,
filter_topic_id_list=have_read_topic_id_list,index_type="topic",routing="4,5,6") filter_topic_id_list=have_read_topic_id_list,
index_type="topic", routing="4,5,6")
have_read_topic_id_list.extend(recommend_topic_ids) have_read_topic_id_list.extend(recommend_topic_ids)
redis_dict = { redis_dict = {
...@@ -48,27 +48,31 @@ def get_discover_page_topic_ids(user_id, device_id, size, query_type=TopicPageTy ...@@ -48,27 +48,31 @@ def get_discover_page_topic_ids(user_id, device_id, size, query_type=TopicPageTy
def get_home_recommend_topic_ids(user_id, device_id, tag_id, offset, size, query=None, def get_home_recommend_topic_ids(user_id, device_id, tag_id, offset, size, query=None,
query_type=TopicPageType.HOME_RECOMMEND,promote_topic_list = [],disable_collpase=False,usefulrecall = -1,useful_tag_list =[]): query_type=TopicPageType.HOME_RECOMMEND, promote_topic_list=[], disable_collpase=False,
usefulrecall=-1, useful_tag_list=[]):
try: try:
topic_star_routing = "6" topic_star_routing = "6"
index_type = "topic-high-star" index_type = "topic-high-star"
device_redis_key = "" device_redis_key = ""
if query is None: if query is None:
if user_id>0: if user_id > 0:
redis_key = "physical:home_recommend" + ":user_id:" + str(user_id) + ":query_type:" + str(query_type) redis_key = "physical:home_recommend" + ":user_id:" + str(user_id) + ":query_type:" + str(query_type)
device_redis_key = "physical:home_recommend" + ":device_id:" + device_id + ":query_type:" + str(query_type) device_redis_key = "physical:home_recommend" + ":device_id:" + device_id + ":query_type:" + str(
query_type)
else: else:
redis_key = "physical:home_recommend" + ":device_id:" + device_id + ":query_type:" + str(query_type) redis_key = "physical:home_recommend" + ":device_id:" + device_id + ":query_type:" + str(query_type)
else: else:
topic_star_routing = "3,4,5,6" topic_star_routing = "3,4,5,6"
index_type = "topic" index_type = "topic"
if user_id>0: if user_id > 0:
redis_key = "physical:home_query" + ":user_id:" + str(user_id) + ":query:" + str(query) + ":query_type:" + str(query_type) redis_key = "physical:home_query" + ":user_id:" + str(user_id) + ":query:" + str(
device_redis_key = "physical:home_query" + ":device_id:" + device_id + ":query:" + str(query) + ":query_type:" + str(query_type) query) + ":query_type:" + str(query_type)
device_redis_key = "physical:home_query" + ":device_id:" + device_id + ":query:" + str(
query) + ":query_type:" + str(query_type)
else: else:
redis_key = "physical:home_query" + ":device_id:" + device_id + ":query:" + str(query) + ":query_type:" + str(query_type) redis_key = "physical:home_query" + ":device_id:" + device_id + ":query:" + str(
query) + ":query_type:" + str(query_type)
redis_field_list = [b'have_read_topic_list'] redis_field_list = [b'have_read_topic_list']
redis_field_val_list = redis_client.hmget(redis_key, redis_field_list) redis_field_val_list = redis_client.hmget(redis_key, redis_field_list)
...@@ -80,18 +84,17 @@ def get_home_recommend_topic_ids(user_id, device_id, tag_id, offset, size, query ...@@ -80,18 +84,17 @@ def get_home_recommend_topic_ids(user_id, device_id, tag_id, offset, size, query
if query is None: if query is None:
have_read_topic_id_list = list(json.loads(redis_field_val_list[0])) have_read_topic_id_list = list(json.loads(redis_field_val_list[0]))
else: else:
if offset>0: # 首次搜索时不需要过滤已读 if offset > 0: # 首次搜索时不需要过滤已读
have_read_topic_id_list = list(json.loads(redis_field_val_list[0])) have_read_topic_id_list = list(json.loads(redis_field_val_list[0]))
elif len(device_redis_key)>0: elif len(device_redis_key) > 0:
redis_field_val_list = redis_client.hmget(device_redis_key, redis_field_list) redis_field_val_list = redis_client.hmget(device_redis_key, redis_field_list)
if redis_field_val_list[0]: if redis_field_val_list[0]:
if query is None: if query is None:
have_read_topic_id_list = list(json.loads(redis_field_val_list[0])) have_read_topic_id_list = list(json.loads(redis_field_val_list[0]))
else: else:
if offset>0: # 首次搜索时不需要过滤已读 if offset > 0: # 首次搜索时不需要过滤已读
have_read_topic_id_list = list(json.loads(redis_field_val_list[0])) have_read_topic_id_list = list(json.loads(redis_field_val_list[0]))
if have_read_topic_id_list == None: if have_read_topic_id_list == None:
have_read_topic_id_list = list() have_read_topic_id_list = list()
have_read_topic_id_list.extend(promote_topic_list) have_read_topic_id_list.extend(promote_topic_list)
...@@ -104,7 +107,8 @@ def get_home_recommend_topic_ids(user_id, device_id, tag_id, offset, size, query ...@@ -104,7 +107,8 @@ def get_home_recommend_topic_ids(user_id, device_id, tag_id, offset, size, query
# 有用标签召回 # 有用标签召回
if usefulrecall != -1: if usefulrecall != -1:
useful_topic_id_list = TopicUtils.userful_tag_topic_list(user_id, have_read_topic_id_list, 4, useful_topic_id_list = TopicUtils.userful_tag_topic_list(user_id, have_read_topic_id_list, 4,
"topic-high-star", "6",useful_tag_list=useful_tag_list) "topic-high-star", "6",
useful_tag_list=useful_tag_list)
# user_similar_score_redis_key = "physical:user_similar_score:user_id:" + str(user_id) # user_similar_score_redis_key = "physical:user_similar_score:user_id:" + str(user_id)
# redis_user_similar_score_redis_val = redis_client.get(user_similar_score_redis_key) # redis_user_similar_score_redis_val = redis_client.get(user_similar_score_redis_key)
# user_similar_score_redis_list = json.loads( # user_similar_score_redis_list = json.loads(
...@@ -124,18 +128,20 @@ def get_home_recommend_topic_ids(user_id, device_id, tag_id, offset, size, query ...@@ -124,18 +128,20 @@ def get_home_recommend_topic_ids(user_id, device_id, tag_id, offset, size, query
linucb_recommend_topic_id_list = list() linucb_recommend_topic_id_list = list()
# 推荐帖子是强插的,要保证推荐帖子不在已读里 # 推荐帖子是强插的,要保证推荐帖子不在已读里
logging.warning("type1:%s,type2:%s"%(type(linucb_recommend_topic_id_list),type(have_read_topic_id_list))) logging.warning(
"type1:%s,type2:%s" % (type(linucb_recommend_topic_id_list), type(have_read_topic_id_list)))
recommend_topic_id_list = list(set(linucb_recommend_topic_id_list) - set(have_read_topic_id_list)) recommend_topic_id_list = list(set(linucb_recommend_topic_id_list) - set(have_read_topic_id_list))
recommend_topic_id_list.sort(key=linucb_recommend_topic_id_list.index) recommend_topic_id_list.sort(key=linucb_recommend_topic_id_list.index)
# cursor = int(str(recommend_topic_dict[b"cursor"], encoding="utf-8")) # cursor = int(str(recommend_topic_dict[b"cursor"], encoding="utf-8"))
# newcursor = cursor + 6 # newcursor = cursor + 6
if len(recommend_topic_id_list) > 0: if len(recommend_topic_id_list) > 0:
recommend_topic_list = recommend_topic_id_list[0:size] recommend_topic_list = recommend_topic_id_list[0:size]
# redis_client.hset(topic_recommend_redis_key, "cursor", newcursor) # redis_client.hset(topic_recommend_redis_key, "cursor", newcursor)
if b"datadict" in recommend_topic_dict: if b"datadict" in recommend_topic_dict:
linucb_recommend_topic_id_dict = json.loads(recommend_topic_dict[b"datadict"]) linucb_recommend_topic_id_dict = json.loads(recommend_topic_dict[b"datadict"])
if linucb_recommend_topic_id_dict is not None and len(recommend_topic_list) >0: if linucb_recommend_topic_id_dict is not None and len(recommend_topic_list) > 0:
for i in recommend_topic_list: for i in recommend_topic_list:
recommend_topic_user_list.append(linucb_recommend_topic_id_dict[str(i)]) recommend_topic_user_list.append(linucb_recommend_topic_id_dict[str(i)])
# if have_read_topic_id_list == None: # if have_read_topic_id_list == None:
...@@ -143,8 +149,8 @@ def get_home_recommend_topic_ids(user_id, device_id, tag_id, offset, size, query ...@@ -143,8 +149,8 @@ def get_home_recommend_topic_ids(user_id, device_id, tag_id, offset, size, query
# 用户关注标签 # 用户关注标签
redis_tag_data = redis_client.hget("physical:linucb:register_user_tag_info", user_id) redis_tag_data = redis_client.hget("physical:linucb:register_user_tag_info", user_id)
attention_tag_list = json.loads(redis_tag_data) if redis_tag_data else [] attention_tag_list = json.loads(redis_tag_data) if redis_tag_data else []
if len(recommend_topic_list)>0: if len(recommend_topic_list) > 0:
size = size-len(recommend_topic_list) size = size - len(recommend_topic_list)
have_read_topic_id_list.extend(recommend_topic_list) have_read_topic_id_list.extend(recommend_topic_list)
# have_read_topic_id_list_add_promote = list() # have_read_topic_id_list_add_promote = list()
...@@ -155,11 +161,15 @@ def get_home_recommend_topic_ids(user_id, device_id, tag_id, offset, size, query ...@@ -155,11 +161,15 @@ def get_home_recommend_topic_ids(user_id, device_id, tag_id, offset, size, query
# for topic_id in promote_recommend_topic_id_list: # for topic_id in promote_recommend_topic_id_list:
# have_read_topic_id_list_add_promote.append(topic_id) # have_read_topic_id_list_add_promote.append(topic_id)
rank_topic_id_list = list() rank_topic_id_list = list()
if size>0: if size > 0:
rank_topic_id_list = TopicUtils.get_recommend_topic_ids(user_id=user_id, tag_id=tag_id, offset=0, size=size, rank_topic_id_list = TopicUtils.get_recommend_topic_ids(user_id=user_id, tag_id=tag_id, offset=0, size=size,
single_size=size,query=query, query_type=query_type, single_size=size, query=query,
query_type=query_type,
filter_topic_id_list=have_read_topic_id_list, filter_topic_id_list=have_read_topic_id_list,
index_type=index_type,routing=topic_star_routing,attention_tag_list=attention_tag_list,linucb_user_id_list=recommend_topic_user_list,disable_collpase=disable_collpase) index_type=index_type, routing=topic_star_routing,
attention_tag_list=attention_tag_list,
linucb_user_id_list=recommend_topic_user_list,
disable_collpase=disable_collpase)
# if len(recommend_topic_list) == 6 and query is None: # if len(recommend_topic_list) == 6 and query is None:
# if (size < 11): # if (size < 11):
...@@ -176,7 +186,7 @@ def get_home_recommend_topic_ids(user_id, device_id, tag_id, offset, size, query ...@@ -176,7 +186,7 @@ def get_home_recommend_topic_ids(user_id, device_id, tag_id, offset, size, query
# topic_id_list.extend(rank_topic_id_list) # topic_id_list.extend(rank_topic_id_list)
have_read_topic_id_list.extend(rank_topic_id_list) have_read_topic_id_list.extend(rank_topic_id_list)
if len(have_read_topic_id_list) > 30000: if len(have_read_topic_id_list) > 30000:
cut_len = len(have_read_topic_id_list)-30000 cut_len = len(have_read_topic_id_list) - 30000
have_read_topic_id_list = have_read_topic_id_list[cut_len:] have_read_topic_id_list = have_read_topic_id_list[cut_len:]
redis_dict = { redis_dict = {
"have_read_topic_list": json.dumps(have_read_topic_id_list), "have_read_topic_list": json.dumps(have_read_topic_id_list),
...@@ -185,13 +195,13 @@ def get_home_recommend_topic_ids(user_id, device_id, tag_id, offset, size, query ...@@ -185,13 +195,13 @@ def get_home_recommend_topic_ids(user_id, device_id, tag_id, offset, size, query
# 每个session key保存60天 # 每个session key保存60天
redis_client.expire(redis_key, 60 * 60 * 24 * 60) redis_client.expire(redis_key, 60 * 60 * 24 * 60)
if usefulrecall != -1: if usefulrecall != -1:
return recommend_topic_list,rank_topic_id_list,useful_topic_id_list return recommend_topic_list, rank_topic_id_list, useful_topic_id_list
else: else:
return recommend_topic_list, rank_topic_id_list return recommend_topic_list, rank_topic_id_list
except: except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc()) logging.error("catch exception,err_msg:%s" % traceback.format_exc())
if usefulrecall != -1: if usefulrecall != -1:
return [],[],[] return [], [], []
else: else:
return [], [] return [], []
...@@ -206,7 +216,10 @@ def query_tag_id_by_topic(offset=0, size=10, topic_id_list=[], user_id=-1): ...@@ -206,7 +216,10 @@ def query_tag_id_by_topic(offset=0, size=10, topic_id_list=[], user_id=-1):
@bind("physical/search/home_recommend") @bind("physical/search/home_recommend")
def home_recommend(device_id="", user_id=-1, offset=0, size=10, query_type=TopicPageType.HOME_RECOMMEND,promote_topic_list=[],usefulrecall = -1,useful_tag_list=[]): def home_recommend(device_id="", user_id=-1, offset=0, size=10, query_type=TopicPageType.HOME_RECOMMEND,
promote_topic_list=[], usefulrecall=-1, useful_tag_list=[]):
""" """
:remark:首页推荐,目前只推荐日记 :remark:首页推荐,目前只推荐日记
:param session_id: :param session_id:
...@@ -239,25 +252,50 @@ def home_recommend(device_id="", user_id=-1, offset=0, size=10, query_type=Topic ...@@ -239,25 +252,50 @@ def home_recommend(device_id="", user_id=-1, offset=0, size=10, query_type=Topic
have_read_topic_id_list = list(json.loads(redis_field_val_list[0])) have_read_topic_id_list = list(json.loads(redis_field_val_list[0]))
if len(have_read_topic_id_list) > offset: if len(have_read_topic_id_list) > offset:
recommend_topic_ids = have_read_topic_id_list[offset:offset+size] recommend_topic_ids = have_read_topic_id_list[offset:offset + size]
else: else:
recommend_topic_ids = have_read_topic_id_list[0:size] recommend_topic_ids = have_read_topic_id_list[0:size]
else: else:
if usefulrecall != -1 and len(useful_tag_list)>0: if usefulrecall != -1 and len(useful_tag_list) > 0:
recommend_topic_ids,rank_topic_ids,useful_topic_ids = get_home_recommend_topic_ids(user_id, device_id, tag_id=0, offset=0, size=size, recommend_topic_ids, rank_topic_ids, useful_topic_ids = get_home_recommend_topic_ids(user_id, device_id, tag_id=0,
query_type=query_type,promote_topic_list=promote_topic_list,usefulrecall=usefulrecall,useful_tag_list=useful_tag_list) offset=0, size=size,
return {"linucb_topic_ids": recommend_topic_ids,"rank_topic_ids":rank_topic_ids,"useful_topic_ids":useful_topic_ids} query_type=query_type,
else: promote_topic_list=promote_topic_list,
recommend_topic_ids,rank_topic_ids = get_home_recommend_topic_ids(user_id, device_id, tag_id=0, offset=0, size=size, usefulrecall=usefulrecall,
query_type=query_type,promote_topic_list=promote_topic_list) useful_tag_list=useful_tag_list)
return {"linucb_topic_ids": recommend_topic_ids, "rank_topic_ids": rank_topic_ids,
return {"linucb_topic_ids": recommend_topic_ids,"rank_topic_ids":rank_topic_ids} "useful_topic_ids": useful_topic_ids}
except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc())
if usefulrecall != -1:
return {"linucb_topic_ids": [],"rank_topic_ids":[],"useful_topic_ids":[]}
else: else:
recommend_topic_ids, rank_topic_ids = get_home_recommend_topic_ids(user_id, device_id, tag_id=0, offset=0,
size=size,
query_type=query_type,
promote_topic_list=promote_topic_list)
return {"linucb_topic_ids": recommend_topic_ids, "rank_topic_ids": rank_topic_ids}
== == == =
if usefulrecall != -1:
recommend_topic_ids, rank_topic_ids, useful_topic_ids = get_home_recommend_topic_ids(user_id, device_id,
tag_id=0, offset=0,
size=size,
query_type=query_type,
promote_topic_list=promote_topic_list,
usefulrecall=usefulrecall)
return {"linucb_topic_ids": recommend_topic_ids, "rank_topic_ids": rank_topic_ids,
"useful_topic_ids": useful_topic_ids}
else:
recommend_topic_ids, rank_topic_ids = get_home_recommend_topic_ids(user_id, device_id, tag_id=0,
offset=0, size=size,
query_type=query_type,
promote_topic_list=promote_topic_list)
return {"linucb_topic_ids": recommend_topic_ids, "rank_topic_ids": rank_topic_ids}
>> >> >> > hotword
except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc())
if usefulrecall != -1:
return {"linucb_topic_ids": [], "rank_topic_ids": [], "useful_topic_ids": []}
else:
return {"linucb_topic_ids": [], "rank_topic_ids": []} return {"linucb_topic_ids": [], "rank_topic_ids": []}
...@@ -302,11 +340,12 @@ def home_query(device_id="", tag_id=-1, user_id=-1, query="", offset=0, size=10) ...@@ -302,11 +340,12 @@ def home_query(device_id="", tag_id=-1, user_id=-1, query="", offset=0, size=10)
if not isinstance(device_id, str): if not isinstance(device_id, str):
device_id = "" device_id = ""
recommend_topic_list, rank_topic_id_list = get_home_recommend_topic_ids(user_id, device_id, tag_id, offset=offset, size=size, query=query) recommend_topic_list, rank_topic_id_list = get_home_recommend_topic_ids(user_id, device_id, tag_id,
if len(rank_topic_id_list)>0 and len(rank_topic_id_list)<size: offset=offset, size=size, query=query)
if len(rank_topic_id_list) > 0 and len(rank_topic_id_list) < size:
recommend_topic_list, rank_topic_id_list = get_home_recommend_topic_ids(user_id, device_id, tag_id, recommend_topic_list, rank_topic_id_list = get_home_recommend_topic_ids(user_id, device_id, tag_id,
offset=offset, size=size, offset=offset, size=size,
query=query,disable_collpase=True) query=query, disable_collpase=True)
return {"recommend_topic_ids": rank_topic_id_list} return {"recommend_topic_ids": rank_topic_id_list}
except: except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc()) logging.error("catch exception,err_msg:%s" % traceback.format_exc())
...@@ -328,9 +367,9 @@ def topic_detail_page_recommend(device_id="", user_id=-1, topic_id=-1, topic_pic ...@@ -328,9 +367,9 @@ def topic_detail_page_recommend(device_id="", user_id=-1, topic_id=-1, topic_pic
if not isinstance(user_id, int): if not isinstance(user_id, int):
user_id = -1 user_id = -1
if user_id > 0: if user_id > 0:
redis_key = "physical:topic_detail_recommend" + ":user_id:" + str(user_id) + "topic_id:"+str(topic_id) redis_key = "physical:topic_detail_recommend" + ":user_id:" + str(user_id) + "topic_id:" + str(topic_id)
else: else:
redis_key = "physical:topic_detail_recommend" + ":device_id:" + device_id + "topic_id:"+str(topic_id) redis_key = "physical:topic_detail_recommend" + ":device_id:" + device_id + "topic_id:" + str(topic_id)
if int(offset) == 0: if int(offset) == 0:
have_read_topic_list = list() have_read_topic_list = list()
# redis_dict = { # redis_dict = {
...@@ -343,7 +382,7 @@ def topic_detail_page_recommend(device_id="", user_id=-1, topic_id=-1, topic_pic ...@@ -343,7 +382,7 @@ def topic_detail_page_recommend(device_id="", user_id=-1, topic_id=-1, topic_pic
have_read_topic_list = list() have_read_topic_list = list()
redis_field_list = [b'have_read_topic_list'] redis_field_list = [b'have_read_topic_list']
have_read_topic_redis_data = redis_client.hmget(redis_key,redis_field_list) have_read_topic_redis_data = redis_client.hmget(redis_key, redis_field_list)
have_read_topic_list = json.loads(have_read_topic_redis_data[0]) if have_read_topic_redis_data[0] else [] have_read_topic_list = json.loads(have_read_topic_redis_data[0]) if have_read_topic_redis_data[0] else []
es_cli_obj = ESPerform.get_cli() es_cli_obj = ESPerform.get_cli()
have_read_topic_list.append(topic_id) have_read_topic_list.append(topic_id)
...@@ -351,18 +390,23 @@ def topic_detail_page_recommend(device_id="", user_id=-1, topic_id=-1, topic_pic ...@@ -351,18 +390,23 @@ def topic_detail_page_recommend(device_id="", user_id=-1, topic_id=-1, topic_pic
topic_tag_result = list() topic_tag_result = list()
result = list() result = list()
if len(topic_tag_list) != 0: if len(topic_tag_list) != 0:
topic_tag_result = TopicUtils.top_get_topic_detail_recommend_list(user_id,topic_id,have_read_topic_list,size,es_cli_obj, topic_tag_result = TopicUtils.top_get_topic_detail_recommend_list(user_id, topic_id, have_read_topic_list,
index_type="topic",routing="3,4,5,6",topic_tag_list = topic_tag_list) size, es_cli_obj,
index_type="topic", routing="3,4,5,6",
topic_tag_list=topic_tag_list)
topic_tag_size = len(topic_tag_result) topic_tag_size = len(topic_tag_result)
have_read_topic_list.extend(topic_tag_result) have_read_topic_list.extend(topic_tag_result)
else: else:
topic_tag_size = 0 topic_tag_size = 0
if topic_tag_size <size: if topic_tag_size < size:
size = size - topic_tag_size size = size - topic_tag_size
if topic_user_id != -1: if topic_user_id != -1:
topic_user_result = TopicUtils.top_get_topic_detail_recommend_list(user_id, topic_id, have_read_topic_list, topic_user_result = TopicUtils.top_get_topic_detail_recommend_list(user_id, topic_id,
have_read_topic_list,
size, es_cli_obj, size, es_cli_obj,
index_type="topic", routing="3,4,5,6",topic_user_id = topic_user_id index_type="topic",
routing="3,4,5,6",
topic_user_id=topic_user_id
) )
topic_user_size = len(topic_user_result) topic_user_size = len(topic_user_result)
have_read_topic_list.extend(topic_user_result) have_read_topic_list.extend(topic_user_result)
...@@ -448,7 +492,7 @@ def topic_search(filters, nfilters=None, sorts_by=None, offset=0, size=10): ...@@ -448,7 +492,7 @@ def topic_search(filters, nfilters=None, sorts_by=None, offset=0, size=10):
"""帖子搜索。""" """帖子搜索。"""
try: try:
(topic_id_list,total_count) = TopicUtils.list_topic_ids(filters=filters, nfilters=nfilters, (topic_id_list, total_count) = TopicUtils.list_topic_ids(filters=filters, nfilters=nfilters,
sorts_by=sorts_by, offset=offset, size=size) sorts_by=sorts_by, offset=offset, size=size)
return { return {
...@@ -472,10 +516,23 @@ def query_topic_by_user_similarity(topic_similarity_score_dict, offset=0, size=1 ...@@ -472,10 +516,23 @@ def query_topic_by_user_similarity(topic_similarity_score_dict, offset=0, size=1
try: try:
must_topic_id_list = list(topic_similarity_score_dict.keys()) must_topic_id_list = list(topic_similarity_score_dict.keys())
topic_id_list = TopicUtils.get_recommend_topic_ids(tag_id=0, user_id=-1, offset=offset, size=size,single_size=size, topic_id_list = TopicUtils.get_recommend_topic_ids(tag_id=0, user_id=-1, offset=offset, size=size,
must_topic_id_list=must_topic_id_list,index_type="topic",routing="4,5,6") single_size=size,
must_topic_id_list=must_topic_id_list, index_type="topic",
routing="4,5,6")
return {"recommend_topic_ids": topic_id_list} return {"recommend_topic_ids": topic_id_list}
except: except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc()) logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return {"recommend_topic_id": []} return {"recommend_topic_id": []}
@bind("physical/search/query_topic_search")
def query_topic_search(query=""):
"""
召回搜索帖子
当搜索的内容 完全匹配 用户昵称,且该用户为推荐用户时,帖子tab中也会展示用户栏
:param query:
:return:
"""
# 召回完全匹配的用户
...@@ -14,7 +14,7 @@ from libs.es import ESPerform ...@@ -14,7 +14,7 @@ from libs.es import ESPerform
@bind("physical/search/recommend_user") @bind("physical/search/recommend_user")
def recommend_user(self_user_id,interesting_user_id,offset=0,size=10): def recommend_user(self_user_id, interesting_user_id, offset=0, size=10):
""" """
:remark 点关注推荐用户 :remark 点关注推荐用户
:param self_user_id: :param self_user_id:
...@@ -29,25 +29,29 @@ def recommend_user(self_user_id,interesting_user_id,offset=0,size=10): ...@@ -29,25 +29,29 @@ def recommend_user(self_user_id,interesting_user_id,offset=0,size=10):
if not isinstance(interesting_user_id, int): if not isinstance(interesting_user_id, int):
interesting_user_id = -1 interesting_user_id = -1
#获取es链接对象 # 获取es链接对象
es_cli_obj = ESPerform.get_cli() es_cli_obj = ESPerform.get_cli()
#获取关注用户列表 # 获取关注用户列表
(self_attention_user_id_list,recursion_attention_user_id_list) = UserUtils.get_attention_user_list([self_user_id,interesting_user_id],self_user_id,es_cli_obj) (self_attention_user_id_list, recursion_attention_user_id_list) = UserUtils.get_attention_user_list(
[self_user_id, interesting_user_id], self_user_id, es_cli_obj)
#去除自身及感兴趣的用户ID # 去除自身及感兴趣的用户ID
self_attention_user_id_list.append(self_user_id) self_attention_user_id_list.append(self_user_id)
self_attention_user_id_list.append(interesting_user_id) self_attention_user_id_list.append(interesting_user_id)
recommend_user_list = UserUtils.get_recommend_user_list(self_attention_user_id_list,recursion_attention_user_id_list,offset,size,es_cli_obj) recommend_user_list = UserUtils.get_recommend_user_list(self_attention_user_id_list,
recursion_attention_user_id_list, offset, size,
es_cli_obj)
return recommend_user_list return recommend_user_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 []
@bind("physical/search/batch_recommend_user") @bind("physical/search/batch_recommend_user")
def batch_recommend_user(self_user_id,interesting_user_id_list,offset=0,size=10): def batch_recommend_user(self_user_id, interesting_user_id_list, offset=0, size=10):
""" """
:remark 点关注推荐用户 - 批量接口 :remark 点关注推荐用户 - 批量接口
:param self_user_id: :param self_user_id:
...@@ -57,23 +61,129 @@ def batch_recommend_user(self_user_id,interesting_user_id_list,offset=0,size=10) ...@@ -57,23 +61,129 @@ def batch_recommend_user(self_user_id,interesting_user_id_list,offset=0,size=10)
:return: :return:
""" """
try: try:
if not isinstance(self_user_id,int): if not isinstance(self_user_id, int):
self_user_id = -1 self_user_id = -1
#获取es链接对象 # 获取es链接对象
es_cli_obj = ESPerform.get_cli() es_cli_obj = ESPerform.get_cli()
#获取关注用户列表 # 获取关注用户列表
(need_filter_attention_user_id_list, attention_user_dict_list,attention_user_id_list) = UserUtils.get_batch_attention_user_list(interesting_user_id_list,self_user_id,es_cli_obj) (need_filter_attention_user_id_list, attention_user_dict_list,
attention_user_id_list) = UserUtils.get_batch_attention_user_list(interesting_user_id_list, self_user_id,
es_cli_obj)
# 去除自身及感兴趣的用户ID
#去除自身及感兴趣的用户ID
need_filter_attention_user_id_list.append(self_user_id) need_filter_attention_user_id_list.append(self_user_id)
recommend_user_dict = UserUtils.get_batch_recommend_user_dict(need_filter_attention_user_id_list=need_filter_attention_user_id_list,attention_user_id_list=attention_user_id_list,attention_user_dict_list=attention_user_dict_list,self_user_id=self_user_id,offset=offset,size=size,es_cli_obj=es_cli_obj) recommend_user_dict = UserUtils.get_batch_recommend_user_dict(
need_filter_attention_user_id_list=need_filter_attention_user_id_list,
attention_user_id_list=attention_user_id_list, attention_user_dict_list=attention_user_dict_list,
self_user_id=self_user_id, offset=offset, size=size, es_cli_obj=es_cli_obj)
logging.info("duan add,recommend_user_dict:%s" % str(recommend_user_dict)) logging.info("duan add,recommend_user_dict:%s" % str(recommend_user_dict))
return recommend_user_dict return recommend_user_dict
except: except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc()) logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return {} return {}
@bind("physical/search/search_user")
def search_user(query="", offset=0, size=10):
"""
:remark 搜索用户
:param query:
:param offset:
:param size:
:return:
搜索域:用户名称
1.识别query:允许模糊匹配
2.召回部分:全部 线上状态用户
3.rank部分:一级-是否推荐,二级-发帖数
"""
try:
es_cli_obj = ESPerform.get_cli()
q = {}
# 先获取完全匹配的用户ID
sheer_user_id = []
q["query"] = {
"bool": {
"must": [
{"term": {
"nick_name_pre": query
}
}, {
"term": {
"is_recommend": True
}
}
]
}
}
q["sort"] = {
"count_topic": {
"order": "desc"
}
}
# que = {"query": {"term": {"nick_name_pre": query}}}
result_dict = ESPerform.get_search_results(es_cli_obj, "user", q, 0, 10)
res = result_dict["hits"]
if len(res) > 0:
user_id = [item["_source"]["user_id"] for item in res]
sheer_user_id.append(user_id)
logging.info("get res:%s" % res)
# 获取模糊匹配的用户ID
multi_match = {
"fields": ["nick_name"],
"type": "cross_fields",
"operator": "and",
"query": query
}
q = {}
q["query"] = {
"bool": {
"must": [{
"multi_match": multi_match
}, {
"term": {
"is_online": True
}
}
]
}
}
q["sort"] = {
"is_recommend": {
"order": "desc"
},
"count_topic": {
"order": "desc"
},
}
logging.info("get q:%s" % q)
result_dict = ESPerform.get_search_results(es_cli_obj, "user", q, offset, size)
logging.info("get result_dict:%s" % result_dict)
search_user_id = []
res = result_dict["hits"]
if len(res) > 0:
search_user_id = [item["_source"]["user_id"] for item in res]
return {"sheer_user_id": sheer_user_id, "search_user_id": search_user_id}
except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return {"sheer_user_id": [], "search_user_id": []}
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
"topic_id_list":{"type":"long"}, "topic_id_list":{"type":"long"},
"effective":{"type":"boolean"}, "effective":{"type":"boolean"},
"offline_score":{"type":"long"}, "offline_score":{"type":"long"},
"is_default":{"type":"long"} "is_default":{"type":"long"},
"is_cover":{"type":"boolean"}
} }
} }
\ No newline at end of file
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
"id":{"type":"long"}, "id":{"type":"long"},
"user_id":{"type":"long"}, "user_id":{"type":"long"},
"nick_name":{"type":"text","analyzer":"gm_default_index","search_analyzer":"gm_default_index"}, //昵称 "nick_name":{"type":"text","analyzer":"gm_default_index","search_analyzer":"gm_default_index"}, //昵称
"nick_name_pre":{"type":"text","analyzer":"not_analyzed"}, //昵称
"profile_pic":{"type":"text"}, //头像 "profile_pic":{"type":"text"}, //头像
"gender":{"type":"integer"}, "gender":{"type":"integer"},
"is_online":{"type":"boolean"},//是否上线 "is_online":{"type":"boolean"},//是否上线
...@@ -60,6 +61,7 @@ ...@@ -60,6 +61,7 @@
"create_time":{"type":"date", "format":"date_time_no_millis"}, "create_time":{"type":"date", "format":"date_time_no_millis"},
"update_time":{"type":"date", "format":"date_time_no_millis"}, "update_time":{"type":"date", "format":"date_time_no_millis"},
"create_time_val":{"type":"long"}, "create_time_val":{"type":"long"},
"update_time_val":{"type":"long"} "update_time_val":{"type":"long"},
"count_topic":{"type":"long"}
} }
} }
\ No newline at end of file
...@@ -63,27 +63,27 @@ class Pictorial(models.Model): ...@@ -63,27 +63,27 @@ class Pictorial(models.Model):
def get_topic_id(self): def get_topic_id(self):
try: try:
topic_id_list = list(PictorialTopics.objects.filter(pictorial_id=self.id,is_online=True,is_deleted=False).values_list("topic_id", flat=True)) topic_id_list = list(
PictorialTopics.objects.filter(pictorial_id=self.id, is_online=True, is_deleted=False).values_list(
"topic_id", flat=True))
return topic_id_list return topic_id_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 []
def get_effective(self, topic_id_list):
def get_effective(self,topic_id_list):
try: try:
effective_num = 0 effective_num = 0
ret = False ret = False
for topic_id in topic_id_list: for topic_id in topic_id_list:
topic_id_object = Topic.objects.filter(id=int(topic_id)).first() topic_id_object = Topic.objects.filter(id=int(topic_id)).first()
if topic_id_object and topic_id_object.is_online and int(topic_id_object.content_level) in [0,3,4,5]: if topic_id_object and topic_id_object.is_online and int(topic_id_object.content_level) in [0, 3, 4, 5]:
effective_num += 1 effective_num += 1
if effective_num >= 5: if effective_num >= 5:
ret = True ret = True
break break
return ret return ret
except: except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc()) logging.error("catch exception,err_msg:%s" % traceback.format_exc())
...@@ -109,7 +109,8 @@ class Pictorial(models.Model): ...@@ -109,7 +109,8 @@ class Pictorial(models.Model):
def get_tag_by_id(self): def get_tag_by_id(self):
try: try:
tag_id_list = list(PictorialTag.objects.filter(pictorial_id=self.id, is_online=True).values_list("tag_id", flat=True)) tag_id_list = list(
PictorialTag.objects.filter(pictorial_id=self.id, is_online=True).values_list("tag_id", flat=True))
return tag_id_list return tag_id_list
except: except:
...@@ -125,6 +126,25 @@ class Pictorial(models.Model): ...@@ -125,6 +126,25 @@ class Pictorial(models.Model):
logging.error("catch exception,err_msg:%s" % traceback.format_exc()) logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return [] return []
def get_is_cover(self, topic_id_list):
try:
effective_num = 0
ret = False
for topic_id in topic_id_list:
topic_id_object = Topic.objects.filter(id=int(topic_id)).first()
if topic_id_object and topic_id_object.is_online and int(topic_id_object.content_level) in [3, 4, 5, 6]:
effective_num += 1
if effective_num >= 5:
ret = True
break
return ret
except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return False
class PictorialTag(models.Model): class PictorialTag(models.Model):
"""画报关注标签""" """画报关注标签"""
......
...@@ -12,10 +12,10 @@ import datetime ...@@ -12,10 +12,10 @@ import datetime
class TopicTag(models.Model): class TopicTag(models.Model):
class Meta: class Meta:
verbose_name=u"帖子标签" verbose_name = u"帖子标签"
db_table="community_topictag" db_table = "community_topictag"
id = models.IntegerField(primary_key=True,verbose_name=u"主键ID") id = models.IntegerField(primary_key=True, verbose_name=u"主键ID")
tag_id = models.IntegerField(verbose_name=u"标签ID") tag_id = models.IntegerField(verbose_name=u"标签ID")
topic_id = models.IntegerField(verbose_name=u"帖子ID") topic_id = models.IntegerField(verbose_name=u"帖子ID")
is_online = models.BooleanField(verbose_name=u"是否在线") is_online = models.BooleanField(verbose_name=u"是否在线")
...@@ -25,73 +25,84 @@ class TopicTag(models.Model): ...@@ -25,73 +25,84 @@ class TopicTag(models.Model):
class AccountUserTag(models.Model): class AccountUserTag(models.Model):
class Meta: class Meta:
verbose_name=u"用户标签" verbose_name = u"用户标签"
db_table="account_user_tag" db_table = "account_user_tag"
id = models.IntegerField(primary_key=True,verbose_name=u"主键ID") id = models.IntegerField(primary_key=True, verbose_name=u"主键ID")
tag_id = models.IntegerField(verbose_name=u"标签ID") tag_id = models.IntegerField(verbose_name=u"标签ID")
user = models.IntegerField(verbose_name=u"用户ID") user = models.IntegerField(verbose_name=u"用户ID")
is_deleted = models.BooleanField(verbose_name=u"是否删除") is_deleted = models.BooleanField(verbose_name=u"是否删除")
create_time = models.DateTimeField(verbose_name=u'创建时间',default=datetime.datetime.fromtimestamp(0)) create_time = models.DateTimeField(verbose_name=u'创建时间', default=datetime.datetime.fromtimestamp(0))
update_time = models.DateTimeField(verbose_name=u'更新时间', default=datetime.datetime.fromtimestamp(0)) update_time = models.DateTimeField(verbose_name=u'更新时间', default=datetime.datetime.fromtimestamp(0))
is_body_esthetics = models.IntegerField(verbose_name=u"是否有用标签") is_body_esthetics = models.IntegerField(verbose_name=u"是否有用标签")
class Tag(models.Model): class Tag(models.Model):
class Meta: class Meta:
verbose_name=u"标签" verbose_name = u"标签"
db_table="community_tag" db_table = "community_tag"
id = models.IntegerField(primary_key=True,verbose_name=u"主键ID") id = models.IntegerField(primary_key=True, verbose_name=u"主键ID")
is_deleted = models.BooleanField(verbose_name=u"是否删除") is_deleted = models.BooleanField(verbose_name=u"是否删除")
is_online = models.BooleanField(verbose_name=u"是否上线") is_online = models.BooleanField(verbose_name=u"是否上线")
name = models.CharField(verbose_name=u"标签名称",max_length=128) name = models.CharField(verbose_name=u"标签名称", max_length=128)
description = models.TextField(verbose_name=u"标签描述") description = models.TextField(verbose_name=u"标签描述")
icon_url=models.CharField(verbose_name=u"icon_url",max_length=120) icon_url = models.CharField(verbose_name=u"icon_url", max_length=120)
collection = models.IntegerField(verbose_name=u"是否编辑") collection = models.IntegerField(verbose_name=u"是否编辑")
is_ai = models.IntegerField(verbose_name=u"是否ai") is_ai = models.IntegerField(verbose_name=u"是否ai")
is_own = models.IntegerField(verbose_name=u"是否ins上自带") is_own = models.IntegerField(verbose_name=u"是否ins上自带")
create_time = models.DateTimeField(verbose_name=u'创建时间',default=datetime.datetime.fromtimestamp(0)) create_time = models.DateTimeField(verbose_name=u'创建时间', default=datetime.datetime.fromtimestamp(0))
update_time = models.DateTimeField(verbose_name=u'更新时间', default=datetime.datetime.fromtimestamp(0)) update_time = models.DateTimeField(verbose_name=u'更新时间', default=datetime.datetime.fromtimestamp(0))
class CommunityTagFollow(models.Model): class CommunityTagFollow(models.Model):
class Meta: class Meta:
verbose_name=u"用户关注标签" verbose_name = u"用户关注标签"
db_table="community_tag_follow" db_table = "community_tag_follow"
id = models.IntegerField(primary_key=True,verbose_name=u"主键ID")
id = models.IntegerField(primary_key=True, verbose_name=u"主键ID")
is_deleted = models.BooleanField(verbose_name=u"是否删除") is_deleted = models.BooleanField(verbose_name=u"是否删除")
is_online = models.BooleanField(verbose_name=u"是否上线") is_online = models.BooleanField(verbose_name=u"是否上线")
user_id = models.IntegerField(verbose_name=u"用户ID") user_id = models.IntegerField(verbose_name=u"用户ID")
tag_id = models.IntegerField(verbose_name=u"帖子ID") tag_id = models.IntegerField(verbose_name=u"帖子ID")
create_time = models.DateTimeField(verbose_name=u'创建时间',default=datetime.datetime.fromtimestamp(0)) create_time = models.DateTimeField(verbose_name=u'创建时间', default=datetime.datetime.fromtimestamp(0))
update_time = models.DateTimeField(verbose_name=u'更新时间', default=datetime.datetime.fromtimestamp(0)) update_time = models.DateTimeField(verbose_name=u'更新时间', default=datetime.datetime.fromtimestamp(0))
class CommunityTagTypeRelation(models.Model): class CommunityTagTypeRelation(models.Model):
class Meta: class Meta:
verbose_name=u"标签类型对应关系" verbose_name = u"标签类型对应关系"
db_table="community_tag_type_relation" db_table = "community_tag_type_relation"
id = models.IntegerField(primary_key=True,verbose_name=u"主键ID") id = models.IntegerField(primary_key=True, verbose_name=u"主键ID")
is_deleted = models.BooleanField(verbose_name=u"是否删除") is_deleted = models.BooleanField(verbose_name=u"是否删除")
is_online = models.BooleanField(verbose_name=u"是否上线") is_online = models.BooleanField(verbose_name=u"是否上线")
tag_id = models.IntegerField(verbose_name=u"标签ID") tag_id = models.IntegerField(verbose_name=u"标签ID")
tag_type_id = models.IntegerField(verbose_name=u"标签类型ID") tag_type_id = models.IntegerField(verbose_name=u"标签类型ID")
create_time = models.DateTimeField(verbose_name=u'创建时间',default=datetime.datetime.fromtimestamp(0)) create_time = models.DateTimeField(verbose_name=u'创建时间', default=datetime.datetime.fromtimestamp(0))
update_time = models.DateTimeField(verbose_name=u'更新时间', default=datetime.datetime.fromtimestamp(0)) update_time = models.DateTimeField(verbose_name=u'更新时间', default=datetime.datetime.fromtimestamp(0))
class CommunityTagType(models.Model): class CommunityTagType(models.Model):
class Meta: class Meta:
verbose_name=u"标签类型" verbose_name = u"标签类型"
db_table="community_tag_type" db_table = "community_tag_type"
id = models.IntegerField(primary_key=True,verbose_name=u"主键ID") id = models.IntegerField(primary_key=True, verbose_name=u"主键ID")
is_deleted = models.BooleanField(verbose_name=u"是否删除") is_deleted = models.BooleanField(verbose_name=u"是否删除")
is_online = models.BooleanField(verbose_name=u"是否上线") is_online = models.BooleanField(verbose_name=u"是否上线")
name = models.CharField(verbose_name=u"名称",max_length=64) name = models.CharField(verbose_name=u"名称", max_length=64)
create_time = models.DateTimeField(verbose_name=u'创建时间',default=datetime.datetime.fromtimestamp(0)) create_time = models.DateTimeField(verbose_name=u'创建时间', default=datetime.datetime.fromtimestamp(0))
update_time = models.DateTimeField(verbose_name=u'更新时间', default=datetime.datetime.fromtimestamp(0)) update_time = models.DateTimeField(verbose_name=u'更新时间', default=datetime.datetime.fromtimestamp(0))
class RegisterShowTag(models.Model):
class Meta:
verbose_name = "搜索热词"
db_table = "register_show_tag"
id = models.IntegerField(primary_key=True, verbose_name=u"主键ID")
is_deleted = models.IntegerField(verbose_name=u"是否删除")
is_online = models.IntegerField(verbose_name=u"是否在线")
tag_id = models.IntegerField(verbose_name="标签ID")
...@@ -16,6 +16,8 @@ from .tag import AccountUserTag ...@@ -16,6 +16,8 @@ from .tag import AccountUserTag
from .topic import Topic from .topic import Topic
from .user_extra import UserExtra from .user_extra import UserExtra
from .pictorial import PictorialFollow from .pictorial import PictorialFollow
from .topic import Topic
class User(models.Model): class User(models.Model):
class Meta: class Meta:
...@@ -35,9 +37,10 @@ class User(models.Model): ...@@ -35,9 +37,10 @@ class User(models.Model):
update_time = models.DateTimeField(verbose_name=u'更新时间', default=datetime.datetime.fromtimestamp(0)) update_time = models.DateTimeField(verbose_name=u'更新时间', default=datetime.datetime.fromtimestamp(0))
@classmethod @classmethod
def get_user_nick_name(cls,user_id): def get_user_nick_name(cls, user_id):
try: try:
nick_name = User.objects.using(settings.SLAVE_DB_NAME).filter(user_id=user_id).values_list("nick_name").first() nick_name = User.objects.using(settings.SLAVE_DB_NAME).filter(user_id=user_id).values_list(
"nick_name").first()
return nick_name[0] return nick_name[0]
except: except:
...@@ -47,7 +50,8 @@ class User(models.Model): ...@@ -47,7 +50,8 @@ class User(models.Model):
def get_is_recommend_flag(self): def get_is_recommend_flag(self):
is_shadow = False is_shadow = False
is_recommend = False is_recommend = False
query_sql = UserExtra.objects.using(settings.SLAVE_DB_NAME).filter(user_id=self.user_id, is_deleted=False, is_online=True) query_sql = UserExtra.objects.using(settings.SLAVE_DB_NAME).filter(user_id=self.user_id, is_deleted=False,
is_online=True)
for record in query_sql: for record in query_sql:
is_recommend = record.is_recommend is_recommend = record.is_recommend
is_shadow = record.is_shadow is_shadow = record.is_shadow
...@@ -58,7 +62,8 @@ class User(models.Model): ...@@ -58,7 +62,8 @@ class User(models.Model):
latest_topic_time_val = -1 latest_topic_time_val = -1
# 获取该用户最新发帖时间 # 获取该用户最新发帖时间
topic_records = Topic.objects.using(settings.SLAVE_DB_NAME).filter(user_id=self.user_id).order_by("-update_time").values_list("update_time", topic_records = Topic.objects.using(settings.SLAVE_DB_NAME).filter(user_id=self.user_id).order_by(
"-update_time").values_list("update_time",
flat=True).first() flat=True).first()
if topic_records: if topic_records:
tzlc_topic_update_time = tzlc(topic_records) tzlc_topic_update_time = tzlc(topic_records)
...@@ -75,7 +80,8 @@ class User(models.Model): ...@@ -75,7 +80,8 @@ class User(models.Model):
logging.info("get follow_user_id_list :%s" % follow_user_id_list) logging.info("get follow_user_id_list :%s" % follow_user_id_list)
for i in range(0, len(follow_user_id_list), 1000): for i in range(0, len(follow_user_id_list), 1000):
logging.info("get follow_user_id_list :%s" % follow_user_id_list[i:i + 1000]) logging.info("get follow_user_id_list :%s" % follow_user_id_list[i:i + 1000])
sql_data_list = User.objects.using(settings.SLAVE_DB_NAME).filter(user_id__in=follow_user_id_list[i:i + 1000]) sql_data_list = User.objects.using(settings.SLAVE_DB_NAME).filter(
user_id__in=follow_user_id_list[i:i + 1000])
for detail_data in sql_data_list: for detail_data in sql_data_list:
item = { item = {
"user_id": detail_data.user_id, "user_id": detail_data.user_id,
...@@ -88,7 +94,8 @@ class User(models.Model): ...@@ -88,7 +94,8 @@ class User(models.Model):
def get_attention_group_id_list(self): def get_attention_group_id_list(self):
try: try:
attention_group_id_list = list() attention_group_id_list = list()
query_results = GroupUserRole.objects.using(settings.SLAVE_DB_NAME).filter(is_online=True, user_id=self.user_id) query_results = GroupUserRole.objects.using(settings.SLAVE_DB_NAME).filter(is_online=True,
user_id=self.user_id)
for item in query_results: for item in query_results:
item_dict = { item_dict = {
"group_id": item.group_id, "group_id": item.group_id,
...@@ -104,15 +111,16 @@ class User(models.Model): ...@@ -104,15 +111,16 @@ class User(models.Model):
def get_attention_pictorial_id_list(self): def get_attention_pictorial_id_list(self):
try: try:
attention_pictorial_id_list = list() attention_pictorial_id_list = list()
query_results = PictorialFollow.objects.using(settings.SLAVE_DB_NAME).filter(is_online=True, user_id=self.user_id) query_results = PictorialFollow.objects.using(settings.SLAVE_DB_NAME).filter(is_online=True,
logging.info("get PictorialFollow:%s"%query_results) user_id=self.user_id)
logging.info("get PictorialFollow:%s" % query_results)
for item in query_results: for item in query_results:
item_dict = { item_dict = {
"pictorial_id": item.pictorial_id, "pictorial_id": item.pictorial_id,
"update_time_val": time.mktime(tzlc(item.update_time).timetuple()) "update_time_val": time.mktime(tzlc(item.update_time).timetuple())
} }
attention_pictorial_id_list.append(item_dict) attention_pictorial_id_list.append(item_dict)
logging.info("get user_id:%s" %self.user_id) logging.info("get user_id:%s" % self.user_id)
logging.info("get same_pictorial_user_id_list:%s" % attention_pictorial_id_list) logging.info("get same_pictorial_user_id_list:%s" % attention_pictorial_id_list)
...@@ -149,25 +157,29 @@ class User(models.Model): ...@@ -149,25 +157,29 @@ class User(models.Model):
def get_same_pictorial_user_id_list(self): def get_same_pictorial_user_id_list(self):
#todo 有的同组数据过大,导致celery cpu过高,暂时限制同组的数据大小,后续可能会去掉同组的数据 # todo 有的同组数据过大,导致celery cpu过高,暂时限制同组的数据大小,后续可能会去掉同组的数据
same_pictorial_user_id_list = list() same_pictorial_user_id_list = list()
pictorial_items_list = list(PictorialFollow.objects.using(settings.SLAVE_DB_NAME).filter(user_id=self.user_id).values_list("pictorial_id",flat=True)) pictorial_items_list = list(
PictorialFollow.objects.using(settings.SLAVE_DB_NAME).filter(user_id=self.user_id).values_list(
"pictorial_id", flat=True))
for pictorial_id in pictorial_items_list: for pictorial_id in pictorial_items_list:
user_items_list = list(PictorialFollow.objects.using(settings.SLAVE_DB_NAME).filter(pictorial_id=pictorial_id).values_list("user_id",flat=True)) user_items_list = list(
PictorialFollow.objects.using(settings.SLAVE_DB_NAME).filter(pictorial_id=pictorial_id).values_list(
"user_id", flat=True))
for user_id in user_items_list: for user_id in user_items_list:
same_pictorial_user_id_list.append(user_id) same_pictorial_user_id_list.append(user_id)
if len(same_pictorial_user_id_list)>=100: if len(same_pictorial_user_id_list) >= 100:
break break
if len(same_pictorial_user_id_list)>=100: if len(same_pictorial_user_id_list) >= 100:
break break
logging.info("get same user_id:%s"%self.user_id) logging.info("get same user_id:%s" % self.user_id)
logging.info("get same_pictorial_user_id_list:%s"%same_pictorial_user_id_list) logging.info("get same_pictorial_user_id_list:%s" % same_pictorial_user_id_list)
same_pictorial_detail_list = list() same_pictorial_detail_list = list()
for i in range(0, len(same_pictorial_user_id_list), 200): for i in range(0, len(same_pictorial_user_id_list), 200):
sql_data_list = User.objects.using(settings.SLAVE_DB_NAME).filter(user_id__in=same_pictorial_user_id_list[i:i + 1000]) sql_data_list = User.objects.using(settings.SLAVE_DB_NAME).filter(
user_id__in=same_pictorial_user_id_list[i:i + 1000])
for detail_data in sql_data_list: for detail_data in sql_data_list:
item = { item = {
"user_id": detail_data.user_id, "user_id": detail_data.user_id,
...@@ -181,7 +193,8 @@ class User(models.Model): ...@@ -181,7 +193,8 @@ class User(models.Model):
try: try:
user_tag_id_list = list() user_tag_id_list = list()
query_results = AccountUserTag.objects.using(settings.SLAVE_DB_NAME).filter(user=self.user_id, is_deleted=False) query_results = AccountUserTag.objects.using(settings.SLAVE_DB_NAME).filter(user=self.user_id,
is_deleted=False)
for item in query_results: for item in query_results:
user_tag_id_list.append(item.tag_id) user_tag_id_list.append(item.tag_id)
...@@ -194,7 +207,9 @@ class User(models.Model): ...@@ -194,7 +207,9 @@ class User(models.Model):
try: try:
user_useful_tag_id_list = list() user_useful_tag_id_list = list()
query_results = AccountUserTag.objects.using(settings.SLAVE_DB_NAME).filter(user=self.user_id,is_body_esthetics =1, is_deleted=False) query_results = AccountUserTag.objects.using(settings.SLAVE_DB_NAME).filter(user=self.user_id,
is_body_esthetics=1,
is_deleted=False)
for item in query_results: for item in query_results:
user_useful_tag_id_list.append(item.tag_id) user_useful_tag_id_list.append(item.tag_id)
...@@ -202,3 +217,11 @@ class User(models.Model): ...@@ -202,3 +217,11 @@ class User(models.Model):
except: except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc()) logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return [] return []
def get_topic_number(self):
try:
number = Topic.objects.filter(user_id=self.user_id).count()
return number
except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return 0
...@@ -14,7 +14,7 @@ import sys ...@@ -14,7 +14,7 @@ import sys
from libs.cache import redis_client from libs.cache import redis_client
import copy import copy
from trans2es.models import topic, user, pick_celebrity, group, celebrity, tag, contrast_similar,pictorial from trans2es.models import topic, user, pick_celebrity, group, celebrity, tag, contrast_similar, pictorial
from trans2es.utils.user_transfer import UserTransfer from trans2es.utils.user_transfer import UserTransfer
from trans2es.utils.pick_celebrity_transfer import PickCelebrityTransfer from trans2es.utils.pick_celebrity_transfer import PickCelebrityTransfer
from trans2es.utils.group_transfer import GroupTransfer from trans2es.utils.group_transfer import GroupTransfer
...@@ -196,7 +196,7 @@ class TypeInfo(object): ...@@ -196,7 +196,7 @@ class TypeInfo(object):
old_data["is_history"] = True old_data["is_history"] = True
data_list.append(old_data) data_list.append(old_data)
if int_ori_topic_star>=4: if int_ori_topic_star >= 4:
topic_data_high_star_list.append(old_data) topic_data_high_star_list.append(old_data)
redis_client.hset(self.physical_topic_star, data["id"], data["content_level"]) redis_client.hset(self.physical_topic_star, data["id"], data["content_level"])
# data_list = [ # data_list = [
...@@ -207,12 +207,11 @@ class TypeInfo(object): ...@@ -207,12 +207,11 @@ class TypeInfo(object):
# ] # ]
# ESPerform.es_helpers_bulk(ESPerform.get_cli(), data_list, "topic-star") # ESPerform.es_helpers_bulk(ESPerform.get_cli(), data_list, "topic-star")
if data["content_level"] and int(data["content_level"]) >= 4:
if data["content_level"] and int(data["content_level"])>=4:
topic_data_high_star_list.append(data) topic_data_high_star_list.append(data)
data_list.append(data) data_list.append(data)
return (data_list,topic_data_high_star_list) return (data_list, topic_data_high_star_list)
def elasticsearch_bulk_insert_data(self, sub_index_name, data_list, es=None): def elasticsearch_bulk_insert_data(self, sub_index_name, data_list, es=None):
...@@ -255,17 +254,17 @@ class TypeInfo(object): ...@@ -255,17 +254,17 @@ class TypeInfo(object):
else: else:
qs = self.model.objects.all() qs = self.model.objects.all()
end = time.time() end = time.time()
time0=end-begin time0 = end - begin
begin = time.time() begin = time.time()
instance_list = qs.filter(pk__in=pk_list) instance_list = qs.filter(pk__in=pk_list)
end = time.time() end = time.time()
time1=end-begin time1 = end - begin
begin = time.time() begin = time.time()
data_list, topic_data_high_star_list = self.bulk_get_data(instance_list) data_list, topic_data_high_star_list = self.bulk_get_data(instance_list)
end = time.time() end = time.time()
time2=end-begin time2 = end - begin
begin = time.time() begin = time.time()
# logging.info("get sub_index_name:%s"%sub_index_name) # logging.info("get sub_index_name:%s"%sub_index_name)
...@@ -277,7 +276,7 @@ class TypeInfo(object): ...@@ -277,7 +276,7 @@ class TypeInfo(object):
es=es, es=es,
) )
if sub_index_name=="topic": if sub_index_name == "topic":
self.elasticsearch_bulk_insert_data( self.elasticsearch_bulk_insert_data(
sub_index_name="topic-star-routing", sub_index_name="topic-star-routing",
data_list=data_list, data_list=data_list,
...@@ -285,7 +284,7 @@ class TypeInfo(object): ...@@ -285,7 +284,7 @@ class TypeInfo(object):
) )
# 同时写4星及以上的帖子 # 同时写4星及以上的帖子
if len(topic_data_high_star_list)>0: if len(topic_data_high_star_list) > 0:
self.elasticsearch_bulk_insert_data( self.elasticsearch_bulk_insert_data(
sub_index_name="topic-high-star", sub_index_name="topic-high-star",
data_list=topic_data_high_star_list, data_list=topic_data_high_star_list,
...@@ -293,9 +292,8 @@ class TypeInfo(object): ...@@ -293,9 +292,8 @@ class TypeInfo(object):
) )
end = time.time() end = time.time()
time3=end-begin time3 = end - begin
logging.info("duan add,insert_table_by_pk_list time cost:%ds,%ds,%ds,%ds" % (time0,time1,time2,time3)) logging.info("duan add,insert_table_by_pk_list time cost:%ds,%ds,%ds,%ds" % (time0, time1, time2, time3))
def insert_table_chunk(self, sub_index_name, table_chunk, es=None): def insert_table_chunk(self, sub_index_name, table_chunk, es=None):
try: try:
...@@ -317,7 +315,7 @@ class TypeInfo(object): ...@@ -317,7 +315,7 @@ class TypeInfo(object):
auto_create_index=True auto_create_index=True
) )
logging.info("es_helpers_bulk,sub_index_name:%s,data_list len:%d" % (sub_index_name,len(data_list))) logging.info("es_helpers_bulk,sub_index_name:%s,data_list len:%d" % (sub_index_name, len(data_list)))
stage_3_time = time.time() stage_3_time = time.time()
end_clock = time.clock() end_clock = time.clock()
...@@ -355,8 +353,8 @@ def get_type_info_map(): ...@@ -355,8 +353,8 @@ def get_type_info_map():
name='topic-star', name='topic-star',
type='topic-star', type='topic-star',
model=topic.Topic, model=topic.Topic,
query_deferred=lambda: topic.Topic.objects.all().query,#假的 query_deferred=lambda: topic.Topic.objects.all().query, # 假的
get_data_func=TopicTransfer.get_topic_data,#假的 get_data_func=TopicTransfer.get_topic_data, # 假的
bulk_insert_chunk_size=100, bulk_insert_chunk_size=100,
round_insert_chunk_size=5, round_insert_chunk_size=5,
round_insert_period=2, round_insert_period=2,
...@@ -525,4 +523,3 @@ def get_type_info_map(): ...@@ -525,4 +523,3 @@ 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
...@@ -7,26 +7,27 @@ import traceback ...@@ -7,26 +7,27 @@ import traceback
from libs.tools import tzlc from libs.tools import tzlc
from trans2es.models.topic import Topic from trans2es.models.topic import Topic
class PictorialTransfer(object): class PictorialTransfer(object):
def __init__(self): def __init__(self):
pass pass
@classmethod @classmethod
def get_offline_score(cls,instance,topic_id_list): def get_offline_score(cls, instance, topic_id_list):
try: try:
total_offline_score = 0 total_offline_score = 0
topic_image_num = 0 topic_image_num = 0
for topic_id in topic_id_list: for topic_id in topic_id_list:
topic_image_num += Topic.get_topic_image_num(topic_id) topic_image_num += Topic.get_topic_image_num(topic_id)
if topic_image_num>=6 and topic_image_num<=10: if topic_image_num >= 6 and topic_image_num <= 10:
total_offline_score += 30 total_offline_score += 30
elif topic_image_num>10 and topic_image_num<=20: elif topic_image_num > 10 and topic_image_num <= 20:
total_offline_score += 60 total_offline_score += 60
elif topic_image_num>20 and topic_image_num<=50: elif topic_image_num > 20 and topic_image_num <= 50:
total_offline_score += 80 total_offline_score += 80
elif topic_image_num>50: elif topic_image_num > 50:
total_offline_score += 100 total_offline_score += 100
total_offline_score += instance.add_score total_offline_score += instance.add_score
...@@ -62,10 +63,11 @@ class PictorialTransfer(object): ...@@ -62,10 +63,11 @@ class PictorialTransfer(object):
tag_id_list = instance.get_tag_by_id() tag_id_list = instance.get_tag_by_id()
res["tag_id"] = tag_id_list res["tag_id"] = tag_id_list
res["tag_name"] = instance.get_tag_by_name(tag_id_list) res["tag_name"] = instance.get_tag_by_name(tag_id_list)
res["topic_id_list"] =instance.get_topic_id() res["topic_id_list"] = instance.get_topic_id()
res["effective"] = instance.get_effective(res["topic_id_list"]) res["effective"] = instance.get_effective(res["topic_id_list"])
res["offline_score"] = cls.get_offline_score(instance,res["topic_id_list"]) res["offline_score"] = cls.get_offline_score(instance, res["topic_id_list"])
res["is_default"] = instance.is_default res["is_default"] = instance.is_default
res["is_cover"] = instance.get_is_cover(res["topic_id_list"])
return res return res
except: except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc()) logging.error("catch exception,err_msg:%s" % traceback.format_exc())
......
...@@ -8,16 +8,15 @@ import logging ...@@ -8,16 +8,15 @@ import logging
import traceback import traceback
from libs.tools import tzlc from libs.tools import tzlc
from trans2es.models.topic import Topic from trans2es.models.topic import Topic
from trans2es.models.tag import TopicTag,CommunityTagType,CommunityTagTypeRelation from trans2es.models.tag import TopicTag, CommunityTagType, CommunityTagTypeRelation
import datetime import datetime
from django.conf import settings from django.conf import settings
class TagTransfer(object): class TagTransfer(object):
@classmethod @classmethod
def get_tag_name_data(cls,instance): def get_tag_name_data(cls, instance):
try: try:
res = dict() res = dict()
res["name"] = instance.name res["name"] = instance.name
...@@ -28,7 +27,7 @@ class TagTransfer(object): ...@@ -28,7 +27,7 @@ class TagTransfer(object):
return dict() return dict()
@classmethod @classmethod
def get_tag_data(cls,instance): def get_tag_data(cls, instance):
try: try:
res = dict() res = dict()
...@@ -36,14 +35,14 @@ class TagTransfer(object): ...@@ -36,14 +35,14 @@ class TagTransfer(object):
tag_name_terms_list = list() tag_name_terms_list = list()
for i in range(len(instance.name)): for i in range(len(instance.name)):
for j in range(i,len(instance.name)+1): for j in range(i, len(instance.name) + 1):
name_term = instance.name[i:j].strip() name_term = instance.name[i:j].strip()
if name_term: if name_term:
tag_name_terms_list.append(name_term.lower()) tag_name_terms_list.append(name_term.lower())
res["suggest"] = { res["suggest"] = {
"input":tag_name_terms_list, "input": tag_name_terms_list,
"contexts":{ "contexts": {
"is_online": [instance.is_online], "is_online": [instance.is_online],
"is_deleted": [instance.is_deleted] "is_deleted": [instance.is_deleted]
} }
...@@ -54,24 +53,27 @@ class TagTransfer(object): ...@@ -54,24 +53,27 @@ class TagTransfer(object):
topic_num = 0 topic_num = 0
res["near_new_topic_num"] = topic_num res["near_new_topic_num"] = topic_num
if instance.is_online==True and instance.is_deleted==False: if instance.is_online == True and instance.is_deleted == False:
topic_id_list = list() topic_id_list = list()
sql_result_results = list(TopicTag.objects.using(settings.SLAVE_DB_NAME).filter( sql_result_results = list(TopicTag.objects.using(settings.SLAVE_DB_NAME).filter(
tag_id=instance.id).values_list("topic_id", "is_online")) tag_id=instance.id).values_list("topic_id", "is_online"))
for topic_id,is_online in sql_result_results: for topic_id, is_online in sql_result_results:
if is_online: if is_online:
topic_id_list.append(topic_id) topic_id_list.append(topic_id)
time_base_val = datetime.datetime.strftime(datetime.datetime.now()+datetime.timedelta(-7), "%Y-%m-%d") time_base_val = datetime.datetime.strftime(datetime.datetime.now() + datetime.timedelta(-7), "%Y-%m-%d")
for topic_begin_index in range(0,len(topic_id_list),100): for topic_begin_index in range(0, len(topic_id_list), 100):
cur_topic_num = Topic.objects.using(settings.SLAVE_DB_NAME).filter(id__in=topic_id_list[topic_begin_index:topic_begin_index+100],create_time__gte=time_base_val).count() cur_topic_num = Topic.objects.using(settings.SLAVE_DB_NAME).filter(
id__in=topic_id_list[topic_begin_index:topic_begin_index + 100],
create_time__gte=time_base_val).count()
topic_num += cur_topic_num topic_num += cur_topic_num
res["near_new_topic_num"] = topic_num res["near_new_topic_num"] = topic_num
tag_type_sql_list = CommunityTagTypeRelation.objects.using(settings.SLAVE_DB_NAME).filter(tag_id=instance.id).values_list("tag_type_id",flat=True) tag_type_sql_list = CommunityTagTypeRelation.objects.using(settings.SLAVE_DB_NAME).filter(
tag_id=instance.id).values_list("tag_type_id", flat=True)
tag_type_list = list() tag_type_list = list()
for tag_type_id in tag_type_sql_list: for tag_type_id in tag_type_sql_list:
tag_type_list.append(tag_type_id) tag_type_list.append(tag_type_id)
......
...@@ -21,7 +21,8 @@ class UserTransfer(object): ...@@ -21,7 +21,8 @@ class UserTransfer(object):
follow_user_detail_list = list() follow_user_detail_list = list()
for i in range(0, len(follow_user_id_list), 1000): for i in range(0, len(follow_user_id_list), 1000):
sql_data_list = User.objects.using(settings.SLAVE_DB_NAME).filter(user_id__in=follow_user_id_list[i:i + 1000], is_online=True, sql_data_list = User.objects.using(settings.SLAVE_DB_NAME).filter(
user_id__in=follow_user_id_list[i:i + 1000], is_online=True,
is_deleted=False) is_deleted=False)
for detail_data in sql_data_list: for detail_data in sql_data_list:
item = { item = {
...@@ -40,13 +41,14 @@ class UserTransfer(object): ...@@ -40,13 +41,14 @@ class UserTransfer(object):
res["id"] = instance.id res["id"] = instance.id
res["user_id"] = instance.user_id res["user_id"] = instance.user_id
res["nick_name"] = instance.nick_name res["nick_name"] = instance.nick_name
res["nick_name_pre"] = instance.nick_name
res["profile_pic"] = instance.profile_pic res["profile_pic"] = instance.profile_pic
res["gender"] = instance.gender res["gender"] = instance.gender
res["city_id"] = instance.city_id res["city_id"] = instance.city_id
res["country_id"] = instance.country_id res["country_id"] = instance.country_id
res["is_online"] = instance.is_online res["is_online"] = instance.is_online
res["is_deleted"] = instance.is_deleted res["is_deleted"] = instance.is_deleted
res["count_topic"] = instance.get_topic_number()
try: try:
(is_recommend, is_shadow) = instance.get_is_recommend_flag() (is_recommend, is_shadow) = instance.get_is_recommend_flag()
res["is_recommend"] = is_recommend res["is_recommend"] = is_recommend
...@@ -95,7 +97,6 @@ class UserTransfer(object): ...@@ -95,7 +97,6 @@ class UserTransfer(object):
res["attention_pictorial_id_list"] = [] res["attention_pictorial_id_list"] = []
res["same_pictorial_user_id_list"] = [] res["same_pictorial_user_id_list"] = []
return res return res
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