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
397a968c
Commit
397a968c
authored
May 14, 2019
by
lixiaofang
Browse files
Options
Browse Files
Download
Plain Diff
update
parents
2c519908
42514adb
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
133 additions
and
169 deletions
+133
-169
es.py
libs/es.py
+17
-17
topic.py
search/utils/topic.py
+75
-98
topic.py
search/views/topic.py
+41
-54
No files found.
libs/es.py
View file @
397a968c
...
...
@@ -378,27 +378,27 @@ class ESPerform(object):
"weight"
:
1
}
)
functions_list
+=
[
{
"filter"
:
{
"term"
:
{
"content_level"
:
6
}},
"weight"
:
6000
},
{
"filter"
:
{
"term"
:
{
"content_level"
:
5
}},
"weight"
:
5000
},
{
"filter"
:
{
"term"
:
{
"content_level"
:
4
}},
"weight"
:
4000
}
]
#
functions_list += [
#
{
#
"filter": {"term": {"content_level": 6}},
#
"weight": 6000
#
},
#
{
#
"filter": {"term": {"content_level": 5}},
#
"weight": 5000
#
},
#
{
#
"filter": {"term": {"content_level": 4}},
#
"weight": 4000
#
}
#
]
q
=
{
"query"
:
{
"function_score"
:
{
"query"
:
{
"bool"
:
{
"must"
:
[
{
"
range"
:
{
"content_level"
:
{
"gte"
:
4
,
"lte"
:
6
}
}},
{
"
term"
:
{
"content_level"
:
6
}},
{
"term"
:
{
"is_online"
:
True
}},
{
"term"
:
{
"is_deleted"
:
False
}},
{
"terms"
:
{
"tag_list"
:
tag_id
}}
...
...
@@ -425,8 +425,8 @@ class ESPerform(object):
"id"
:
have_read_topic_id_list
}
}
result_dict
=
ESPerform
.
get_search_results
(
ESPerform
.
get_cli
(),
sub_index_name
=
"topic"
,
query_body
=
q
,
offset
=
0
,
size
=
size
,
routing
=
"
4,5,
6"
)
result_dict
=
ESPerform
.
get_search_results
(
ESPerform
.
get_cli
(),
sub_index_name
=
"topic
-high-star
"
,
query_body
=
q
,
offset
=
0
,
size
=
size
,
routing
=
"6"
)
topic_id_list
=
[
item
[
"_source"
][
"id"
]
for
item
in
result_dict
[
"hits"
]]
# logging.info("topic_id_list:%s" % str(topic_id_list))
...
...
search/utils/topic.py
View file @
397a968c
...
...
@@ -12,7 +12,6 @@ from .common import TopicDocumentField
from
search.utils.common
import
*
from
trans2es.models.pictorial
import
PictorialTopics
class
TopicUtils
(
object
):
@classmethod
...
...
@@ -121,11 +120,9 @@ class TopicUtils(object):
return
{}
@classmethod
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
=
[],
index_type
=
"topic"
,
routing
=
None
,
attention_tag_list
=
[],
current_topic_id
=-
1
,
topic_tag_list
=
[],
topic_user_id
=-
1
):
def
get_recommend_topic_ids
(
cls
,
user_id
,
tag_id
,
offset
,
size
,
single_size
,
query
=
None
,
query_type
=
TopicPageType
.
HOME_RECOMMEND
,
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
=
[],
linucb_user_id_list
=
[],
disable_collpase
=
False
):
"""
:remark:获取首页推荐帖子列表
:param user_id:
...
...
@@ -161,14 +158,14 @@ class TopicUtils(object):
q
[
"query"
]
=
dict
()
functions_list
=
[
{
"filter"
:
{
"term"
:
{
"language_type"
:
1
}
},
"weight"
:
60
},
#
{
#
"filter": {
#
"term": {
#
"language_type": 1
#
}
#
},
#
"weight": 60
#
},
{
"gauss"
:
{
"create_time"
:
{
...
...
@@ -178,18 +175,18 @@ class TopicUtils(object):
},
"weight"
:
60
},
{
"filter"
:
{
"constant_score"
:
{
"filter"
:
{
"term"
:
{
"content_level"
:
6
}
}
}
},
"weight"
:
600
}
#
{
#
"filter": {
# "constant_score":
{
# "filter":
{
#
"term": {
#
"content_level": 6
#
}
#
}
#
}
#
},
#
"weight": 600
#
}
]
# if len(user_similar_score_list) > 0:
...
...
@@ -219,30 +216,12 @@ class TopicUtils(object):
"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"
:
2000
}
)
if
topic_user_id
!=
-
1
:
functions_list
.
append
(
{
"filter"
:
{
"bool"
:
{
"should"
:
{
"term"
:
{
"user_id"
:
topic_user_id
}}}},
"weight"
:
1500
}
)
query_function_score
=
{
"query"
:
{
"bool"
:
{
"filter"
:
[
{
"range"
:
{
"content_level"
:
{
"gte"
:
4
,
"lte"
:
6
}
}},
# {"term": {"content_level": 6
}},
# {"term": {"has_image":True}},
{
"term"
:
{
"is_online"
:
True
}},
{
"term"
:
{
"is_deleted"
:
False
}}
...
...
@@ -263,7 +242,10 @@ class TopicUtils(object):
}
}
],
"minimum_should_match"
:
1
"minimum_should_match"
:
1
,
"must_not"
:
[
{
"term"
:
{
"is_history"
:
True
}}
]
}
},
"score_mode"
:
"sum"
,
...
...
@@ -278,14 +260,18 @@ class TopicUtils(object):
}
if
len
(
filter_topic_id_list
)
>
0
:
query_function_score
[
"query"
][
"bool"
][
"must_not"
]
=
[
{
"terms"
:
{
"id"
:
filter_topic_id_list
}}
{
"terms"
:{
"id"
:
filter_topic_id_list
}}
]
if
len
(
linucb_user_id_list
)
>
0
:
if
"must_not"
in
query_function_score
[
"query"
][
"bool"
]:
query_function_score
[
"query"
][
"bool"
][
"must_not"
]
+=
[
{
"terms"
:
{
"user_id"
:
linucb_user_id_list
}}
]
else
:
query_function_score
[
"query"
][
"bool"
][
"must_not"
]
=
[
{
"terms"
:
{
"user_id"
:
linucb_user_id_list
}}
]
if
current_topic_id
!=
-
1
:
query_function_score
[
"query"
][
"bool"
][
"must_not"
]
=
[{
"term"
:
{
"id"
:
current_topic_id
}
}]
if
query
is
not
None
:
# 搜索帖子
multi_fields
=
{
'description'
:
200
,
...
...
@@ -303,9 +289,13 @@ class TopicUtils(object):
query_function_score
[
"query"
][
"bool"
][
"should"
]
=
[
{
'multi_match'
:
multi_match
},
{
"term"
:
{
"tag_list"
:
tag_id
}}
{
"term"
:
{
"tag_list"
:
tag_id
}},
{
"term"
:
{
"user_nick_name_pre"
:
query
.
lower
()}}
]
query_function_score
[
"query"
][
"bool"
][
"minimum_should_match"
]
=
1
query_function_score
[
"query"
][
"bool"
][
"filter"
]
.
append
(
{
"range"
:
{
"content_level"
:
{
"gte"
:
4
,
"lte"
:
6
}}}
)
else
:
if
"must_not"
in
query_function_score
[
"query"
][
"bool"
]:
query_function_score
[
"query"
][
"bool"
][
"must_not"
]
+=
[
...
...
@@ -315,7 +305,16 @@ class TopicUtils(object):
query_function_score
[
"query"
][
"bool"
][
"must_not"
]
=
[
{
"term"
:
{
"is_operation_home_recommend"
:
True
}}
]
if
query_type
==
TopicPageType
.
FIND_PAGE
:
query_function_score
[
"query"
][
"bool"
][
"filter"
]
.
append
(
{
"range"
:
{
"content_level"
:
{
"gte"
:
4
,
"lte"
:
6
}}}
)
else
:
query_function_score
[
"query"
][
"bool"
][
"filter"
]
.
append
(
{
"term"
:
{
"content_level"
:
6
}}
)
q
[
"query"
][
"function_score"
]
=
query_function_score
if
not
disable_collpase
:
q
[
"collapse"
]
=
{
"field"
:
"user_id"
}
...
...
@@ -337,18 +336,18 @@ class TopicUtils(object):
# }
# },
{
"_score"
:
{
"
offline
_score"
:
{
"order"
:
"desc"
}
},
{
"
offline
_score"
:
{
"_score"
:
{
"order"
:
"desc"
}
}
]
result_dict
=
ESPerform
.
get_search_results
(
ESPerform
.
get_cli
(),
sub_index_name
=
index_type
,
query_body
=
q
,
offset
=
offset
,
size
=
size
,
routing
=
routing
)
offset
=
offset
,
size
=
size
,
routing
=
routing
)
topic_id_list
=
list
()
...
...
@@ -361,8 +360,7 @@ class TopicUtils(object):
@classmethod
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
,
index_type
=
"topic"
,
routing
=
None
):
filter_topic_user_id
,
have_read_topic_list
,
offset
,
size
,
es_cli_obj
=
None
,
index_type
=
"topic"
,
routing
=
None
):
"""
:remark 帖子详情页推荐列表,缺少按时间衰减
:param user_id:
...
...
@@ -409,7 +407,7 @@ class TopicUtils(object):
"query"
:
{
"bool"
:
{
"must"
:
[
{
"range"
:
{
"content_level"
:
{
"gte"
:
4
,
"lte"
:
5
}}},
{
"range"
:
{
"content_level"
:
{
"gte"
:
4
,
"lte"
:
6
}}},
{
"term"
:
{
"is_online"
:
True
}},
{
"term"
:
{
"is_deleted"
:
False
}}
],
...
...
@@ -439,7 +437,7 @@ class TopicUtils(object):
}
result_dict
=
ESPerform
.
get_search_results
(
es_cli_obj
,
sub_index_name
=
index_type
,
query_body
=
q
,
offset
=
offset
,
size
=
size
,
routing
=
routing
)
offset
=
offset
,
size
=
size
,
routing
=
routing
)
return
result_dict
[
"hits"
]
except
:
...
...
@@ -447,10 +445,9 @@ class TopicUtils(object):
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
):
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:
...
...
@@ -494,7 +491,7 @@ class TopicUtils(object):
# "weight": 5000
# }
# )
if
len
(
topic_tag_list
)
!=
0
or
topic_user_id
!=
-
1
:
if
len
(
topic_tag_list
)
!=
0
or
topic_user_id
!=
-
1
:
query_function_score
=
{
"query"
:
{
"bool"
:
{
...
...
@@ -562,8 +559,7 @@ class TopicUtils(object):
# {"create_time": {"order": "desc"}}
# ]
result_dict
=
ESPerform
.
get_search_results
(
es_cli_obj
,
sub_index_name
=
index_type
,
query_body
=
q
,
size
=
size
,
routing
=
routing
)
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"
]:
...
...
@@ -706,7 +702,7 @@ class TopicUtils(object):
{
"term"
:
{
"is_deleted"
:
False
}},
]
logging
.
info
(
"get filters:
%
s"
%
filters
)
logging
.
info
(
"get filters:
%
s"
%
filters
)
if
not
filters
:
return
f
...
...
@@ -718,6 +714,7 @@ class TopicUtils(object):
if
v
in
(
None
,
''
,
[]):
continue
if
k
in
[
"create_time_gte"
,
"create_time_lte"
]:
if
k
==
"create_time_gte"
:
...
...
@@ -811,10 +808,7 @@ class TopicUtils(object):
return
nf
for
k
,
v
in
nfilters
.
items
():
if
k
==
"is_his"
:
nf
.
append
({
"term"
:
{
k
:
v
},
})
pass
return
nf
...
...
@@ -898,26 +892,24 @@ class TopicUtils(object):
query_body
=
q
,
offset
=
offset
,
size
=
size
)
if
len
(
result_dict
[
"hits"
])
>
0
:
if
len
(
result_dict
[
"hits"
])
>
0
:
topic_id_list
=
[
item
[
"_source"
][
"id"
]
for
item
in
result_dict
[
"hits"
]]
return
(
topic_id_list
,
result_dict
[
"total_count"
])
elif
offset
==
0
and
"pictorial_id"
in
filters
:
# 防止帖子同步延迟,画报详情页为空
return
(
topic_id_list
,
result_dict
[
"total_count"
])
elif
offset
==
0
and
"pictorial_id"
in
filters
:
# 防止帖子同步延迟,画报详情页为空
pictorial_id
=
int
(
filters
[
"pictorial_id"
])
topic_id_list
=
list
(
PictorialTopics
.
objects
.
filter
(
pictorial_id
=
pictorial_id
,
is_online
=
True
,
is_deleted
=
False
)
.
values_list
(
"topic_id"
,
flat
=
True
)[
offset
:
size
])
topic_id_list
=
list
(
PictorialTopics
.
objects
.
filter
(
pictorial_id
=
pictorial_id
,
is_online
=
True
,
is_deleted
=
False
)
.
values_list
(
"topic_id"
,
flat
=
True
)[
offset
:
size
])
return
(
topic_id_list
,
len
(
topic_id_list
))
return
(
topic_id_list
,
len
(
topic_id_list
))
else
:
return
([],
0
)
except
:
logging
.
error
(
"catch exception,err_msg:
%
s"
%
traceback
.
format_exc
())
return
([],
0
)
return
([],
0
)
@classmethod
def
business_topic_id
(
cls
,
filters
,
nfilters
,
sorts_by
,
offset
=
0
,
size
=
10
,
index_name
=
"topic"
,
filter_online
=
True
):
def
business_topic_id
s
(
cls
,
filters
,
nfilters
,
sorts_by
,
offset
=
0
,
size
=
10
,
index_name
=
"topic"
,
filter_online
=
True
):
must
=
cls
.
business_filters
(
filters
,
filter_online
=
filter_online
)
q
=
{
...
...
@@ -981,21 +973,6 @@ class TopicUtils(object):
"match"
:
{
k
:
v
}
})
elif
k
in
[
"create_time_gte"
,
"create_time_lte"
]:
if
k
==
"create_time_gte"
:
op
=
"gte"
elif
k
==
"create_time_lte"
:
op
=
"lte"
f
.
append
({
"range"
:
{
"create_time_val"
:
{
op
:
v
,
}
}
})
else
:
if
isinstance
(
v
,
list
):
...
...
search/views/topic.py
View file @
397a968c
...
...
@@ -15,6 +15,7 @@ from libs.es import ESPerform
from
django.conf
import
settings
def
get_discover_page_topic_ids
(
user_id
,
device_id
,
size
,
query_type
=
TopicPageType
.
FIND_PAGE
):
try
:
if
user_id
==
-
1
:
...
...
@@ -27,11 +28,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
=
TopicUtils
.
get_recommend_topic_ids
(
user_id
=
user_id
,
tag_id
=
0
,
offset
=
0
,
size
=
size
,
single_size
=
size
,
recommend_topic_ids
=
TopicUtils
.
get_recommend_topic_ids
(
user_id
=
user_id
,
tag_id
=
0
,
offset
=
0
,
size
=
size
,
single_size
=
size
,
query_type
=
query_type
,
filter_topic_id_list
=
have_read_topic_id_list
,
index_type
=
"topic"
,
routing
=
"4,5,6"
)
filter_topic_id_list
=
have_read_topic_id_list
,
index_type
=
"topic"
,
routing
=
"4,5,6"
)
have_read_topic_id_list
.
extend
(
recommend_topic_ids
)
redis_dict
=
{
...
...
@@ -46,21 +46,21 @@ 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
,
query_type
=
TopicPageType
.
HOME_RECOMMEND
,
promote_topic_list
=
[],
disable_collpase
=
False
):
query_type
=
TopicPageType
.
HOME_RECOMMEND
,
promote_topic_list
=
[],
disable_collpase
=
False
):
try
:
topic_star_routing
=
"6"
if
query
is
None
:
if
user_id
>
0
:
if
user_id
>
0
:
redis_key
=
"physical:home_recommend"
+
":user_id:"
+
str
(
user_id
)
+
":query_type:"
+
str
(
query_type
)
else
:
redis_key
=
"physical:home_recommend"
+
":device_id:"
+
device_id
+
":query_type:"
+
str
(
query_type
)
else
:
if
user_id
>
0
:
redis_key
=
"physical:home_query"
+
":user_id:"
+
str
(
user_id
)
+
":query:"
+
str
(
query
)
+
":query_type:"
+
str
(
query_type
)
topic_star_routing
=
"4,5,6"
if
user_id
>
0
:
redis_key
=
"physical:home_query"
+
":user_id:"
+
str
(
user_id
)
+
":query:"
+
str
(
query
)
+
":query_type:"
+
str
(
query_type
)
else
:
redis_key
=
"physical:home_query"
+
":device_id:"
+
device_id
+
":query:"
+
str
(
query
)
+
":query_type:"
+
str
(
query_type
)
redis_key
=
"physical:home_query"
+
":device_id:"
+
device_id
+
":query:"
+
str
(
query
)
+
":query_type:"
+
str
(
query_type
)
redis_field_list
=
[
b
'have_read_topic_list'
]
redis_field_val_list
=
redis_client
.
hmget
(
redis_key
,
redis_field_list
)
...
...
@@ -72,7 +72,7 @@ def get_home_recommend_topic_ids(user_id, device_id, tag_id, offset, size, query
if
query
is
None
:
have_read_topic_id_list
=
list
(
json
.
loads
(
redis_field_val_list
[
0
]))
else
:
if
offset
>
0
:
# 首次搜索时不需要过滤已读
if
offset
>
0
:
# 首次搜索时不需要过滤已读
have_read_topic_id_list
=
list
(
json
.
loads
(
redis_field_val_list
[
0
]))
# user_similar_score_redis_key = "physical:user_similar_score:user_id:" + str(user_id)
...
...
@@ -91,6 +91,7 @@ def get_home_recommend_topic_ids(user_id, device_id, tag_id, offset, size, query
if
b
"data"
in
recommend_topic_dict
:
recommend_topic_id_list
=
json
.
loads
(
recommend_topic_dict
[
b
"data"
])
# 推荐帖子是强插的,要保证推荐帖子不在已读里
# recommend_topic_id_list = list(set(recommend_topic_id_list) - set(have_read_topic_id_list))
cursor
=
int
(
str
(
recommend_topic_dict
[
b
"cursor"
],
encoding
=
"utf-8"
))
...
...
@@ -107,8 +108,8 @@ def get_home_recommend_topic_ids(user_id, device_id, tag_id, offset, size, query
# 用户关注标签
redis_tag_data
=
redis_client
.
hget
(
"physical:linucb:register_user_tag_info"
,
user_id
)
attention_tag_list
=
json
.
loads
(
redis_tag_data
)
if
redis_tag_data
else
[]
if
len
(
recommend_topic_list
)
>
0
:
size
=
size
-
len
(
recommend_topic_list
)
if
len
(
recommend_topic_list
)
>
0
:
size
=
size
-
len
(
recommend_topic_list
)
have_read_topic_id_list
.
extend
(
recommend_topic_list
)
# have_read_topic_id_list_add_promote = list()
...
...
@@ -121,12 +122,9 @@ def get_home_recommend_topic_ids(user_id, device_id, tag_id, offset, size, query
have_read_topic_id_list
.
extend
(
promote_topic_list
)
topic_id_list
=
list
()
rank_topic_id_list
=
TopicUtils
.
get_recommend_topic_ids
(
user_id
=
user_id
,
tag_id
=
tag_id
,
offset
=
0
,
size
=
size
,
single_size
=
size
,
query
=
query
,
query_type
=
query_type
,
single_size
=
size
,
query
=
query
,
query_type
=
query_type
,
filter_topic_id_list
=
have_read_topic_id_list
,
index_type
=
"topic-high-star"
,
routing
=
"4,5,6"
,
attention_tag_list
=
attention_tag_list
,
linucb_user_id_list
=
recommend_topic_user_list
,
disable_collpase
=
disable_collpase
)
index_type
=
"topic-high-star"
,
routing
=
topic_star_routing
,
attention_tag_list
=
attention_tag_list
,
linucb_user_id_list
=
recommend_topic_user_list
,
disable_collpase
=
disable_collpase
)
# if len(recommend_topic_list) == 6 and query is None:
# if (size < 11):
...
...
@@ -144,7 +142,7 @@ def get_home_recommend_topic_ids(user_id, device_id, tag_id, offset, size, query
have_read_topic_id_list
.
extend
(
rank_topic_id_list
)
if
len
(
have_read_topic_id_list
)
>
30000
:
cut_len
=
len
(
have_read_topic_id_list
)
-
30000
cut_len
=
len
(
have_read_topic_id_list
)
-
30000
have_read_topic_id_list
=
have_read_topic_id_list
[
cut_len
:]
redis_dict
=
{
"have_read_topic_list"
:
json
.
dumps
(
have_read_topic_id_list
),
...
...
@@ -153,10 +151,10 @@ def get_home_recommend_topic_ids(user_id, device_id, tag_id, offset, size, query
# 每个session key保存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
except
:
logging
.
error
(
"catch exception,err_msg:
%
s"
%
traceback
.
format_exc
())
return
[],
[]
return
[],[]
@bind
(
"physical/search/query_tag_id_by_topic"
)
...
...
@@ -169,8 +167,7 @@ def query_tag_id_by_topic(offset=0, size=10, topic_id_list=[], user_id=-1):
@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
=
[]):
"""
:remark:首页推荐,目前只推荐日记
:param session_id:
...
...
@@ -202,20 +199,18 @@ def home_recommend(device_id="", user_id=-1, offset=0, size=10, query_type=Topic
have_read_topic_id_list
=
list
(
json
.
loads
(
redis_field_val_list
[
0
]))
if
len
(
have_read_topic_id_list
)
>
offset
:
recommend_topic_ids
=
have_read_topic_id_list
[
offset
:
offset
+
size
]
recommend_topic_ids
=
have_read_topic_id_list
[
offset
:
offset
+
size
]
else
:
recommend_topic_ids
=
have_read_topic_id_list
[
0
:
size
]
else
:
recommend_topic_ids
,
rank_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
)
recommend_topic_ids
,
rank_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
)
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
:
logging
.
error
(
"catch exception,err_msg:
%
s"
%
traceback
.
format_exc
())
return
{
"linucb_topic_ids"
:
[],
"rank_topic_ids"
:
[]}
return
{
"linucb_topic_ids"
:
[],
"rank_topic_ids"
:
[]}
@bind
(
"physical/search/discover_page"
)
...
...
@@ -259,12 +254,11 @@ def home_query(device_id="", tag_id=-1, user_id=-1, query="", offset=0, size=10)
if
not
isinstance
(
device_id
,
str
):
device_id
=
""
recommend_topic_list
,
rank_topic_id_list
=
get_home_recommend_topic_ids
(
user_id
,
device_id
,
tag_id
,
offset
=
offset
,
size
=
size
,
query
=
query
)
if
len
(
rank_topic_id_list
)
>
0
and
len
(
rank_topic_id_list
)
<
size
:
recommend_topic_list
,
rank_topic_id_list
=
get_home_recommend_topic_ids
(
user_id
,
device_id
,
tag_id
,
offset
=
offset
,
size
=
size
,
query
=
query
)
if
len
(
rank_topic_id_list
)
>
0
and
len
(
rank_topic_id_list
)
<
size
:
recommend_topic_list
,
rank_topic_id_list
=
get_home_recommend_topic_ids
(
user_id
,
device_id
,
tag_id
,
offset
=
offset
,
size
=
size
,
query
=
query
,
disable_collpase
=
True
)
query
=
query
,
disable_collpase
=
True
)
return
{
"recommend_topic_ids"
:
rank_topic_id_list
}
except
:
logging
.
error
(
"catch exception,err_msg:
%
s"
%
traceback
.
format_exc
())
...
...
@@ -286,9 +280,9 @@ def topic_detail_page_recommend(device_id="", user_id=-1, topic_id=-1, topic_pic
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
)
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
)
redis_key
=
"physical:topic_detail_recommend"
+
":device_id:"
+
device_id
+
"topic_id:"
+
str
(
topic_id
)
if
int
(
offset
)
==
0
:
have_read_topic_list
=
list
()
# redis_dict = {
...
...
@@ -301,7 +295,7 @@ def topic_detail_page_recommend(device_id="", user_id=-1, topic_id=-1, topic_pic
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_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
)
...
...
@@ -309,23 +303,18 @@ def topic_detail_page_recommend(device_id="", user_id=-1, topic_id=-1, topic_pic
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
=
"3,4,5,6"
,
topic_tag_list
=
topic_tag_list
)
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
=
"3,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
:
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
,
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
=
"3,4,5,6"
,
topic_user_id
=
topic_user_id
index_type
=
"topic"
,
routing
=
"3,4,5,6"
,
topic_user_id
=
topic_user_id
)
topic_user_size
=
len
(
topic_user_result
)
have_read_topic_list
.
extend
(
topic_user_result
)
...
...
@@ -411,7 +400,7 @@ def topic_search(filters, nfilters=None, sorts_by=None, offset=0, size=10):
"""帖子搜索。"""
try
:
(
topic_id_list
,
total_count
)
=
TopicUtils
.
list_topic_ids
(
filters
=
filters
,
nfilters
=
nfilters
,
(
topic_id_list
,
total_count
)
=
TopicUtils
.
list_topic_ids
(
filters
=
filters
,
nfilters
=
nfilters
,
sorts_by
=
sorts_by
,
offset
=
offset
,
size
=
size
)
return
{
...
...
@@ -435,10 +424,8 @@ 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_list
=
TopicUtils
.
get_recommend_topic_ids
(
tag_id
=
0
,
user_id
=-
1
,
offset
=
offset
,
size
=
size
,
single_size
=
size
,
must_topic_id_list
=
must_topic_id_list
,
index_type
=
"topic"
,
routing
=
"4,5,6"
)
topic_id_list
=
TopicUtils
.
get_recommend_topic_ids
(
tag_id
=
0
,
user_id
=-
1
,
offset
=
offset
,
size
=
size
,
single_size
=
size
,
must_topic_id_list
=
must_topic_id_list
,
index_type
=
"topic"
,
routing
=
"4,5,6"
)
return
{
"recommend_topic_ids"
:
topic_id_list
}
except
:
...
...
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