Commit ba9eca6c authored by lixiaofang's avatar lixiaofang

Merge branch 'test' of git.wanmeizhensuo.com:alpha/physical into test

parents 32e5a81a 94907185
......@@ -220,7 +220,7 @@ class TopicUtils(object):
{
"filter": {"bool": {
"should": {"term": {"user_id": topic_user_id}}}},
"weight": 100000
"weight": 5000
}
)
query_function_score = {
......@@ -425,6 +425,105 @@ class TopicUtils(object):
logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return []
@classmethod
def top_get_topic_detail_recommend_list(cls, user_id, topic_id,have_read_topic_list, size, es_cli_obj=None,
index_type="topic", routing=None,collection_topic_tag_list = [],topic_tag_list = [],
topic_user_id =-1):
"""
:remark 帖子详情页推荐列表,缺少按时间衰减
:param user_id:
:param topic_tag_list:
:param topic_group_id:
:param topic_user_id:
:param offset:
:param size:
:return:
"""
try:
if not es_cli_obj:
es_cli_obj = ESPerform.get_cli()
q = dict()
q["query"] = dict()
functions_list = [
{
"linear": {
"create_time": {
"scale": "1d",
"decay": 0.5
}
}
}
]
if len(topic_tag_list) != 0 or user_id!= -1:
query_function_score = {
"query": {
"bool": {
"must": [
{"range": {"content_level": {"gte": 3, "lte": 6}}},
{"term": {"is_online": True}},
{"term": {"is_deleted": False}}
],
"must_not": {
"terms": {
"id": have_read_topic_list
}
}
}
},
"score_mode": "sum",
"boost_mode": "sum",
"functions": functions_list
}
else:
query_function_score = {
"query": {
"bool": {
"must": [
{"range": {"content_level": {"gte": 4, "lte": 6}}},
{"term": {"is_online": True}},
{"term": {"is_deleted": False}}
],
"must_not": {
"terms": {
"id": have_read_topic_list
}
}
}
},
"score_mode": "sum",
"boost_mode": "sum",
"functions": functions_list
}
if len(topic_tag_list) > 0:
query_function_score["query"]["bool"]["should"] = {
"terms": {
"tag_list": topic_tag_list
}
}
if topic_user_id != -1:
query_function_score["query"]["bool"]["should"] = {
"term": {
"user_id": topic_user_id
}
}
q["query"]["function_score"] = query_function_score
q["_source"] = {
"includes": ["id", "pictorial_id", "user_id", "_score"]
}
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
def get_topic_tag_id_list(cls, topic_id, es_cli_obj=None):
"""
......
......@@ -265,7 +265,56 @@ def topic_detail_page_recommend(device_id="", user_id=-1, topic_id=-1, topic_pic
try:
if not isinstance(user_id, int):
user_id = -1
if user_id > 0:
redis_key = "physical:topic_detail_recommend" + ":user_id:" + str(user_id) + "topic_id:"+str(topic_id)
else:
redis_key = "physical:topic_detail_recommend" + ":device_id:" + device_id + "topic_id:"+str(topic_id)
if offset == 0:
have_read_topic_list = list()
# redis_dict = {
# "have_read_topic_id": json.dumps(have_read_topic_list)
# }
redis_client.delete(redis_key)
# redis_client.expire(redis_key, 60 * 60 * 24)
else:
have_read_topic_list = 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_list = json.loads(have_read_topic_redis_data[0]) if have_read_topic_redis_data[0] else []
es_cli_obj = ESPerform.get_cli()
have_read_topic_list.append(topic_id)
topic_user_result = list()
topic_tag_result = list()
result = list()
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,
index_type="topic",routing="4,5,6",topic_tag_list = topic_tag_list)
topic_tag_size = len(topic_tag_result)
have_read_topic_list.extend(topic_tag_result)
else:
topic_tag_size = 0
if topic_tag_size <size:
size = size - topic_tag_size
if topic_user_id != -1:
topic_user_result = TopicUtils.top_get_topic_detail_recommend_list(user_id, topic_id, have_read_topic_list,
size, es_cli_obj,
index_type="topic", routing="4,5,6",
topic_tag_list=topic_tag_list)
topic_user_size = len(topic_user_result)
have_read_topic_list.extend(topic_user_result)
if topic_user_size < size:
size = size - topic_user_size
result = TopicUtils.top_get_topic_detail_recommend_list(user_id, topic_id,
have_read_topic_list,
size, es_cli_obj,
index_type="topic", routing="4,5,6")
have_read_topic_list.extend(result)
# 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 []
#
# 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_list = json.loads(have_read_topic_redis_data) if have_read_topic_redis_data else []
......@@ -279,21 +328,24 @@ def topic_detail_page_recommend(device_id="", user_id=-1, topic_id=-1, topic_pic
# 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,6")
result_list = TopicUtils.get_recommend_topic_ids(user_id, tag_id=-1, single_size=size, offset=offset, size=size,
topic_user_id=topic_user_id, current_topic_id=topic_id,
topic_tag_list=topic_tag_list)
recommend_topic_ids_list = list()
# if len(result_list) > 0:
# recommend_topic_ids_list = [item["_source"]["id"] for item in result_list]
recommend_topic_ids_list.extend(result_list)
#
# have_read_topic_list.extend(recommend_topic_ids_list)
# result_list = TopicUtils.get_recommend_topic_ids(user_id, tag_id=-1, single_size=size, offset=offset, size=size,
# topic_user_id=topic_user_id, current_topic_id=topic_id,
# topic_tag_list=topic_tag_list)
#
# have_read_topic_len = len(have_read_topic_list)
# if 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))
# user_recommend_list = TopicUtils.get_recommend_topic_ids(user_id, tag_id=-1, single_size=size, offset=offset, size=size,
# topic_user_id=topic_user_id, current_topic_id=topic_id,
# topic_tag_list=topic_tag_list)
redis_dict = {
"have_read_topic_list": json.dumps(have_read_topic_list),
}
redis_client.hmset(redis_key, redis_dict)
redis_client.expire(redis_key, 60 * 60 * 24)
result_list = []
recommend_topic_ids_list = list()
recommend_topic_ids_list.extend(topic_tag_result)
recommend_topic_ids_list.extend(topic_user_result)
recommend_topic_ids_list.extend(result)
return {"recommend_topic_ids": recommend_topic_ids_list}
except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc())
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment