Commit 87920e61 authored by lixiaofang's avatar lixiaofang

add

parent 779336c0
...@@ -120,9 +120,11 @@ class TopicUtils(object): ...@@ -120,9 +120,11 @@ class TopicUtils(object):
return {} return {}
@classmethod @classmethod
def get_recommend_topic_ids(cls,user_id,tag_id,offset,size,single_size,query=None,query_type=TopicPageType.FIND_PAGE, def get_recommend_topic_ids(cls, user_id, tag_id, offset, size, single_size, query=None,
filter_topic_id_list=[],test_score=False,must_topic_id_list=[],recommend_tag_list=[], query_type=TopicPageType.FIND_PAGE,
user_similar_score_list=[],index_type="topic",routing=None,attention_tag_list=[],linucb_user_id_list = [],disable_collpase=False): 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):
""" """
:remark:获取首页推荐帖子列表 :remark:获取首页推荐帖子列表
:param user_id: :param user_id:
...@@ -158,14 +160,14 @@ class TopicUtils(object): ...@@ -158,14 +160,14 @@ class TopicUtils(object):
q["query"] = dict() q["query"] = dict()
functions_list = [ functions_list = [
# { {
# "filter": { "filter": {
# "term": { "term": {
# "language_type": 1 "language_type": 1
# } }
# }, },
# "weight": 60 "weight": 60
# }, },
{ {
"gauss": { "gauss": {
"create_time": { "create_time": {
...@@ -175,18 +177,18 @@ class TopicUtils(object): ...@@ -175,18 +177,18 @@ class TopicUtils(object):
}, },
"weight": 60 "weight": 60
}, },
# { {
# "filter": { "filter": {
# "constant_score":{ "constant_score": {
# "filter":{ "filter": {
# "term": { "term": {
# "content_level": 6 "content_level": 6
# } }
# } }
# } }
# }, },
# "weight": 600 "weight": 600
# } }
] ]
# if len(user_similar_score_list) > 0: # if len(user_similar_score_list) > 0:
...@@ -216,7 +218,25 @@ class TopicUtils(object): ...@@ -216,7 +218,25 @@ class TopicUtils(object):
"weight": 100 "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_function_score = {
"query": { "query": {
"bool": { "bool": {
...@@ -242,10 +262,7 @@ class TopicUtils(object): ...@@ -242,10 +262,7 @@ class TopicUtils(object):
} }
} }
], ],
"minimum_should_match": 1, "minimum_should_match": 1
"must_not": [
{"term": {"is_history":True}}
]
} }
}, },
"score_mode": "sum", "score_mode": "sum",
...@@ -260,18 +277,14 @@ class TopicUtils(object): ...@@ -260,18 +277,14 @@ class TopicUtils(object):
} }
if len(filter_topic_id_list) > 0: if len(filter_topic_id_list) > 0:
query_function_score["query"]["bool"]["must_not"] = [ query_function_score["query"]["bool"]["must_not"] = [
{"terms":{"id":filter_topic_id_list}} {"terms": {"id": filter_topic_id_list}}
] ]
if current_topic_id != -1:
if len(linucb_user_id_list)>0: query_function_score["query"]["bool"]["must_not"] = [{
if "must_not" in query_function_score["query"]["bool"]: "term": {
query_function_score["query"]["bool"]["must_not"] += [ "id": current_topic_id
{"terms": {"user_id": linucb_user_id_list}} }
] }]
else:
query_function_score["query"]["bool"]["must_not"] = [
{"terms": {"user_id": linucb_user_id_list}}
]
if query is not None: # 搜索帖子 if query is not None: # 搜索帖子
multi_fields = { multi_fields = {
'description': 200, 'description': 200,
...@@ -289,8 +302,7 @@ class TopicUtils(object): ...@@ -289,8 +302,7 @@ class TopicUtils(object):
query_function_score["query"]["bool"]["should"] = [ query_function_score["query"]["bool"]["should"] = [
{'multi_match': multi_match}, {'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"]["minimum_should_match"] = 1
else: else:
...@@ -303,10 +315,9 @@ class TopicUtils(object): ...@@ -303,10 +315,9 @@ class TopicUtils(object):
{"term": {"is_operation_home_recommend": True}} {"term": {"is_operation_home_recommend": True}}
] ]
q["query"]["function_score"] = query_function_score q["query"]["function_score"] = query_function_score
if not disable_collpase: q["collapse"] = {
q["collapse"] = { "field": "user_id"
"field": "user_id" }
}
# "includes": ["id", "pictorial_id", "offline_score", "user_id", "edit_tag_list"] # "includes": ["id", "pictorial_id", "offline_score", "user_id", "edit_tag_list"]
q["_source"] = { q["_source"] = {
"includes": ["id"] "includes": ["id"]
...@@ -325,18 +336,18 @@ class TopicUtils(object): ...@@ -325,18 +336,18 @@ class TopicUtils(object):
# } # }
# }, # },
{ {
"offline_score": { "_score": {
"order": "desc" "order": "desc"
} }
}, },
{ {
"_score": { "offline_score": {
"order": "desc" "order": "desc"
} }
} }
] ]
result_dict = ESPerform.get_search_results(ESPerform.get_cli(), sub_index_name=index_type, query_body=q, 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() topic_id_list = list()
...@@ -349,7 +360,8 @@ class TopicUtils(object): ...@@ -349,7 +360,8 @@ class TopicUtils(object):
@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,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 帖子详情页推荐列表,缺少按时间衰减 :remark 帖子详情页推荐列表,缺少按时间衰减
:param user_id: :param user_id:
...@@ -426,7 +438,7 @@ class TopicUtils(object): ...@@ -426,7 +438,7 @@ class TopicUtils(object):
} }
result_dict = ESPerform.get_search_results(es_cli_obj, sub_index_name=index_type, query_body=q, 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"] return result_dict["hits"]
except: except:
...@@ -434,9 +446,10 @@ class TopicUtils(object): ...@@ -434,9 +446,10 @@ class TopicUtils(object):
return [] return []
@classmethod @classmethod
def top_get_topic_detail_recommend_list(cls, user_id, topic_id,have_read_topic_list, size, es_cli_obj=None, 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 = [], index_type="topic", routing=None, collection_topic_tag_list=[],
topic_user_id =-1): topic_tag_list=[],
topic_user_id=-1):
""" """
:remark 帖子详情页推荐列表,缺少按时间衰减 :remark 帖子详情页推荐列表,缺少按时间衰减
:param user_id: :param user_id:
...@@ -480,26 +493,26 @@ class TopicUtils(object): ...@@ -480,26 +493,26 @@ class TopicUtils(object):
# "weight": 5000 # "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_function_score = {
"query": { "query": {
"bool": { "bool": {
"must": [ "must": [
{"range": {"content_level": {"gte": 3, "lte": 6}}}, {"range": {"content_level": {"gte": 3, "lte": 6}}},
{"term": {"is_online": True}}, {"term": {"is_online": True}},
{"term": {"is_deleted": False}} {"term": {"is_deleted": False}}
], ],
"must_not": { "must_not": {
"terms": { "terms": {
"id": have_read_topic_list "id": have_read_topic_list
}
} }
} }
} },
}, "score_mode": "sum",
"score_mode": "sum", "boost_mode": "sum",
"boost_mode": "sum", "functions": functions_list
"functions": functions_list }
}
else: else:
query_function_score = { query_function_score = {
"query": { "query": {
...@@ -535,9 +548,9 @@ class TopicUtils(object): ...@@ -535,9 +548,9 @@ class TopicUtils(object):
} }
q["query"]["function_score"] = query_function_score q["query"]["function_score"] = query_function_score
if topic_user_id == -1: if topic_user_id == -1:
q["collapse"] = { q["collapse"] = {
"field": "user_id" "field": "user_id"
} }
q["_source"] = { q["_source"] = {
"includes": ["id", "pictorial_id", "user_id", "_score"] "includes": ["id", "pictorial_id", "user_id", "_score"]
} }
...@@ -548,7 +561,8 @@ class TopicUtils(object): ...@@ -548,7 +561,8 @@ class TopicUtils(object):
# {"create_time": {"order": "desc"}} # {"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() topic_id_list = list()
for item in result_dict["hits"]: for item in result_dict["hits"]:
...@@ -691,7 +705,7 @@ class TopicUtils(object): ...@@ -691,7 +705,7 @@ class TopicUtils(object):
{"term": {"is_deleted": False}}, {"term": {"is_deleted": False}},
] ]
logging.info("get filters:%s"%filters) logging.info("get filters:%s" % filters)
if not filters: if not filters:
return f return f
...@@ -797,7 +811,10 @@ class TopicUtils(object): ...@@ -797,7 +811,10 @@ class TopicUtils(object):
return nf return nf
for k, v in nfilters.items(): for k, v in nfilters.items():
pass if k == "is_his":
nf.append({
"term": {k: v},
})
return nf return nf
...@@ -894,13 +911,14 @@ class TopicUtils(object): ...@@ -894,13 +911,14 @@ class TopicUtils(object):
} }
@classmethod @classmethod
def business_topic_ids(cls, filters, nfilters, sorts_by, offset=0, size=10, index_name="topic", filter_online=True): def business_topic_id(cls, filters, nfilters, sorts_by, offset=0, size=10, index_name="topic", filter_online=True):
must = cls.process_filters(filters, filter_online=filter_online) must = cls.process_filters(filters, filter_online=filter_online)
query = '' query = ''
for k, v in filters.items(): for k, v in filters.items():
if k == "content": if k == "content":
query = filters[k] query = filters[k]
q = {} q = {}
q["query"] = { q["query"] = {
"function_score": { "function_score": {
...@@ -915,7 +933,7 @@ class TopicUtils(object): ...@@ -915,7 +933,7 @@ class TopicUtils(object):
}], }],
"query": { "query": {
"multi_match": { "multi_match": {
"fields":["content"], "fields": ["content"],
"type": "cross_fields", "type": "cross_fields",
"operator": "and", "operator": "and",
"query": query "query": query
...@@ -923,15 +941,53 @@ class TopicUtils(object): ...@@ -923,15 +941,53 @@ class TopicUtils(object):
} }
} }
} }
} }
if query == '': if query == '':
q["query"] = { q["query"] = {
"bool": { "bool": {
"must": must, "must": must,
"must_not": cls.process_nfilters(nfilters), "must_not": cls.process_nfilters(nfilters),
} }
}
if sorts_by:
sorts = cls.process_sort(sorts_by)
if sorts:
q["sort"] = sorts
try:
result_dict = ESPerform.get_search_results(
ESPerform.get_cli(), sub_index_name=index_name,
query_body=q, offset=offset, size=size
)
return {
"hits": result_dict["hits"],
"total_count": result_dict["total_count"]
}
except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return {
"hits": [],
"total_count": 0
} }
@classmethod
def business_topic_ids(cls, filters, nfilters, sorts_by, offset=0, size=10, index_name="topic", filter_online=True):
must = cls.process_filters(filters, filter_online=filter_online)
q = {
"query": {
"bool": {
"must": must,
"must_not": cls.process_nfilters(nfilters),
}
}
}
if sorts_by: if sorts_by:
sorts = cls.process_sort(sorts_by) sorts = cls.process_sort(sorts_by)
if sorts: if sorts:
......
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