Commit 5995ef9a authored by lixiaofang's avatar lixiaofang

新增搜索接口

parent 53a3904c
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from gm_rpcd.all import bind
import logging
import traceback
import json
from libs.cache import redis_client
from libs.es import ESPerform
from search.utils.group import GroupUtils
from search.utils.common import GroupSortTypes
from libs.es import ESPerform
from trans2es.models.pictorial import PictorialTopics
from trans2es.models.tag import RegisterShowTag, Tag
import random
@bind("physical/search/search_hotword")
def search_hotword(device_id=None, offset=0, size=10):
"""
:remark:搜索页的热门灵感
内容组成:搜索推荐热词register_show_tag
个性化标签physical:linucb:tag_recommend:device_id:
核心词,需要去重community_tag collection 1
①先从后台随机取最多6个搜索推荐热词,不够6个则有多少取多少
②然后随机取和推荐热词一样数量的linUCB标签,取不够数则取核心标签,linUCB+核心词去重后的数量要和搜索推荐热词的数量一样
③最后打乱顺序填入到热门灵感中,超过3行的标签弃掉,不够3行不用管
:param query:
:param offset:
:param size:
:return:
"""
try:
all_tag_name_list = set()
# 获取搜索推荐热词
results_registr_tag = RegisterShowTag.objects.filter(is_deleted=False, is_online=1).count()
results_tag = Tag.objects.filter(is_online=True, collection=1).count()
for i in range(0, 6):
tag_id = random.randint(0, results_registr_tag)
results_tag_chose = Tag.objects.filter(id=tag_id).values_list("name", flat=True)
all_tag_name_list.add(results_tag_chose)
logging.info("get all_tag_name_list01:%s" % all_tag_name_list)
# 获取个性化标签
linucb_recommend_redis_prefix = "physical:linucb:tag_recommend:device_id:"
tag_recommend_redis_key = linucb_recommend_redis_prefix + str(device_id)
linucb_recommend_tag_data = redis_client.get(tag_recommend_redis_key)
linucb_recommend_tag_list = json.loads(linucb_recommend_tag_data) if linucb_recommend_tag_data else []
for item in linucb_recommend_tag_list:
all_tag_name_list.add(item)
logging.info("get all_tag_name_list02:%s" % all_tag_name_list)
# 取不够数则取核心标签
if len(all_tag_name_list) < 12:
for i in range(0, 12):
tag_id = random.randint(0, results_tag)
results_tag_hexin = Tag.objects.filter(id=tag_id).values_list("name", flat=True)
if results_tag_hexin not in all_tag_name_list:
all_tag_name_list.add(results_tag_hexin)
if len(all_tag_name_list) >= 12:
logging.info("get all_tag_name_list03:%s" % all_tag_name_list)
return {"recommend_name": all_tag_name_list}
except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return {"recommend_name": []}
...@@ -14,7 +14,7 @@ from libs.es import ESPerform ...@@ -14,7 +14,7 @@ from libs.es import ESPerform
@bind("physical/search/recommend_user") @bind("physical/search/recommend_user")
def recommend_user(self_user_id,interesting_user_id,offset=0,size=10): def recommend_user(self_user_id, interesting_user_id, offset=0, size=10):
""" """
:remark 点关注推荐用户 :remark 点关注推荐用户
:param self_user_id: :param self_user_id:
...@@ -29,25 +29,29 @@ def recommend_user(self_user_id,interesting_user_id,offset=0,size=10): ...@@ -29,25 +29,29 @@ def recommend_user(self_user_id,interesting_user_id,offset=0,size=10):
if not isinstance(interesting_user_id, int): if not isinstance(interesting_user_id, int):
interesting_user_id = -1 interesting_user_id = -1
#获取es链接对象 # 获取es链接对象
es_cli_obj = ESPerform.get_cli() es_cli_obj = ESPerform.get_cli()
#获取关注用户列表 # 获取关注用户列表
(self_attention_user_id_list,recursion_attention_user_id_list) = UserUtils.get_attention_user_list([self_user_id,interesting_user_id],self_user_id,es_cli_obj) (self_attention_user_id_list, recursion_attention_user_id_list) = UserUtils.get_attention_user_list(
[self_user_id, interesting_user_id], self_user_id, es_cli_obj)
#去除自身及感兴趣的用户ID # 去除自身及感兴趣的用户ID
self_attention_user_id_list.append(self_user_id) self_attention_user_id_list.append(self_user_id)
self_attention_user_id_list.append(interesting_user_id) self_attention_user_id_list.append(interesting_user_id)
recommend_user_list = UserUtils.get_recommend_user_list(self_attention_user_id_list,recursion_attention_user_id_list,offset,size,es_cli_obj) recommend_user_list = UserUtils.get_recommend_user_list(self_attention_user_id_list,
recursion_attention_user_id_list, offset, size,
es_cli_obj)
return recommend_user_list return recommend_user_list
except: except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc()) logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return [] return []
@bind("physical/search/batch_recommend_user") @bind("physical/search/batch_recommend_user")
def batch_recommend_user(self_user_id,interesting_user_id_list,offset=0,size=10): def batch_recommend_user(self_user_id, interesting_user_id_list, offset=0, size=10):
""" """
:remark 点关注推荐用户 - 批量接口 :remark 点关注推荐用户 - 批量接口
:param self_user_id: :param self_user_id:
...@@ -57,23 +61,92 @@ def batch_recommend_user(self_user_id,interesting_user_id_list,offset=0,size=10) ...@@ -57,23 +61,92 @@ def batch_recommend_user(self_user_id,interesting_user_id_list,offset=0,size=10)
:return: :return:
""" """
try: try:
if not isinstance(self_user_id,int): if not isinstance(self_user_id, int):
self_user_id = -1 self_user_id = -1
#获取es链接对象 # 获取es链接对象
es_cli_obj = ESPerform.get_cli() es_cli_obj = ESPerform.get_cli()
#获取关注用户列表 # 获取关注用户列表
(need_filter_attention_user_id_list, attention_user_dict_list,attention_user_id_list) = UserUtils.get_batch_attention_user_list(interesting_user_id_list,self_user_id,es_cli_obj) (need_filter_attention_user_id_list, attention_user_dict_list,
attention_user_id_list) = UserUtils.get_batch_attention_user_list(interesting_user_id_list, self_user_id,
es_cli_obj)
# 去除自身及感兴趣的用户ID
#去除自身及感兴趣的用户ID
need_filter_attention_user_id_list.append(self_user_id) need_filter_attention_user_id_list.append(self_user_id)
recommend_user_dict = UserUtils.get_batch_recommend_user_dict(need_filter_attention_user_id_list=need_filter_attention_user_id_list,attention_user_id_list=attention_user_id_list,attention_user_dict_list=attention_user_dict_list,self_user_id=self_user_id,offset=offset,size=size,es_cli_obj=es_cli_obj) recommend_user_dict = UserUtils.get_batch_recommend_user_dict(
need_filter_attention_user_id_list=need_filter_attention_user_id_list,
attention_user_id_list=attention_user_id_list, attention_user_dict_list=attention_user_dict_list,
self_user_id=self_user_id, offset=offset, size=size, es_cli_obj=es_cli_obj)
logging.info("duan add,recommend_user_dict:%s" % str(recommend_user_dict)) logging.info("duan add,recommend_user_dict:%s" % str(recommend_user_dict))
return recommend_user_dict return recommend_user_dict
except: except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc()) logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return {} return {}
\ No newline at end of file
@bind("physical/search/search_user")
def search_user(query="", offset=0, size=10):
"""
:remark 搜索用户
:param query:
:param offset:
:param size:
:return:
搜索域:用户名称
1.识别query:允许模糊匹配
2.召回部分:全部 线上状态用户
3.rank部分:一级-是否推荐,二级-发帖数
"""
try:
multi_match = {
"fields": ["nick_name"],
"type": "cross_fields",
"operator": "and",
"query": query
}
q = {}
q["query"] = {
"bool": {
"must": [{
"multi_match": multi_match
}, {
"term": {
"is_online": True
}
}
]
}
}
q["sort"] = {
"is_recommend": {
"order": "asc"
},
"count_topic": {
"order": "desc"
},
}
result_dict = ESPerform.get_analyze_results(
ESPerform.get_cli(), sub_index_name="user", query_body=q, offset=offset, size=size
)
res = result_dict["hits"]["hits"]
user_id = [item["_source"]["user_id"] for item in res]
return {"search_user_id": user_id}
except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return {"search_user_id": []}
...@@ -60,6 +60,7 @@ ...@@ -60,6 +60,7 @@
"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"}, "create_time_val":{"type":"long"},
"update_time_val":{"type":"long"} "update_time_val":{"type":"long"},
"count_topic":{"type":"long"}
} }
} }
\ No newline at end of file
...@@ -12,10 +12,10 @@ import datetime ...@@ -12,10 +12,10 @@ import datetime
class TopicTag(models.Model): class TopicTag(models.Model):
class Meta: class Meta:
verbose_name=u"帖子标签" verbose_name = u"帖子标签"
db_table="community_topictag" db_table = "community_topictag"
id = models.IntegerField(primary_key=True,verbose_name=u"主键ID") id = models.IntegerField(primary_key=True, verbose_name=u"主键ID")
tag_id = models.IntegerField(verbose_name=u"标签ID") tag_id = models.IntegerField(verbose_name=u"标签ID")
topic_id = models.IntegerField(verbose_name=u"帖子ID") topic_id = models.IntegerField(verbose_name=u"帖子ID")
is_online = models.BooleanField(verbose_name=u"是否在线") is_online = models.BooleanField(verbose_name=u"是否在线")
...@@ -25,73 +25,84 @@ class TopicTag(models.Model): ...@@ -25,73 +25,84 @@ class TopicTag(models.Model):
class AccountUserTag(models.Model): class AccountUserTag(models.Model):
class Meta: class Meta:
verbose_name=u"用户标签" verbose_name = u"用户标签"
db_table="account_user_tag" db_table = "account_user_tag"
id = models.IntegerField(primary_key=True,verbose_name=u"主键ID") id = models.IntegerField(primary_key=True, verbose_name=u"主键ID")
tag_id = models.IntegerField(verbose_name=u"标签ID") tag_id = models.IntegerField(verbose_name=u"标签ID")
user = models.IntegerField(verbose_name=u"用户ID") user = models.IntegerField(verbose_name=u"用户ID")
is_deleted = models.BooleanField(verbose_name=u"是否删除") is_deleted = models.BooleanField(verbose_name=u"是否删除")
create_time = models.DateTimeField(verbose_name=u'创建时间',default=datetime.datetime.fromtimestamp(0)) create_time = models.DateTimeField(verbose_name=u'创建时间', default=datetime.datetime.fromtimestamp(0))
update_time = models.DateTimeField(verbose_name=u'更新时间', default=datetime.datetime.fromtimestamp(0)) update_time = models.DateTimeField(verbose_name=u'更新时间', default=datetime.datetime.fromtimestamp(0))
is_body_esthetics = models.IntegerField(verbose_name=u"是否有用标签") is_body_esthetics = models.IntegerField(verbose_name=u"是否有用标签")
class Tag(models.Model): class Tag(models.Model):
class Meta: class Meta:
verbose_name=u"标签" verbose_name = u"标签"
db_table="community_tag" db_table = "community_tag"
id = models.IntegerField(primary_key=True,verbose_name=u"主键ID") id = models.IntegerField(primary_key=True, verbose_name=u"主键ID")
is_deleted = models.BooleanField(verbose_name=u"是否删除") is_deleted = models.BooleanField(verbose_name=u"是否删除")
is_online = models.BooleanField(verbose_name=u"是否上线") is_online = models.BooleanField(verbose_name=u"是否上线")
name = models.CharField(verbose_name=u"标签名称",max_length=128) name = models.CharField(verbose_name=u"标签名称", max_length=128)
description = models.TextField(verbose_name=u"标签描述") description = models.TextField(verbose_name=u"标签描述")
icon_url=models.CharField(verbose_name=u"icon_url",max_length=120) icon_url = models.CharField(verbose_name=u"icon_url", max_length=120)
collection = models.IntegerField(verbose_name=u"是否编辑") collection = models.IntegerField(verbose_name=u"是否编辑")
is_ai = models.IntegerField(verbose_name=u"是否ai") is_ai = models.IntegerField(verbose_name=u"是否ai")
is_own = models.IntegerField(verbose_name=u"是否ins上自带") is_own = models.IntegerField(verbose_name=u"是否ins上自带")
create_time = models.DateTimeField(verbose_name=u'创建时间',default=datetime.datetime.fromtimestamp(0)) create_time = models.DateTimeField(verbose_name=u'创建时间', default=datetime.datetime.fromtimestamp(0))
update_time = models.DateTimeField(verbose_name=u'更新时间', default=datetime.datetime.fromtimestamp(0)) update_time = models.DateTimeField(verbose_name=u'更新时间', default=datetime.datetime.fromtimestamp(0))
class CommunityTagFollow(models.Model): class CommunityTagFollow(models.Model):
class Meta: class Meta:
verbose_name=u"用户关注标签" verbose_name = u"用户关注标签"
db_table="community_tag_follow" db_table = "community_tag_follow"
id = models.IntegerField(primary_key=True,verbose_name=u"主键ID")
id = models.IntegerField(primary_key=True, verbose_name=u"主键ID")
is_deleted = models.BooleanField(verbose_name=u"是否删除") is_deleted = models.BooleanField(verbose_name=u"是否删除")
is_online = models.BooleanField(verbose_name=u"是否上线") is_online = models.BooleanField(verbose_name=u"是否上线")
user_id = models.IntegerField(verbose_name=u"用户ID") user_id = models.IntegerField(verbose_name=u"用户ID")
tag_id = models.IntegerField(verbose_name=u"帖子ID") tag_id = models.IntegerField(verbose_name=u"帖子ID")
create_time = models.DateTimeField(verbose_name=u'创建时间',default=datetime.datetime.fromtimestamp(0)) create_time = models.DateTimeField(verbose_name=u'创建时间', default=datetime.datetime.fromtimestamp(0))
update_time = models.DateTimeField(verbose_name=u'更新时间', default=datetime.datetime.fromtimestamp(0)) update_time = models.DateTimeField(verbose_name=u'更新时间', default=datetime.datetime.fromtimestamp(0))
class CommunityTagTypeRelation(models.Model): class CommunityTagTypeRelation(models.Model):
class Meta: class Meta:
verbose_name=u"标签类型对应关系" verbose_name = u"标签类型对应关系"
db_table="community_tag_type_relation" db_table = "community_tag_type_relation"
id = models.IntegerField(primary_key=True,verbose_name=u"主键ID") id = models.IntegerField(primary_key=True, verbose_name=u"主键ID")
is_deleted = models.BooleanField(verbose_name=u"是否删除") is_deleted = models.BooleanField(verbose_name=u"是否删除")
is_online = models.BooleanField(verbose_name=u"是否上线") is_online = models.BooleanField(verbose_name=u"是否上线")
tag_id = models.IntegerField(verbose_name=u"标签ID") tag_id = models.IntegerField(verbose_name=u"标签ID")
tag_type_id = models.IntegerField(verbose_name=u"标签类型ID") tag_type_id = models.IntegerField(verbose_name=u"标签类型ID")
create_time = models.DateTimeField(verbose_name=u'创建时间',default=datetime.datetime.fromtimestamp(0)) create_time = models.DateTimeField(verbose_name=u'创建时间', default=datetime.datetime.fromtimestamp(0))
update_time = models.DateTimeField(verbose_name=u'更新时间', default=datetime.datetime.fromtimestamp(0)) update_time = models.DateTimeField(verbose_name=u'更新时间', default=datetime.datetime.fromtimestamp(0))
class CommunityTagType(models.Model): class CommunityTagType(models.Model):
class Meta: class Meta:
verbose_name=u"标签类型" verbose_name = u"标签类型"
db_table="community_tag_type" db_table = "community_tag_type"
id = models.IntegerField(primary_key=True,verbose_name=u"主键ID") id = models.IntegerField(primary_key=True, verbose_name=u"主键ID")
is_deleted = models.BooleanField(verbose_name=u"是否删除") is_deleted = models.BooleanField(verbose_name=u"是否删除")
is_online = models.BooleanField(verbose_name=u"是否上线") is_online = models.BooleanField(verbose_name=u"是否上线")
name = models.CharField(verbose_name=u"名称",max_length=64) name = models.CharField(verbose_name=u"名称", max_length=64)
create_time = models.DateTimeField(verbose_name=u'创建时间',default=datetime.datetime.fromtimestamp(0)) create_time = models.DateTimeField(verbose_name=u'创建时间', default=datetime.datetime.fromtimestamp(0))
update_time = models.DateTimeField(verbose_name=u'更新时间', default=datetime.datetime.fromtimestamp(0)) update_time = models.DateTimeField(verbose_name=u'更新时间', default=datetime.datetime.fromtimestamp(0))
class RegisterShowTag(models.Model):
class Meta:
verbose_name = "搜索热词"
db_table = "register_show_tag"
id = models.IntegerField(primary_key=True, verbose_name=u"主键ID")
is_deleted = models.IntegerField(verbose_name=u"是否删除")
is_online = models.IntegerField(verbose_name=u"是否在线")
tag_id = models.IntegerField(verbose_name="标签ID")
...@@ -16,6 +16,8 @@ from .tag import AccountUserTag ...@@ -16,6 +16,8 @@ from .tag import AccountUserTag
from .topic import Topic from .topic import Topic
from .user_extra import UserExtra from .user_extra import UserExtra
from .pictorial import PictorialFollow from .pictorial import PictorialFollow
from .topic import Topic
class User(models.Model): class User(models.Model):
class Meta: class Meta:
...@@ -35,9 +37,10 @@ class User(models.Model): ...@@ -35,9 +37,10 @@ class User(models.Model):
update_time = models.DateTimeField(verbose_name=u'更新时间', default=datetime.datetime.fromtimestamp(0)) update_time = models.DateTimeField(verbose_name=u'更新时间', default=datetime.datetime.fromtimestamp(0))
@classmethod @classmethod
def get_user_nick_name(cls,user_id): def get_user_nick_name(cls, user_id):
try: try:
nick_name = User.objects.using(settings.SLAVE_DB_NAME).filter(user_id=user_id).values_list("nick_name").first() nick_name = User.objects.using(settings.SLAVE_DB_NAME).filter(user_id=user_id).values_list(
"nick_name").first()
return nick_name[0] return nick_name[0]
except: except:
...@@ -47,7 +50,8 @@ class User(models.Model): ...@@ -47,7 +50,8 @@ class User(models.Model):
def get_is_recommend_flag(self): def get_is_recommend_flag(self):
is_shadow = False is_shadow = False
is_recommend = False is_recommend = False
query_sql = UserExtra.objects.using(settings.SLAVE_DB_NAME).filter(user_id=self.user_id, is_deleted=False, is_online=True) query_sql = UserExtra.objects.using(settings.SLAVE_DB_NAME).filter(user_id=self.user_id, is_deleted=False,
is_online=True)
for record in query_sql: for record in query_sql:
is_recommend = record.is_recommend is_recommend = record.is_recommend
is_shadow = record.is_shadow is_shadow = record.is_shadow
...@@ -58,8 +62,9 @@ class User(models.Model): ...@@ -58,8 +62,9 @@ class User(models.Model):
latest_topic_time_val = -1 latest_topic_time_val = -1
# 获取该用户最新发帖时间 # 获取该用户最新发帖时间
topic_records = Topic.objects.using(settings.SLAVE_DB_NAME).filter(user_id=self.user_id).order_by("-update_time").values_list("update_time", topic_records = Topic.objects.using(settings.SLAVE_DB_NAME).filter(user_id=self.user_id).order_by(
flat=True).first() "-update_time").values_list("update_time",
flat=True).first()
if topic_records: if topic_records:
tzlc_topic_update_time = tzlc(topic_records) tzlc_topic_update_time = tzlc(topic_records)
latest_topic_time_val = int(time.mktime(tzlc_topic_update_time.timetuple())) latest_topic_time_val = int(time.mktime(tzlc_topic_update_time.timetuple()))
...@@ -75,7 +80,8 @@ class User(models.Model): ...@@ -75,7 +80,8 @@ class User(models.Model):
logging.info("get follow_user_id_list :%s" % follow_user_id_list) logging.info("get follow_user_id_list :%s" % follow_user_id_list)
for i in range(0, len(follow_user_id_list), 1000): for i in range(0, len(follow_user_id_list), 1000):
logging.info("get follow_user_id_list :%s" % follow_user_id_list[i:i + 1000]) logging.info("get follow_user_id_list :%s" % follow_user_id_list[i:i + 1000])
sql_data_list = User.objects.using(settings.SLAVE_DB_NAME).filter(user_id__in=follow_user_id_list[i:i + 1000]) sql_data_list = User.objects.using(settings.SLAVE_DB_NAME).filter(
user_id__in=follow_user_id_list[i:i + 1000])
for detail_data in sql_data_list: for detail_data in sql_data_list:
item = { item = {
"user_id": detail_data.user_id, "user_id": detail_data.user_id,
...@@ -88,7 +94,8 @@ class User(models.Model): ...@@ -88,7 +94,8 @@ class User(models.Model):
def get_attention_group_id_list(self): def get_attention_group_id_list(self):
try: try:
attention_group_id_list = list() attention_group_id_list = list()
query_results = GroupUserRole.objects.using(settings.SLAVE_DB_NAME).filter(is_online=True, user_id=self.user_id) query_results = GroupUserRole.objects.using(settings.SLAVE_DB_NAME).filter(is_online=True,
user_id=self.user_id)
for item in query_results: for item in query_results:
item_dict = { item_dict = {
"group_id": item.group_id, "group_id": item.group_id,
...@@ -104,15 +111,16 @@ class User(models.Model): ...@@ -104,15 +111,16 @@ class User(models.Model):
def get_attention_pictorial_id_list(self): def get_attention_pictorial_id_list(self):
try: try:
attention_pictorial_id_list = list() attention_pictorial_id_list = list()
query_results = PictorialFollow.objects.using(settings.SLAVE_DB_NAME).filter(is_online=True, user_id=self.user_id) query_results = PictorialFollow.objects.using(settings.SLAVE_DB_NAME).filter(is_online=True,
logging.info("get PictorialFollow:%s"%query_results) user_id=self.user_id)
logging.info("get PictorialFollow:%s" % query_results)
for item in query_results: for item in query_results:
item_dict = { item_dict = {
"pictorial_id": item.pictorial_id, "pictorial_id": item.pictorial_id,
"update_time_val": time.mktime(tzlc(item.update_time).timetuple()) "update_time_val": time.mktime(tzlc(item.update_time).timetuple())
} }
attention_pictorial_id_list.append(item_dict) attention_pictorial_id_list.append(item_dict)
logging.info("get user_id:%s" %self.user_id) logging.info("get user_id:%s" % self.user_id)
logging.info("get same_pictorial_user_id_list:%s" % attention_pictorial_id_list) logging.info("get same_pictorial_user_id_list:%s" % attention_pictorial_id_list)
...@@ -149,25 +157,29 @@ class User(models.Model): ...@@ -149,25 +157,29 @@ class User(models.Model):
def get_same_pictorial_user_id_list(self): def get_same_pictorial_user_id_list(self):
#todo 有的同组数据过大,导致celery cpu过高,暂时限制同组的数据大小,后续可能会去掉同组的数据 # todo 有的同组数据过大,导致celery cpu过高,暂时限制同组的数据大小,后续可能会去掉同组的数据
same_pictorial_user_id_list = list() same_pictorial_user_id_list = list()
pictorial_items_list = list(PictorialFollow.objects.using(settings.SLAVE_DB_NAME).filter(user_id=self.user_id).values_list("pictorial_id",flat=True)) pictorial_items_list = list(
PictorialFollow.objects.using(settings.SLAVE_DB_NAME).filter(user_id=self.user_id).values_list(
"pictorial_id", flat=True))
for pictorial_id in pictorial_items_list: for pictorial_id in pictorial_items_list:
user_items_list = list(PictorialFollow.objects.using(settings.SLAVE_DB_NAME).filter(pictorial_id=pictorial_id).values_list("user_id",flat=True)) user_items_list = list(
PictorialFollow.objects.using(settings.SLAVE_DB_NAME).filter(pictorial_id=pictorial_id).values_list(
"user_id", flat=True))
for user_id in user_items_list: for user_id in user_items_list:
same_pictorial_user_id_list.append(user_id) same_pictorial_user_id_list.append(user_id)
if len(same_pictorial_user_id_list)>=100: if len(same_pictorial_user_id_list) >= 100:
break break
if len(same_pictorial_user_id_list)>=100: if len(same_pictorial_user_id_list) >= 100:
break break
logging.info("get same user_id:%s"%self.user_id) logging.info("get same user_id:%s" % self.user_id)
logging.info("get same_pictorial_user_id_list:%s"%same_pictorial_user_id_list) logging.info("get same_pictorial_user_id_list:%s" % same_pictorial_user_id_list)
same_pictorial_detail_list = list() same_pictorial_detail_list = list()
for i in range(0, len(same_pictorial_user_id_list), 200): for i in range(0, len(same_pictorial_user_id_list), 200):
sql_data_list = User.objects.using(settings.SLAVE_DB_NAME).filter(user_id__in=same_pictorial_user_id_list[i:i + 1000]) sql_data_list = User.objects.using(settings.SLAVE_DB_NAME).filter(
user_id__in=same_pictorial_user_id_list[i:i + 1000])
for detail_data in sql_data_list: for detail_data in sql_data_list:
item = { item = {
"user_id": detail_data.user_id, "user_id": detail_data.user_id,
...@@ -181,7 +193,8 @@ class User(models.Model): ...@@ -181,7 +193,8 @@ class User(models.Model):
try: try:
user_tag_id_list = list() user_tag_id_list = list()
query_results = AccountUserTag.objects.using(settings.SLAVE_DB_NAME).filter(user=self.user_id, is_deleted=False) query_results = AccountUserTag.objects.using(settings.SLAVE_DB_NAME).filter(user=self.user_id,
is_deleted=False)
for item in query_results: for item in query_results:
user_tag_id_list.append(item.tag_id) user_tag_id_list.append(item.tag_id)
...@@ -194,11 +207,21 @@ class User(models.Model): ...@@ -194,11 +207,21 @@ class User(models.Model):
try: try:
user_useful_tag_id_list = list() user_useful_tag_id_list = list()
query_results = AccountUserTag.objects.using(settings.SLAVE_DB_NAME).filter(user=self.user_id,is_body_esthetics =1, is_deleted=False) query_results = AccountUserTag.objects.using(settings.SLAVE_DB_NAME).filter(user=self.user_id,
is_body_esthetics=1,
is_deleted=False)
for item in query_results: for item in query_results:
user_useful_tag_id_list.append(item.tag_id) user_useful_tag_id_list.append(item.tag_id)
return user_useful_tag_id_list return user_useful_tag_id_list
except: except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc()) logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return [] return []
\ No newline at end of file
def get_topic_number(self):
try:
number = Topic.objects.filter(user_id=self.user_id).count()
return number
except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return 0
...@@ -14,7 +14,7 @@ import sys ...@@ -14,7 +14,7 @@ import sys
from libs.cache import redis_client from libs.cache import redis_client
import copy import copy
from trans2es.models import topic, user, pick_celebrity, group, celebrity, tag, contrast_similar,pictorial from trans2es.models import topic, user, pick_celebrity, group, celebrity, tag, contrast_similar, pictorial
from trans2es.utils.user_transfer import UserTransfer from trans2es.utils.user_transfer import UserTransfer
from trans2es.utils.pick_celebrity_transfer import PickCelebrityTransfer from trans2es.utils.pick_celebrity_transfer import PickCelebrityTransfer
from trans2es.utils.group_transfer import GroupTransfer from trans2es.utils.group_transfer import GroupTransfer
...@@ -196,7 +196,7 @@ class TypeInfo(object): ...@@ -196,7 +196,7 @@ class TypeInfo(object):
old_data["is_history"] = True old_data["is_history"] = True
data_list.append(old_data) data_list.append(old_data)
if int_ori_topic_star>=4: if int_ori_topic_star >= 4:
topic_data_high_star_list.append(old_data) topic_data_high_star_list.append(old_data)
redis_client.hset(self.physical_topic_star, data["id"], data["content_level"]) redis_client.hset(self.physical_topic_star, data["id"], data["content_level"])
# data_list = [ # data_list = [
...@@ -207,12 +207,11 @@ class TypeInfo(object): ...@@ -207,12 +207,11 @@ class TypeInfo(object):
# ] # ]
# ESPerform.es_helpers_bulk(ESPerform.get_cli(), data_list, "topic-star") # ESPerform.es_helpers_bulk(ESPerform.get_cli(), data_list, "topic-star")
if data["content_level"] and int(data["content_level"]) >= 4:
if data["content_level"] and int(data["content_level"])>=4:
topic_data_high_star_list.append(data) topic_data_high_star_list.append(data)
data_list.append(data) data_list.append(data)
return (data_list,topic_data_high_star_list) return (data_list, topic_data_high_star_list)
def elasticsearch_bulk_insert_data(self, sub_index_name, data_list, es=None): def elasticsearch_bulk_insert_data(self, sub_index_name, data_list, es=None):
...@@ -255,17 +254,17 @@ class TypeInfo(object): ...@@ -255,17 +254,17 @@ class TypeInfo(object):
else: else:
qs = self.model.objects.all() qs = self.model.objects.all()
end = time.time() end = time.time()
time0=end-begin time0 = end - begin
begin = time.time() begin = time.time()
instance_list = qs.filter(pk__in=pk_list) instance_list = qs.filter(pk__in=pk_list)
end = time.time() end = time.time()
time1=end-begin time1 = end - begin
begin = time.time() begin = time.time()
data_list, topic_data_high_star_list = self.bulk_get_data(instance_list) data_list, topic_data_high_star_list = self.bulk_get_data(instance_list)
end = time.time() end = time.time()
time2=end-begin time2 = end - begin
begin = time.time() begin = time.time()
# logging.info("get sub_index_name:%s"%sub_index_name) # logging.info("get sub_index_name:%s"%sub_index_name)
...@@ -277,7 +276,7 @@ class TypeInfo(object): ...@@ -277,7 +276,7 @@ class TypeInfo(object):
es=es, es=es,
) )
if sub_index_name=="topic": if sub_index_name == "topic":
self.elasticsearch_bulk_insert_data( self.elasticsearch_bulk_insert_data(
sub_index_name="topic-star-routing", sub_index_name="topic-star-routing",
data_list=data_list, data_list=data_list,
...@@ -285,7 +284,7 @@ class TypeInfo(object): ...@@ -285,7 +284,7 @@ class TypeInfo(object):
) )
# 同时写4星及以上的帖子 # 同时写4星及以上的帖子
if len(topic_data_high_star_list)>0: if len(topic_data_high_star_list) > 0:
self.elasticsearch_bulk_insert_data( self.elasticsearch_bulk_insert_data(
sub_index_name="topic-high-star", sub_index_name="topic-high-star",
data_list=topic_data_high_star_list, data_list=topic_data_high_star_list,
...@@ -293,9 +292,8 @@ class TypeInfo(object): ...@@ -293,9 +292,8 @@ class TypeInfo(object):
) )
end = time.time() end = time.time()
time3=end-begin time3 = end - begin
logging.info("duan add,insert_table_by_pk_list time cost:%ds,%ds,%ds,%ds" % (time0,time1,time2,time3)) logging.info("duan add,insert_table_by_pk_list time cost:%ds,%ds,%ds,%ds" % (time0, time1, time2, time3))
def insert_table_chunk(self, sub_index_name, table_chunk, es=None): def insert_table_chunk(self, sub_index_name, table_chunk, es=None):
try: try:
...@@ -317,7 +315,7 @@ class TypeInfo(object): ...@@ -317,7 +315,7 @@ class TypeInfo(object):
auto_create_index=True auto_create_index=True
) )
logging.info("es_helpers_bulk,sub_index_name:%s,data_list len:%d" % (sub_index_name,len(data_list))) logging.info("es_helpers_bulk,sub_index_name:%s,data_list len:%d" % (sub_index_name, len(data_list)))
stage_3_time = time.time() stage_3_time = time.time()
end_clock = time.clock() end_clock = time.clock()
...@@ -355,8 +353,8 @@ def get_type_info_map(): ...@@ -355,8 +353,8 @@ def get_type_info_map():
name='topic-star', name='topic-star',
type='topic-star', type='topic-star',
model=topic.Topic, model=topic.Topic,
query_deferred=lambda: topic.Topic.objects.all().query,#假的 query_deferred=lambda: topic.Topic.objects.all().query, # 假的
get_data_func=TopicTransfer.get_topic_data,#假的 get_data_func=TopicTransfer.get_topic_data, # 假的
bulk_insert_chunk_size=100, bulk_insert_chunk_size=100,
round_insert_chunk_size=5, round_insert_chunk_size=5,
round_insert_period=2, round_insert_period=2,
...@@ -525,4 +523,3 @@ def get_type_info_map(): ...@@ -525,4 +523,3 @@ def get_type_info_map():
_get_type_info_map_result = type_info_map _get_type_info_map_result = type_info_map
return type_info_map return type_info_map
...@@ -8,16 +8,15 @@ import logging ...@@ -8,16 +8,15 @@ import logging
import traceback import traceback
from libs.tools import tzlc from libs.tools import tzlc
from trans2es.models.topic import Topic from trans2es.models.topic import Topic
from trans2es.models.tag import TopicTag,CommunityTagType,CommunityTagTypeRelation from trans2es.models.tag import TopicTag, CommunityTagType, CommunityTagTypeRelation
import datetime import datetime
from django.conf import settings from django.conf import settings
class TagTransfer(object): class TagTransfer(object):
@classmethod @classmethod
def get_tag_name_data(cls,instance): def get_tag_name_data(cls, instance):
try: try:
res = dict() res = dict()
res["name"] = instance.name res["name"] = instance.name
...@@ -28,7 +27,7 @@ class TagTransfer(object): ...@@ -28,7 +27,7 @@ class TagTransfer(object):
return dict() return dict()
@classmethod @classmethod
def get_tag_data(cls,instance): def get_tag_data(cls, instance):
try: try:
res = dict() res = dict()
...@@ -36,14 +35,14 @@ class TagTransfer(object): ...@@ -36,14 +35,14 @@ class TagTransfer(object):
tag_name_terms_list = list() tag_name_terms_list = list()
for i in range(len(instance.name)): for i in range(len(instance.name)):
for j in range(i,len(instance.name)+1): for j in range(i, len(instance.name) + 1):
name_term = instance.name[i:j].strip() name_term = instance.name[i:j].strip()
if name_term: if name_term:
tag_name_terms_list.append(name_term.lower()) tag_name_terms_list.append(name_term.lower())
res["suggest"] = { res["suggest"] = {
"input":tag_name_terms_list, "input": tag_name_terms_list,
"contexts":{ "contexts": {
"is_online": [instance.is_online], "is_online": [instance.is_online],
"is_deleted": [instance.is_deleted] "is_deleted": [instance.is_deleted]
} }
...@@ -54,24 +53,27 @@ class TagTransfer(object): ...@@ -54,24 +53,27 @@ class TagTransfer(object):
topic_num = 0 topic_num = 0
res["near_new_topic_num"] = topic_num res["near_new_topic_num"] = topic_num
if instance.is_online==True and instance.is_deleted==False: if instance.is_online == True and instance.is_deleted == False:
topic_id_list = list() topic_id_list = list()
sql_result_results = list(TopicTag.objects.using(settings.SLAVE_DB_NAME).filter( sql_result_results = list(TopicTag.objects.using(settings.SLAVE_DB_NAME).filter(
tag_id=instance.id).values_list("topic_id", "is_online")) tag_id=instance.id).values_list("topic_id", "is_online"))
for topic_id,is_online in sql_result_results: for topic_id, is_online in sql_result_results:
if is_online: if is_online:
topic_id_list.append(topic_id) topic_id_list.append(topic_id)
time_base_val = datetime.datetime.strftime(datetime.datetime.now()+datetime.timedelta(-7), "%Y-%m-%d") time_base_val = datetime.datetime.strftime(datetime.datetime.now() + datetime.timedelta(-7), "%Y-%m-%d")
for topic_begin_index in range(0,len(topic_id_list),100): for topic_begin_index in range(0, len(topic_id_list), 100):
cur_topic_num = Topic.objects.using(settings.SLAVE_DB_NAME).filter(id__in=topic_id_list[topic_begin_index:topic_begin_index+100],create_time__gte=time_base_val).count() cur_topic_num = Topic.objects.using(settings.SLAVE_DB_NAME).filter(
id__in=topic_id_list[topic_begin_index:topic_begin_index + 100],
create_time__gte=time_base_val).count()
topic_num += cur_topic_num topic_num += cur_topic_num
res["near_new_topic_num"] = topic_num res["near_new_topic_num"] = topic_num
tag_type_sql_list = CommunityTagTypeRelation.objects.using(settings.SLAVE_DB_NAME).filter(tag_id=instance.id).values_list("tag_type_id",flat=True) tag_type_sql_list = CommunityTagTypeRelation.objects.using(settings.SLAVE_DB_NAME).filter(
tag_id=instance.id).values_list("tag_type_id", flat=True)
tag_type_list = list() tag_type_list = list()
for tag_type_id in tag_type_sql_list: for tag_type_id in tag_type_sql_list:
tag_type_list.append(tag_type_id) tag_type_list.append(tag_type_id)
...@@ -83,4 +85,4 @@ class TagTransfer(object): ...@@ -83,4 +85,4 @@ class TagTransfer(object):
return res return res
except: except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc()) logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return dict() return dict()
\ No newline at end of file
...@@ -21,8 +21,9 @@ class UserTransfer(object): ...@@ -21,8 +21,9 @@ class UserTransfer(object):
follow_user_detail_list = list() follow_user_detail_list = list()
for i in range(0, len(follow_user_id_list), 1000): for i in range(0, len(follow_user_id_list), 1000):
sql_data_list = User.objects.using(settings.SLAVE_DB_NAME).filter(user_id__in=follow_user_id_list[i:i + 1000], is_online=True, sql_data_list = User.objects.using(settings.SLAVE_DB_NAME).filter(
is_deleted=False) user_id__in=follow_user_id_list[i:i + 1000], is_online=True,
is_deleted=False)
for detail_data in sql_data_list: for detail_data in sql_data_list:
item = { item = {
"user_id": detail_data.user_id, "user_id": detail_data.user_id,
...@@ -46,7 +47,7 @@ class UserTransfer(object): ...@@ -46,7 +47,7 @@ class UserTransfer(object):
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["is_deleted"] = instance.is_deleted res["is_deleted"] = instance.is_deleted
res["count_topic"] = instance.get_topic_number()
try: try:
(is_recommend, is_shadow) = instance.get_is_recommend_flag() (is_recommend, is_shadow) = instance.get_is_recommend_flag()
res["is_recommend"] = is_recommend res["is_recommend"] = is_recommend
...@@ -95,7 +96,6 @@ class UserTransfer(object): ...@@ -95,7 +96,6 @@ class UserTransfer(object):
res["attention_pictorial_id_list"] = [] res["attention_pictorial_id_list"] = []
res["same_pictorial_user_id_list"] = [] res["same_pictorial_user_id_list"] = []
return res return res
except: except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc()) logging.error("catch exception,err_msg:%s" % traceback.format_exc())
......
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