1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import logging
import traceback
from libs.es import ESPerform
class GroupUtils(object):
@classmethod
def get_group_query_result(cls, query, offset, size):
try:
q = dict()
multi_fields = {
'description': 2,
'name': 4,
}
query_fields = ['^'.join((k, str(v))) for (k, v) in multi_fields.items()]
multi_match = {
'query': query,
'type': 'cross_fields',
'operator': 'and',
'fields': query_fields,
}
q['query'] = {
'bool': {
"must": [
{"term": {"is_online": True}},
{"term": {"is_deleted": False}}
],
"should": [
{'multi_match': multi_match}
],
"minimum_should_match": 1
}
}
q["_source"] = {
"includes": ["id"]
}
return ESPerform.get_search_results(ESPerform.get_cli(), "group", q, offset, size)
except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return {"total_count": 0, "hits": []}
@classmethod
def get_hot_pictorial_recommend_result_list(cls,offset,size,es_cli_obj=None):
try:
if not es_cli_obj:
es_cli_obj = ESPerform.get_cli()
q = dict()
q["query"] = {
"bool":{
"must":[
{"term": {"is_online": True}},
{"term":{"is_deleted": False}},
{"term": {"effective": True}}
]
}
}
q["sort"] = [
{"high_quality_topic_num":{"order":"desc"}}
]
q["_source"] = {
"includes":["id"]
}
result_dict = ESPerform.get_search_results(es_cli_obj,"pictorial",q,offset,size)
pictorial_ids_list = []
if len(result_dict["hits"]) > 0:
pictorial_ids_list = [item["_source"]["id"] for item in result_dict["hits"]]
return pictorial_ids_list
except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return []
@classmethod
def get_user_attention_pictorial_list(cls, user_id, offset=0, size=10, es_cli_obj=None):
"""
:remark: 获取用户关注小组列表
:return:
"""
try:
if not es_cli_obj:
es_cli_obj = ESPerform.get_cli()
q = dict()
q["query"] = {
"bool": {
"must": [
{"term": {"is_online": True}},
{"term": {"user_id": user_id}},
{"term": {"is_deleted": False}},
{"term":{"effective":True}}
]
}
}
q["_source"] = {
"includes": ["attention_pictorial_id_list"]
}
result_dict = ESPerform.get_search_results(es_cli_obj, "user", q, offset, size)
if len(result_dict["hits"]) > 0:
return result_dict["hits"][0]["_source"]["attention_pictorial_id_list"]
else:
return []
except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return []
@classmethod
def get_pictorial_ids_by_aggs(cls, pictorial_ids_list, es_cli_obj=None):
"""
:remark:聚合查询获取小组列表
:param group_id_list:
:return:
"""
try:
if not es_cli_obj:
es_cli_obj = ESPerform.get_cli()
q = dict()
q["size"] = 0
q["query"] = {
"terms": {
"pictorial_id": pictorial_ids_list
}
}
q["aggs"] = {
"pictorial_ids": {
"terms": {
"field": "pictorial_id"
},
"aggs": {
"max_date": {
"max": {
"field": "update_time_val"
}
}
}
}
}
result_dict = ESPerform.get_search_results(es_cli_obj, "topic", q, aggregations_query=True)
buckets_list = result_dict["aggregations"]["pictorial_ids"]["buckets"]
sorted_buckets_list = sorted(buckets_list, key=lambda item: item["max_date"]["value"], reverse=True)
sorted_pictorial_id_list = [item["key"] for item in sorted_buckets_list]
return sorted_pictorial_id_list
except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return []