Commit 246f4915 authored by 段英荣's avatar 段英荣

modify

parent 231c8371
...@@ -151,7 +151,7 @@ class ESPerform(object): ...@@ -151,7 +151,7 @@ class ESPerform(object):
@classmethod @classmethod
def get_search_results(cls, es_cli,sub_index_name,query_body,offset=0,size=10, def get_search_results(cls, es_cli,sub_index_name,query_body,offset=0,size=10,
auto_create_index=False,doc_type="_doc"): auto_create_index=False,doc_type="_doc",aggregations_query=False):
try: try:
assert (es_cli is not None) assert (es_cli is not None)
...@@ -170,6 +170,8 @@ class ESPerform(object): ...@@ -170,6 +170,8 @@ class ESPerform(object):
"total_count":res["hits"]["total"], "total_count":res["hits"]["total"],
"hits":res["hits"]["hits"] "hits":res["hits"]["hits"]
} }
if aggregations_query:
result_dict["aggregations"] = res["aggregations"]
return result_dict return result_dict
except: except:
......
...@@ -53,7 +53,7 @@ class GroupUtils(object): ...@@ -53,7 +53,7 @@ class GroupUtils(object):
def get_hot_group_recommend_results(cls,offset,size): def get_hot_group_recommend_results(cls,offset,size):
try: try:
q = dict() q = dict()
q["filter"] = { q["query"] = {
"bool":{ "bool":{
"must":{ "must":{
"is_online":True "is_online":True
...@@ -80,7 +80,7 @@ class GroupUtils(object): ...@@ -80,7 +80,7 @@ class GroupUtils(object):
""" """
try: try:
q = dict() q = dict()
q["filter"] = { q["query"] = {
"bool":{ "bool":{
"must":{ "must":{
"is_online":True, "is_online":True,
...@@ -94,9 +94,50 @@ class GroupUtils(object): ...@@ -94,9 +94,50 @@ class GroupUtils(object):
result_dict = ESPerform.get_search_results(ESPerform.get_cli(),"user",q,offset,size) result_dict = ESPerform.get_search_results(ESPerform.get_cli(),"user",q,offset,size)
if len(result_dict["hits"])>0: if len(result_dict["hits"])>0:
return result_dict["hits"][0]["attention_group_id_list"] return result_dict["hits"][0]["_source"]["attention_group_id_list"]
else: else:
return [] return []
except: except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc()) logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return [] return []
@classmethod
def get_group_ids_by_aggs(cls,group_id_list):
"""
:remark:聚合查询获取小组列表
:param group_id_list:
:return:
"""
try:
q = dict()
q["size"]=0
q["query"] = {
"terms":{
"group_id":group_id_list
}
}
q["aggs"] = {
"group_ids":{
"terms":{
"field":"group_id"
},
"aggs":{
"max_date":{
"max":{
"field":"update_time_val"
}
}
}
}
}
result_dict = ESPerform.get_search_results(ESPerform.get_cli(),"topic",q,aggregations_query=True)
buckets_list = result_dict["aggregations"]["buckets"]
sorted_buckets_list = sorted(buckets_list,key=lambda item:item["max_date"]["value"],reverse=True)
sorted_group_id_list = [item["key"] for item in sorted_buckets_list]
return sorted_group_id_list
except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return []
\ No newline at end of file
...@@ -112,7 +112,7 @@ class TopicUtils(object): ...@@ -112,7 +112,7 @@ class TopicUtils(object):
result_dict = TopicUtils.get_related_user_info(user_id, 0, 1) result_dict = TopicUtils.get_related_user_info(user_id, 0, 1)
if len(result_dict["hits"]) == 0: if len(result_dict["hits"]) == 0:
logging.error("not find user_id:%d in es!" % int(user_id)) logging.error("not find user_id:%d in es!" % int(user_id))
return {"recommend_topic_ids": []} return ([],[])
logging.info("duan add,result_dict:%s" % str(result_dict).encode("utf-8")) logging.info("duan add,result_dict:%s" % str(result_dict).encode("utf-8"))
attention_user_info_list = result_dict["hits"][0]["_source"]["attention_user_id_list"] attention_user_info_list = result_dict["hits"][0]["_source"]["attention_user_id_list"]
......
...@@ -49,6 +49,18 @@ def group_sort(user_id=-1,sort_type=GroupSortTypes.HOT_RECOMMEND,offset=0,size=1 ...@@ -49,6 +49,18 @@ def group_sort(user_id=-1,sort_type=GroupSortTypes.HOT_RECOMMEND,offset=0,size=1
attention_group_list = GroupUtils.get_user_attention_group_list(user_id,offset,size) attention_group_list = GroupUtils.get_user_attention_group_list(user_id,offset,size)
if len(attention_group_list)==0: if len(attention_group_list)==0:
return {"group_recommend_ids": []} return {"group_recommend_ids": []}
else:
attention_group_id_list = [item["group_id"] for item in attention_group_list]
sorted_group_ids_list = GroupUtils.get_group_ids_by_aggs(attention_group_id_list)
group_recommend_ids_list = sorted_group_ids_list
if len(group_recommend_ids_list) < size and len(group_recommend_ids_list)<len(attention_group_list):
sorted_attention_group_list = sorted(attention_group_list,key=lambda item:item["update_time_val"],reverse=False)
for item in sorted_attention_group_list:
if item["group_id"] not in group_recommend_ids_list:
group_recommend_ids_list.append(item["group_id"])
return {"group_recommend_ids": group_recommend_ids_list}
except: except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc()) logging.error("catch exception,err_msg:%s" % traceback.format_exc())
......
...@@ -22,8 +22,8 @@ def home_recommend(user_id=-1,offset=0,size=10): ...@@ -22,8 +22,8 @@ def home_recommend(user_id=-1,offset=0,size=10):
if len(redis_val_dict) > 0: if len(redis_val_dict) > 0:
group_topic_ids = json.loads(redis_val_dict[b"group_topic_ids"]) group_topic_ids = json.loads(redis_val_dict[b"group_topic_ids"])
not_group_topic_ids = json.loads(redis_val_dict[b"not_group_topic_ids"]) not_group_topic_ids = json.loads(redis_val_dict[b"not_group_topic_ids"])
group_topic_ids_index = redis_val_dict[b"group_topic_ids_index"] group_topic_ids_index = int(redis_val_dict[b"group_topic_ids_index"])
not_group_topic_ids_index = redis_val_dict[b"not_group_topic_ids_index"] not_group_topic_ids_index = int(redis_val_dict[b"not_group_topic_ids_index"])
if len(group_topic_ids) < (size-1) or len(not_group_topic_ids) < 1: if len(group_topic_ids) < (size-1) or len(not_group_topic_ids) < 1:
if len(group_topic_ids) < (size-1): if len(group_topic_ids) < (size-1):
......
...@@ -35,10 +35,12 @@ ...@@ -35,10 +35,12 @@
"type":"nested", "type":"nested",
"properties":{ "properties":{
"group_id":{"type":"long"}, "group_id":{"type":"long"},
"update_time":{"type":"date", "format":"date_time_no_millis"} "update_time_val":{"type":"long"}
} }
}, },
"create_time":{"type":"date", "format":"date_time_no_millis"}, "create_time":{"type":"date", "format":"date_time_no_millis"},
"update_time":{"type":"date", "format":"date_time_no_millis"} "update_time":{"type":"date", "format":"date_time_no_millis"},
"create_time_val":{"type":"long"},
"update_time_val":{"type":"long"}
} }
} }
\ No newline at end of file
...@@ -8,6 +8,7 @@ import logging ...@@ -8,6 +8,7 @@ import logging
from libs.es import ESPerform from libs.es import ESPerform
from django.db import models from django.db import models
import datetime import datetime
import time
from libs.tools import tzlc from libs.tools import tzlc
from .group_user_role import GroupUserRole from .group_user_role import GroupUserRole
...@@ -55,7 +56,7 @@ class User(models.Model): ...@@ -55,7 +56,7 @@ class User(models.Model):
for item in query_results: for item in query_results:
item_dict = { item_dict = {
"group_id": item.group_id, "group_id": item.group_id,
"update_time":tzlc(item.update_time) "update_time_val":time.mktime(tzlc(item.update_time).timetuple())
} }
attention_group_id_list.append(item_dict) attention_group_id_list.append(item_dict)
......
...@@ -4,6 +4,7 @@ import os ...@@ -4,6 +4,7 @@ import os
import sys import sys
import logging import logging
import traceback import traceback
import time
from libs.tools import tzlc from libs.tools import tzlc
class UserTransfer(object): class UserTransfer(object):
...@@ -21,8 +22,14 @@ class UserTransfer(object): ...@@ -21,8 +22,14 @@ class UserTransfer(object):
res["city_id"] = instance.city_id res["city_id"] = instance.city_id
res["country_id"] = instance.country_id res["country_id"] = instance.country_id
res["is_online"] = instance.is_online res["is_online"] = instance.is_online
res["create_time"] = tzlc(instance.create_time)
res["update_time"] = tzlc(instance.update_time) tzlc_create_time = tzlc(instance.create_time)
res["create_time"] = tzlc_create_time
res["create_time_val"] = int(time.mktime(tzlc_create_time.timetuple()))
tzlc_update_time = tzlc(instance.update_time)
res["update_time"] = tzlc_update_time
res["update_time_val"] = int(time.mktime(tzlc_update_time.timetuple()))
res["tag_list"] = instance.get_user_tag_id_list() res["tag_list"] = instance.get_user_tag_id_list()
res["attention_user_id_list"] = instance.get_follow_user_id_list() res["attention_user_id_list"] = instance.get_follow_user_id_list()
......
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