Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
P
physical
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
alpha
physical
Commits
0b8524b3
Commit
0b8524b3
authored
Mar 15, 2019
by
段英荣
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
modify
parent
3f56fc67
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
74 additions
and
56 deletions
+74
-56
topic.py
search/utils/topic.py
+32
-12
topic.py
search/views/topic.py
+42
-44
No files found.
search/utils/topic.py
View file @
0b8524b3
...
...
@@ -121,7 +121,9 @@ class TopicUtils(object):
return
{}
@classmethod
def
get_recommend_topic_ids
(
cls
,
user_id
,
tag_id
,
offset
,
size
,
query
=
None
,
query_type
=
TopicPageType
.
FIND_PAGE
,
filter_topic_id_list
=
[],
test_score
=
False
,
must_topic_id_list
=
[],
recommend_tag_list
=
[],
user_similar_score_list
=
[]):
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
=
[],
user_similar_score_list
=
[]):
"""
:需增加打散逻辑
:remark:获取首页推荐帖子列表
...
...
@@ -324,22 +326,40 @@ class TopicUtils(object):
result_dict
=
ESPerform
.
get_search_results
(
ESPerform
.
get_cli
(),
sub_index_name
=
"topic"
,
query_body
=
q
,
offset
=
offset
,
size
=
size
)
if
not
test_score
:
topic_id_dict
=
dict
()
for
item
in
result_dict
[
"hits"
]:
topic_id_dict
[
item
[
"_source"
][
"id"
]]
=
[
item
[
"_source"
][
"group_id"
],
item
[
"_source"
][
"user_id"
]]
topic_id_list
=
list
()
same_group_id_set
=
set
()
same_user_id_set
=
set
()
return
topic_id_dict
else
:
topic_id_dict
=
dict
()
for
item
in
result_dict
[
"hits"
]:
topic_id_dict
[
item
[
"_source"
][
"id"
]]
=
[
item
[
"_source"
][
"group_id"
],
item
[
"_source"
][
"user_id"
],
item
[
"_score"
]]
return
topic_id_dict
if
item
[
"_source"
][
"group_id"
]
>
0
and
item
[
"_source"
][
"group_id"
]
not
in
same_group_id_set
:
same_group_id_set
.
add
(
item
[
"_source"
][
"id"
])
topic_id_list
.
append
(
item
[
"_source"
][
"id"
])
else
:
same_group_id_set
.
add
(
item
[
"_source"
][
"id"
])
if
item
[
"_source"
][
"user_id"
]
not
in
same_user_id_set
:
same_user_id_set
.
add
(
item
[
"_source"
][
"id"
])
topic_id_list
.
append
(
item
[
"_source"
][
"id"
])
else
:
same_user_id_set
.
add
(
item
[
"_source"
][
"id"
])
if
len
(
topic_id_list
)
>=
single_size
:
break
if
len
(
topic_id_list
)
<
single_size
:
for
topic_id
in
same_group_id_set
:
topic_id_list
.
append
(
topic_id
)
if
len
(
topic_id_list
)
>=
single_size
:
break
for
topic_id
in
same_user_id_set
:
topic_id_list
.
append
(
topic_id
)
if
len
(
topic_id_list
)
>=
single_size
:
break
return
topic_id_list
except
:
logging
.
error
(
"catch exception,err_msg:
%
s"
%
traceback
.
format_exc
())
return
dic
t
()
return
lis
t
()
@classmethod
def
get_topic_detail_recommend_list
(
cls
,
user_id
,
topic_id
,
topic_tag_list
,
topic_group_id
,
topic_user_id
,
filter_topic_user_id
,
have_read_topic_list
,
offset
,
size
,
es_cli_obj
=
None
):
...
...
search/views/topic.py
View file @
0b8524b3
...
...
@@ -25,13 +25,10 @@ def get_discover_page_topic_ids(user_id, device_id, size, query_type=TopicPageTy
have_read_topic_id_list
=
json
.
loads
(
redis_field_val_list
[
0
])
if
redis_field_val_list
[
0
]
else
[]
recommend_topic_ids
=
[]
topic_id_dict
=
TopicUtils
.
get_recommend_topic_ids
(
user_id
=
user_id
,
tag_id
=
0
,
offset
=
0
,
size
=
size
,
recommend_topic_ids
=
TopicUtils
.
get_recommend_topic_ids
(
user_id
=
user_id
,
tag_id
=
0
,
offset
=
0
,
size
=
size
,
query_type
=
query_type
,
filter_topic_id_list
=
have_read_topic_id_list
)
for
topic_id
in
topic_id_dict
:
recommend_topic_ids
.
append
(
topic_id
)
have_read_topic_id_list
.
extend
(
recommend_topic_ids
)
redis_dict
=
{
...
...
@@ -83,7 +80,8 @@ def get_home_recommend_topic_ids(user_id, device_id, tag_id, offset, size, query
user_similar_score_redis_list
=
json
.
loads
(
redis_user_similar_score_redis_val
)
if
redis_user_similar_score_redis_val
else
[]
topic_id_dict
=
TopicUtils
.
get_recommend_topic_ids
(
user_id
,
tag_id
,
offset
,
size
*
size
,
query
,
query_type
,
topic_id_list
=
TopicUtils
.
get_recommend_topic_ids
(
user_id
=
user_id
,
tag_id
=
tag_id
,
offset
=
offset
,
size
=
size
*
size
,
single_size
=
size
,
query
=
query
,
query_type
=
query_type
,
filter_topic_id_list
=
have_read_topic_id_list
,
recommend_tag_list
=
recommend_tag_list
,
user_similar_score_list
=
user_similar_score_redis_list
)
...
...
@@ -91,42 +89,43 @@ def get_home_recommend_topic_ids(user_id, device_id, tag_id, offset, size, query
have_read_user_id_set
=
set
()
unread_topic_id_dict
=
dict
()
# 当前页小组数量
cur_page_group_num
=
0
# 当前页用户数量
cur_page_user_num
=
0
for
topic_id
in
topic_id_dict
:
if
topic_id_dict
[
topic_id
][
0
]
in
have_read_group_id_set
or
topic_id_dict
[
topic_id
][
1
]
in
have_read_user_id_set
:
unread_topic_id_dict
[
topic_id
]
=
topic_id_dict
[
topic_id
]
else
:
if
isinstance
(
topic_id_dict
[
topic_id
][
0
],
int
)
and
topic_id_dict
[
topic_id
][
0
]
>
0
and
cur_page_group_num
<
(
size
*
0.9
):
have_read_group_id_set
.
add
(
topic_id_dict
[
topic_id
][
0
])
have_read_user_id_set
.
add
(
topic_id_dict
[
topic_id
][
1
])
have_read_topic_id_list
.
append
(
topic_id
)
cur_page_group_num
+=
1
recommend_topic_ids
.
append
(
topic_id
)
elif
topic_id_dict
[
topic_id
]
and
cur_page_user_num
<
(
size
*
0.1
):
have_read_user_id_set
.
add
(
topic_id_dict
[
topic_id
][
1
])
cur_page_user_num
+=
1
recommend_topic_ids
.
append
(
topic_id
)
have_read_topic_id_list
.
append
(
topic_id
)
else
:
unread_topic_id_dict
[
topic_id
]
=
topic_id_dict
[
topic_id
]
if
len
(
recommend_topic_ids
)
>=
size
:
break
if
len
(
recommend_topic_ids
)
<
size
and
len
(
unread_topic_id_dict
)
>
0
:
for
unread_topic_id
in
unread_topic_id_dict
:
if
len
(
recommend_topic_ids
)
<
size
:
recommend_topic_ids
.
append
(
unread_topic_id
)
have_read_topic_id_list
.
append
(
unread_topic_id
)
else
:
break
# # 当前页小组数量
# cur_page_group_num = 0
# # 当前页用户数量
# cur_page_user_num = 0
#
# for topic_id in topic_id_dict:
# if topic_id_dict[topic_id][0] in have_read_group_id_set or topic_id_dict[topic_id][
# 1] in have_read_user_id_set:
# unread_topic_id_dict[topic_id] = topic_id_dict[topic_id]
# else:
# if isinstance(topic_id_dict[topic_id][0], int) and topic_id_dict[topic_id][
# 0] > 0 and cur_page_group_num < (size * 0.9):
# have_read_group_id_set.add(topic_id_dict[topic_id][0])
# have_read_user_id_set.add(topic_id_dict[topic_id][1])
# have_read_topic_id_list.append(topic_id)
# cur_page_group_num += 1
# recommend_topic_ids.append(topic_id)
# elif topic_id_dict[topic_id] and cur_page_user_num < (size * 0.1):
# have_read_user_id_set.add(topic_id_dict[topic_id][1])
# cur_page_user_num += 1
# recommend_topic_ids.append(topic_id)
# have_read_topic_id_list.append(topic_id)
# else:
# unread_topic_id_dict[topic_id] = topic_id_dict[topic_id]
#
# if len(recommend_topic_ids) >= size:
# break
# if len(recommend_topic_ids) < size and len(unread_topic_id_dict) > 0:
# for unread_topic_id in unread_topic_id_dict:
# if len(recommend_topic_ids) < size:
# recommend_topic_ids.append(unread_topic_id)
# have_read_topic_id_list.append(unread_topic_id)
# else:
# break
have_read_topic_id_list
.
extend
(
topic_id_list
)
if
len
(
have_read_topic_id_list
)
>
5000
:
cut_len
=
len
(
have_read_topic_id_list
)
-
5000
have_read_topic_id_list
=
have_read_topic_id_list
[
cut_len
:]
...
...
@@ -321,11 +320,10 @@ def query_topic_by_user_similarity(topic_similarity_score_dict, offset=0, size=1
"""
try
:
must_topic_id_list
=
list
(
topic_similarity_score_dict
.
keys
())
topic_id_
dic
t
=
TopicUtils
.
get_recommend_topic_ids
(
tag_id
=
0
,
user_id
=-
1
,
offset
=
offset
,
size
=
size
,
topic_id_
lis
t
=
TopicUtils
.
get_recommend_topic_ids
(
tag_id
=
0
,
user_id
=-
1
,
offset
=
offset
,
size
=
size
,
must_topic_id_list
=
must_topic_id_list
)
recommend_topic_ids
=
list
(
topic_id_dict
.
keys
())
return
{
"recommend_topic_ids"
:
recommend_topic_ids
}
return
{
"recommend_topic_ids"
:
topic_id_list
}
except
:
logging
.
error
(
"catch exception,err_msg:
%
s"
%
traceback
.
format_exc
())
return
{
"recommend_topic_id"
:
[]}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment