Exemple #1
0
    def post(self, returnToken=None, userId=None):  #匹配微信
        params = self.parser.parse_args()
        where = {}
        weights_obj = cal_weights()
        weights = weights_obj.get("weights",
                                  ['org_name', 'work_range', 'timerange'])
        weights_front = self.generateWeights(weights, filter_keys=['org_name'])
        org_name_len = 0
        for key, val in params.items():
            if not val:
                continue

            if key == 'timerange':
                if val:
                    if len(val) == 2:
                        where.update({
                            "info.company_create_date": {
                                "$gte": val[0],
                                "$lte": val[1]
                            },
                        })
                    elif len(val) == 1:
                        where.update({
                            "info.company_create_date": {
                                "$gte": val[0]
                            },
                        })
                else:
                    continue
            else:
                if key == "unite_credict_code":
                    continue
                if key != "page" and key != "limit":
                    val = val.strip()
                    if val:
                        if key == "org_name":
                            org_name_len = len(val.replace(" ", ""))
                            temp = re.split('\s+', val)  #空格匹配
                            where["$and"] = []
                            for temp_i in temp:
                                # print(temp_i)
                                where["$and"].append({
                                    "NickName": {
                                        "$regex":
                                        re.compile("{0}".format(temp_i))
                                    }
                                })
                            continue
                        if key == "work_range":
                            val = "|".join(jieba.lcut(val))
                            where.update({"$or": []})
                            where["$or"] = where["$or"] + [{
                                "info.work_range_common":
                                re.compile("{0}".format(val))
                            }, {
                                "info.work_range_front_premit":
                                re.compile("{0}".format(val))
                            }]

        if params.get("unite_credict_code"):
            where = {
                "$or": [{
                    "gs_credict_code": params.get("unite_credict_code")
                }, {
                    "zz_credict_code": params.get("unite_credict_code")
                }, where]
            }
        if where == {}:
            abort(400, **{"message": {"must": "请至少提供一个参数"}})

        page = params.get("page")
        limit = params.get("limit")
        source_collecion = Source._get_collection()
        res = source_collecion.aggregate([
            {
                "$match": where
            },
            {
                "$facet": {
                    "total": [{
                        "$count": "total"
                    }],
                    "data": [
                        {
                            "$project": {
                                "_id": 1,
                                "NickName": 1,
                                "info": {
                                    "$ifNull": ["$info", {}]
                                },
                                "__biz": 1,
                                "name_score_one": 1,
                                "name_score_one": {
                                    "$cond": [
                                        {
                                            "$and": [
                                                "$NickName",
                                                bool(params.get("org_name"))
                                            ]
                                        },
                                        {
                                            "$divide": [
                                                org_name_len, {
                                                    "$strLenCP": "$NickName"
                                                }
                                            ]
                                        },
                                        None,
                                    ]
                                },
                                "name_score_two": {
                                    "$cond": [
                                        {
                                            "$and": [
                                                "$info.full_name",
                                                bool(params.get("org_name"))
                                            ]
                                        },
                                        {
                                            "$divide": [
                                                org_name_len, {
                                                    "$strLenCP":
                                                    "$info.full_name"
                                                }
                                            ]
                                        },
                                        None,
                                    ]
                                },
                                "work_range_score": {
                                    "$cond": {
                                        "if": bool(params.get("work_range")),
                                        "then": 1,
                                        "else": 0
                                    }
                                },
                                "timerange_score": {
                                    "$cond": {
                                        "if": bool(params.get("timerange")),
                                        "then": 1,
                                        "else": 0
                                    }
                                },
                                "unite_credict_code_score": {
                                    "$cond": {
                                        "if":
                                        bool(params.get("unite_credict_code")),
                                        "then":
                                        1,
                                        "else":
                                        0
                                    }
                                },
                            }
                        },
                        {
                            "$addFields": {
                                "name_score": {
                                    "$let": {
                                        "vars": {
                                            "name_score":
                                            "$name_score_one"
                                            or "$name_score_two" or 0
                                        },
                                        "in": "$$name_score"
                                    }
                                },
                                "source_id": {
                                    "$toString": "$_id"
                                },
                            }
                        },
                        {
                            "$addFields": {
                                "name_score_rate": {
                                    "$multiply":
                                    ["$name_score",
                                     weights.get("org_name")]
                                },
                                "work_range_score_rate": {
                                    "$multiply": [
                                        "$work_range_score",
                                        weights.get("work_range")
                                    ]
                                },
                                "timerange_score_rate": {
                                    "$multiply": [
                                        "$timerange_score",
                                        weights.get("timerange")
                                    ]
                                },
                                "unite_credict_code_score_rate":
                                "$unite_credict_code_score",
                                "f_name_score_rate": {
                                    "$multiply": [
                                        "$name_score",
                                        weights_front.get("org_name")
                                    ]
                                },
                                "f_work_range_score_rate": {
                                    "$multiply": [
                                        "$work_range_score",
                                        weights_front.get("work_range")
                                    ]
                                },
                                "f_timerange_score_rate": {
                                    "$multiply": [
                                        "$timerange_score",
                                        weights_front.get("timerange")
                                    ]
                                },
                            }
                        },
                        {
                            "$addFields": {
                                "score": {
                                    "$sum": [
                                        "$name_score_rate",
                                        "$work_range_score_rate",
                                        "timerange_score_rate",
                                        "$unite_credict_code_score_rate"
                                    ]
                                },
                                "f_score": {
                                    "$sum": [
                                        "$f_name_score_rate",
                                        "f_timerange_score_rate",
                                        "$f_work_range_score_rate",
                                        "$unite_credict_code_score_rate"
                                    ]
                                }
                            }
                        },
                        {
                            "$sort": {
                                "score": -1
                            }
                        },
                        {
                            "$skip": (page - 1) * limit
                        },
                        {
                            "$limit": limit
                        },
                    ]
                },
            },
            {
                "$project": {
                    "total": {
                        "$arrayElemAt": ["$total.total", 0]
                    },
                    "data": 1
                }
            },
        ])
        source_data = loads(dumps(res))
        source_data = source_data[0] if len(source_data) > 0 else {}
        return {"items": source_data, "code": 200, "returnToken": returnToken}
Exemple #2
0
    def get(self, returnToken=None, userId=None):
        collection = Source._get_collection()
        # collection.create_index([("updateAt",-1)])
        params = self.parser.parse_args()
        page = params.get("page")
        limit = params.get("limit")
        tags = params.get("tags")
        keyword = params.get("keyword")
        source_id = params.get("source_id")
        if source_id:
            res = Source.objects.get(_id=ObjectId(source_id)).to_json()
            return {"code": 200, "items": resData, "returnToken": returnToken}
        where = {
            "$or": [{
                "NickName": {
                    "$regex": keyword,
                    "$options": "i"
                }
            }, {
                "__biz": {
                    "$regex": keyword,
                    "$options": "i"
                }
            }]
        } if keyword else {}
        if tags and len(tags) > 0:
            where.update({"tags": {"$all": tags}})
        resData = collection.aggregate([
            {
                "$match": where
            },
            {
                "$lookup": {
                    "from":
                    "article.list",
                    "let": {
                        "sourceId": "$__biz"
                    },
                    "pipeline": [{
                        "$match": {
                            "$expr": {
                                "$eq": ["$biz_id", "$$sourceId"]
                            }
                        }
                    }, {
                        "$count": "article_count"
                    }],
                    "as":
                    "count"
                }
            },
            {
                "$facet": {
                    "total": [{
                        "$count": "total"
                    }],
                    "data": [
                        # {
                        #     "$match": where
                        # },
                        # {
                        # "$lookup":{
                        #     "from": "article.list",
                        #     "localField": "__biz",
                        #     "foreignField": "biz_id",
                        #     "as": "count"
                        # }
                        #    "$lookup":{
                        #         "from": "article.list",
                        #         "let":  {"sourceId": "$__biz"},
                        #         "pipeline": [
                        #             {
                        #                 "$match": {
                        #                     "$expr": {
                        #                         "$eq":["$biz_id" ,"$$sourceId"]
                        #                         }
                        #                 }
                        #             },
                        #             {
                        #                 "$count": "article_count"
                        #             }

                        #         ],
                        #         "as": "count"
                        #     }
                        # },
                        {
                            "$unwind": {
                                "path": "$count",
                                "preserveNullAndEmptyArrays": True
                            }
                        },
                        {
                            "$project": {
                                "updateAt": 1,
                                "NickName": 1,
                                "__biz": 1,
                                "info": {
                                    "$ifNull": ["$info", {}]
                                },
                                "tags": {
                                    "$ifNull": ["$tags", []]
                                },
                                "article_count": {
                                    "$ifNull":
                                    ["$count.article_count", {
                                        "$literal": 0
                                    }]
                                }
                            }
                        },
                        {
                            "$sort": {
                                "updateAt": -1
                            }
                        },
                        {
                            "$skip": (page - 1) * limit
                        },
                        {
                            "$limit": limit
                        },
                    ]
                }
            },
            {
                "$project": {
                    "total": {
                        "$arrayElemAt": ["$total.total", 0]
                    },
                    "data": 1
                }
            }
        ])
        resData = loads(dumps(resData))
        if len(resData) == 0:
            resData = {"data": [], "total": 0}
        else:
            resData = resData[0]

        return {"code": 200, "items": resData, "returnToken": returnToken}