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

add batch_recommend_user

parent 0affd82d
......@@ -10,7 +10,7 @@ from libs.es import ESPerform
class UserUtils(object):
@classmethod
def get_batch_attention_user_dict(cls,user_id_list,self_user_id):
def get_batch_attention_user_list(cls,user_id_list,self_user_id):
"""
:remark 批量用户 关注的 用户列表
:param user_id_list:
......@@ -32,17 +32,25 @@ class UserUtils(object):
result_dict = ESPerform.get_search_results(ESPerform.get_cli(), "user", q, offset=0, size=len(user_id_list))
attention_user_dict = dict()
self_attention_user_id_list = []
attention_user_dict_list = list()
for hit_item in result_dict["hits"]:
attention_user_dict = dict()
user_id = hit_item["_source"]["user_id"]
attention_user_id_list = [item["user_id"] for item in hit_item["_source"]["attention_user_id_list"]]
attention_user_dict[user_id] = attention_user_id_list
if user_id == self_user_id:
self_attention_user_id_list = [item["user_id"] for item in hit_item["_source"]["attention_user_id_list"]]
else:
attention_user_id_list = [item["user_id"] for item in hit_item["_source"]["attention_user_id_list"]]
attention_user_dict[user_id] = attention_user_id_list
attention_user_dict_list.append(attention_user_dict)
return attention_user_dict
return [self_attention_user_id_list,attention_user_dict_list]
except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return dict()
return ([],[])
@classmethod
......@@ -170,7 +178,7 @@ class UserUtils(object):
@classmethod
def get_batch_recommend_user_dict(cls,need_filter_attention_user_id_list,attention_user_dict,self_user_id,offset,size):
def get_batch_recommend_user_dict(cls,need_filter_attention_user_id_list,attention_user_dict_list,self_user_id,offset,size):
"""
:remark 获取批量推荐用户
:param need_filter_attention_user_id_list:
......@@ -182,66 +190,68 @@ class UserUtils(object):
"""
try:
batch_query_list = list()
for interesting_user_id in attention_user_dict:
if interesting_user_id != self_user_id:
filter_user_id_list = need_filter_attention_user_id_list
filter_user_id_list.append(interesting_user_id)
q = dict()
q["from"] = offset
q["size"] = size
q["query"] = dict()
for interesting_user_item_dict in attention_user_dict_list:
for interesting_user_id in interesting_user_item_dict:
if interesting_user_id != self_user_id:
logging.info("duan add,interesting_user_id:%d" % interesting_user_id)
filter_user_id_list = need_filter_attention_user_id_list
filter_user_id_list.append(interesting_user_id)
functions_list = [
{
"gauss": {
"latest_topic_time_val": {
"origin": int(time.time()),
"scale": "600",
"decay": 0.1
}
}
}
]
q = dict()
q["from"] = offset
q["size"] = size
q["query"] = dict()
if len(attention_user_dict[interesting_user_id]) > 0:
functions_list.append(
functions_list = [
{
"filter":{
"bool":{
"should":{"terms":{"user_id":attention_user_dict[interesting_user_id]}}
"gauss": {
"latest_topic_time_val": {
"origin": int(time.time()),
"scale": "600",
"decay": 0.1
}
},
"weight":10
}
}
)
]
query_function_score = {
"query": {
"bool": {
"must": [
{"term": {"is_recommend": True}},
{"term": {"is_online": True}},
{"term": {"is_deleted": False}},
{"term": {"is_shadow": False}}
],
"must_not":{
"terms":{
"user_id":filter_user_id_list
if len(interesting_user_item_dict[interesting_user_id]) > 0:
functions_list.append(
{
"filter":{
"bool":{
"should":{"terms":{"user_id":interesting_user_item_dict[interesting_user_id]}}
}
},
"weight":10
}
)
query_function_score = {
"query": {
"bool": {
"must": [
{"term": {"is_recommend": True}},
{"term": {"is_online": True}},
{"term": {"is_deleted": False}},
{"term": {"is_shadow": False}}
],
"must_not":{
"terms":{
"user_id":filter_user_id_list
}
}
}
}
},
"score_mode": "sum",
"boost_mode": "sum",
"functions": functions_list
}
q["query"]["function_score"] = query_function_score
q["_source"] = {
"include":["user_id"]
}
},
"score_mode": "sum",
"boost_mode": "sum",
"functions": functions_list
}
q["query"]["function_score"] = query_function_score
q["_source"] = {
"include":["user_id"]
}
batch_query_list.append(q)
batch_query_list.append(q)
index_name = ESPerform.get_official_index_name("user","read")
search_header_dict = {'index': index_name, 'type': "_doc"}
......
......@@ -55,15 +55,14 @@ def batch_recommend_user(self_user_id,interesting_user_id_list,offset=0,size=10)
self_user_id = -1
#获取关注用户列表
attention_user_dict = UserUtils.get_batch_attention_user_dict(interesting_user_id_list,self_user_id)
(need_filter_attention_user_id_list, attention_user_dict_list) = UserUtils.get_batch_attention_user_list(interesting_user_id_list,self_user_id)
#去除自身及感兴趣的用户ID
need_filter_attention_user_id_list = attention_user_dict[self_user_id] if self_user_id in attention_user_dict else list()
need_filter_attention_user_id_list.append(self_user_id)
recommend_user_list = UserUtils.get_batch_recommend_user_dict(need_filter_attention_user_id_list=need_filter_attention_user_id_list,attention_user_dict=attention_user_dict,
recommend_user_list = UserUtils.get_batch_recommend_user_dict(need_filter_attention_user_id_list=need_filter_attention_user_id_list,attention_user_dict_list=attention_user_dict_list,
self_user_id=self_user_id,offset=offset,size=size)
......
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