Commit c02ee3eb authored by Kai's avatar Kai

add topic detail page recommend

parent d54d5d2d
...@@ -122,7 +122,8 @@ class TopicUtils(object): ...@@ -122,7 +122,8 @@ class TopicUtils(object):
@classmethod @classmethod
def get_recommend_topic_ids(cls,user_id,tag_id,offset,size,single_size,query=None,query_type=TopicPageType.FIND_PAGE, def get_recommend_topic_ids(cls,user_id,tag_id,offset,size,single_size,query=None,query_type=TopicPageType.FIND_PAGE,
filter_topic_id_list=[],test_score=False,must_topic_id_list=[],recommend_tag_list=[], filter_topic_id_list=[],test_score=False,must_topic_id_list=[],recommend_tag_list=[],
user_similar_score_list=[],index_type="topic",routing=None,attention_tag_list=[]): user_similar_score_list=[],index_type="topic",routing=None,attention_tag_list=[],
current_topic_id = None,topic_user_id = None,topic_tag_list = []):
""" """
:remark:获取首页推荐帖子列表 :remark:获取首页推荐帖子列表
:param user_id: :param user_id:
...@@ -223,6 +224,26 @@ class TopicUtils(object): ...@@ -223,6 +224,26 @@ class TopicUtils(object):
"weight": 100 "weight": 100
} }
) )
#详情页推荐
if current_topic_id != -1:
if len(topic_tag_list)>0:
functions_list.append(
{
"filter": {"bool": {
"should": {"terms": {"tag_list": topic_tag_list}}}},
"weight": 10000
}
)
if topic_user_id != -1:
functions_list.append(
{
"filter": {"bool": {
"should": {"terms": {"user_id": topic_user_id}}}},
"weight": 5000
}
)
# if len(recommend_tag_list)>0: # if len(recommend_tag_list)>0:
# if len(recommend_tag_list)>1: # if len(recommend_tag_list)>1:
# functions_list += [ # functions_list += [
...@@ -296,7 +317,12 @@ class TopicUtils(object): ...@@ -296,7 +317,12 @@ class TopicUtils(object):
"id": filter_topic_id_list "id": filter_topic_id_list
} }
} }
if current_topic_id!=-1:
query_function_score["query"]["bool"]["must_not"] = {
"term": {
"id": current_topic_id
}
}
if query is not None: # 搜索帖子 if query is not None: # 搜索帖子
multi_fields = { multi_fields = {
'description': 200, 'description': 200,
...@@ -471,6 +497,7 @@ class TopicUtils(object): ...@@ -471,6 +497,7 @@ class TopicUtils(object):
logging.error("catch exception,err_msg:%s" % traceback.format_exc()) logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return [] return []
@classmethod @classmethod
def get_topic_tag_id_list(cls, topic_id, es_cli_obj=None): def get_topic_tag_id_list(cls, topic_id, es_cli_obj=None):
""" """
......
...@@ -251,7 +251,7 @@ def home_query(device_id="", tag_id=-1, user_id=-1, query="", offset=0, size=10) ...@@ -251,7 +251,7 @@ def home_query(device_id="", tag_id=-1, user_id=-1, query="", offset=0, size=10)
@bind("physical/search/topic_detail_page_recommend") @bind("physical/search/topic_detail_page_recommend")
def topic_detail_page_recommend(device_id="", user_id=-1, topic_id=-1, topic_pictorial_id=-1, topic_user_id=-1, def topic_detail_page_recommend(device_id="", user_id=-1, topic_id=-1, topic_pictorial_id=-1, topic_user_id=-1,
filter_topic_user_id=False, offset=0, size=10): filter_topic_user_id=False, offset=0, size=10,topic_tag_list =[]):
""" """
:remark:帖子详情页推荐策略,缺少第一个卡片策略 :remark:帖子详情页推荐策略,缺少第一个卡片策略
:param user_id: :param user_id:
...@@ -264,30 +264,33 @@ def topic_detail_page_recommend(device_id="", user_id=-1, topic_id=-1, topic_pic ...@@ -264,30 +264,33 @@ 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
redis_key = "physical:topic_detail_page_recommend" + ":user_id:" + str(user_id) + ":device_id:" + str(device_id) # redis_key = "physical:topic_detail_page_recommend" + ":user_id:" + str(user_id) + ":device_id:" + str(device_id)
have_read_topic_redis_data = redis_client.get(redis_key) # have_read_topic_redis_data = redis_client.get(redis_key)
have_read_topic_list = json.loads(have_read_topic_redis_data) if have_read_topic_redis_data else [] # have_read_topic_list = json.loads(have_read_topic_redis_data) if have_read_topic_redis_data else []
# 获取es链接对象 # 获取es链接对象
es_cli_obj = ESPerform.get_cli() # es_cli_obj = ESPerform.get_cli()
# have_read_topic_list = list()
# # 获取帖子标签列表
# topic_tag_list = TopicUtils.get_topic_tag_id_list(topic_id, es_cli_obj)
# 获取帖子标签列表 # result_list = TopicUtils.get_topic_detail_recommend_list(user_id, topic_id, topic_tag_list, topic_pictorial_id,
topic_tag_list = TopicUtils.get_topic_tag_id_list(topic_id, es_cli_obj) # topic_user_id, filter_topic_user_id,
# have_read_topic_list, offset, size, es_cli_obj,index_type="topic",routing="4,5")
result_list = TopicUtils.get_recommend_topic_ids(user_id,offset=offset,size=size,topic_user_id=topic_user_id,current_topic_id=topic_id,topic_tag_list=topic_tag_list)
result_list = TopicUtils.get_topic_detail_recommend_list(user_id, topic_id, topic_tag_list, topic_pictorial_id,
topic_user_id, filter_topic_user_id,
have_read_topic_list, offset, size, es_cli_obj,index_type="topic",routing="4,5")
recommend_topic_ids_list = list() recommend_topic_ids_list = list()
if len(result_list) > 0: if len(result_list) > 0:
recommend_topic_ids_list = [item["_source"]["id"] for item in result_list] recommend_topic_ids_list = [item["_source"]["id"] for item in result_list]
#
have_read_topic_list.extend(recommend_topic_ids_list) # have_read_topic_list.extend(recommend_topic_ids_list)
#
have_read_topic_len = len(have_read_topic_list) # have_read_topic_len = len(have_read_topic_list)
if have_read_topic_len > 5000: # if have_read_topic_len > 5000:
have_read_topic_list = have_read_topic_list[(have_read_topic_len - 5000):] # have_read_topic_list = have_read_topic_list[(have_read_topic_len - 5000):]
#
redis_client.set(redis_key, json.dumps(have_read_topic_list)) # redis_client.set(redis_key, json.dumps(have_read_topic_list))
return {"recommend_topic_ids": recommend_topic_ids_list} return {"recommend_topic_ids": recommend_topic_ids_list}
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