Commit 5aa663c1 authored by 黄凯's avatar 黄凯

Merge branch 'hk_now' into 'test'

add useful tag recall

See merge request alpha/physical!339
parents 4893144a 1f10cff4
...@@ -333,6 +333,74 @@ class TopicUtils(object): ...@@ -333,6 +333,74 @@ class TopicUtils(object):
logging.error("catch exception,err_msg:%s" % traceback.format_exc()) logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return list() return list()
@classmethod
def userful_tag_topic_list(cls, user_id, have_read_topic_list, size,
index_type="topic-high-star",routing=None):
"""
:remark 帖子详情页推荐列表,缺少按时间衰减
:param user_id:
:param topic_tag_list:
:param topic_group_id:
:param topic_user_id:
:param offset:
:param size:
:return:
"""
try:
es_cli_obj = ESPerform.get_cli()
useful_tag_list = list()
q = dict()
q["query"] = {
"term": {
"user_id": user_id
}
}
q["_source"] = {
"include": ["useful_tag_list"]
}
result_dict = ESPerform.get_search_results(es_cli_obj, "user", q, 0, 1)
if len(result_dict["hits"]) == 0:
logging.warning("not find user_id:%d in es!" % int(user_id))
else:
useful_tag_list = result_dict["hits"][0]["_source"]["useful_tag_list"]
if len(useful_tag_list) == 0:
return []
else:
q = dict()
q["query"] = dict()
# logging.warning("topic_tag_list:%s"%str(topic_tag_list))
query_function_score = {
"query": {
"bool": {
"must": [
{"term": {"is_online": True}},
{"term": {"is_deleted": False}},
{"terms": {"tag_list": useful_tag_list}}
],
"must_not": {
"terms": {
"id": have_read_topic_list
}
}
}
}
}
q["_source"] = {
"includes": ["id"]
}
result_dict = ESPerform.get_search_results(es_cli_obj, sub_index_name=index_type, query_body=q, size=size,
routing=routing)
topic_id_list = list()
for item in result_dict["hits"]:
topic_id_list.append(item["_source"]["id"])
return topic_id_list
except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return []
@classmethod @classmethod
def get_topic_detail_recommend_list(cls, user_id, topic_id, topic_tag_list, topic_pictorial_id, topic_user_id, def get_topic_detail_recommend_list(cls, 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=None, filter_topic_user_id, have_read_topic_list, offset, size, es_cli_obj=None,
......
...@@ -46,7 +46,7 @@ def get_discover_page_topic_ids(user_id, device_id, size, query_type=TopicPageTy ...@@ -46,7 +46,7 @@ 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): query_type=TopicPageType.HOME_RECOMMEND,promote_topic_list = [],disable_collpase=False,usefulrecall = -1):
try: try:
topic_star_routing = "6" topic_star_routing = "6"
index_type = "topic-high-star" index_type = "topic-high-star"
...@@ -76,13 +76,18 @@ def get_home_recommend_topic_ids(user_id, device_id, tag_id, offset, size, query ...@@ -76,13 +76,18 @@ def get_home_recommend_topic_ids(user_id, device_id, tag_id, offset, size, query
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]))
have_read_topic_id_list.extend(promote_topic_list)
useful_topic_id_list = list()
#有用标签召回
if usefulrecall != -1:
useful_topic_id_list = TopicUtils.userful_tag_topic_list(user_id,have_read_topic_id_list,4,"topic-high-star","6")
# 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(
# redis_user_similar_score_redis_val) if redis_user_similar_score_redis_val else [] # redis_user_similar_score_redis_val) if redis_user_similar_score_redis_val else []
size = size - len(useful_topic_id_list)
have_read_topic_id_list = have_read_topic_id_list.extend(useful_topic_id_list)
recommend_topic_user_list = list() recommend_topic_user_list = list()
attention_tag_list = list() attention_tag_list = list()
recommend_topic_list = list() recommend_topic_list = list()
if query is None: if query is None:
...@@ -121,7 +126,6 @@ def get_home_recommend_topic_ids(user_id, device_id, tag_id, offset, size, query ...@@ -121,7 +126,6 @@ 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)
have_read_topic_id_list.extend(promote_topic_list)
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,
...@@ -154,7 +158,7 @@ def get_home_recommend_topic_ids(user_id, device_id, tag_id, offset, size, query ...@@ -154,7 +158,7 @@ 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)
return recommend_topic_list,rank_topic_id_list return recommend_topic_list,rank_topic_id_list,useful_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 [],[]
...@@ -170,7 +174,7 @@ def query_tag_id_by_topic(offset=0, size=10, topic_id_list=[], user_id=-1): ...@@ -170,7 +174,7 @@ 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=[]): def home_recommend(device_id="", user_id=-1, offset=0, size=10, query_type=TopicPageType.HOME_RECOMMEND,promote_topic_list=[],usefulrecall = -1):
""" """
:remark:首页推荐,目前只推荐日记 :remark:首页推荐,目前只推荐日记
:param session_id: :param session_id:
...@@ -187,6 +191,7 @@ def home_recommend(device_id="", user_id=-1, offset=0, size=10, query_type=Topic ...@@ -187,6 +191,7 @@ def home_recommend(device_id="", user_id=-1, offset=0, size=10, query_type=Topic
recommend_topic_ids = list() recommend_topic_ids = list()
rank_topic_ids = list() rank_topic_ids = list()
useful_topic_ids = list()
es_node_load_high_flag = False es_node_load_high_flag = False
# try: # try:
# es_node_load_high_flag = ESPerform.if_es_node_load_high(ESPerform.get_cli()) # es_node_load_high_flag = ESPerform.if_es_node_load_high(ESPerform.get_cli())
...@@ -207,13 +212,18 @@ def home_recommend(device_id="", user_id=-1, offset=0, size=10, query_type=Topic ...@@ -207,13 +212,18 @@ def home_recommend(device_id="", user_id=-1, offset=0, size=10, query_type=Topic
recommend_topic_ids = have_read_topic_id_list[0:size] recommend_topic_ids = have_read_topic_id_list[0:size]
else: else:
recommend_topic_ids,rank_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, offset=0, size=size,
query_type=query_type,promote_topic_list=promote_topic_list) query_type=query_type,promote_topic_list=promote_topic_list,usefulrecall=usefulrecall)
if usefulrecall != -1:
return {"linucb_topic_ids": recommend_topic_ids,"rank_topic_ids":rank_topic_ids,"useful_topic_ids":useful_topic_ids}
else:
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}
except: except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc()) logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return {"linucb_topic_ids": [],"rank_topic_ids":[]} if usefulrecall != -1:
return {"linucb_topic_ids": [],"rank_topic_ids":[],"useful_topic_ids":[]}
else:
return {"linucb_topic_ids": [], "rank_topic_ids": []}
@bind("physical/search/discover_page") @bind("physical/search/discover_page")
......
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