#!/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, attention_tag_list=[]): try: if not es_cli_obj: es_cli_obj = ESPerform.get_cli() functions_list = list() for tag_id in attention_tag_list: functions_list.append({ "filter": { "constant_score": { "filter": { "term": { "tag_id": tag_id } } } }, "weight": 20 }) if len(functions_list) >= 20: break functions_list.append( { "gauss": { "create_time": { "scale": "1d", "decay": 0.99 } }, "weight": 60 } ) q = { "query": { "function_score": { "query": { "bool": { "filter": [ {"term": {"is_online": True}}, {"term": {"is_deleted": False}}, {"term": {"effective": True}} ], "must_not": [ {"term": {"is_default": 1}} ] } }, "score_mode": "sum", "boost_mode": "sum", "functions": functions_list } } } q["sort"] = [ { "_script": { "type": "number", "script": { "lang": "expression", "source": "_score+doc['offline_score']" }, "order": "desc" } }, { "_score": { "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 [] @classmethod def get_search_pictorial_topic(cls, query, offset, size): try: q = dict() multi_fields = { 'name': 4, 'description': 4, 'tag_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}}, {"term": {"is_default": 0}}, {"range": {"topic_id_list": {"gte": 0}}}, {"term": {"is_cover": True}} ], "should": [ {'multi_match': multi_match} ], "minimum_should_match": 1 } } q["_source"] = { "includes": ["id", "is_online", "is_deleted", "is_default", "name", "tag_name", "description", "is_cover", "offline_score", "is_default"] } q["sort"] = [ { "_script": { "type": "number", "script": { "lang": "expression", "source": "_score+doc['offline_score']" }, "order": "desc" } }, { "_score": { "order": "desc" } } ] logging.info("get get_search_pictorial_topic:%s" % q) es_cli_obj = ESPerform.get_cli() result_dict = ESPerform.get_search_results(es_cli_obj, "pictorial", q, offset, size) return result_dict except: logging.error("catch exception,err_msg:%s" % traceback.format_exc()) return None