def delete_product(product): """删除产品及产品相关信息""" product_id = product.get('_id') MongodbUtil.delete('shopping', 'product', product) logger.info('Delete a product: %s successfully!!!' % product.get('title')) #删除索引中product.id keyword_list = product.get('keywordList') for keyword in keyword_list: keyword = MongodbUtil.find_one('shopping', 'keywordIndex', {'keyword': keyword}) if not keyword: continue if keyword['invertedIndex'].__contains__(product_id.__str__()): del keyword['invertedIndex'][product_id.__str__()] MongodbUtil.save('shopping', 'keywordIndex', keyword) logger.info('Update keyword index: %s successfully!!!' % keyword.get('keyword')) #删除merchant中product.id merchant_id = product.get('merchantId') merchant = MongodbUtil.find_one('shopping', 'merchant', {'_id': merchant_id}) index_id = merchant['productIdList'].index(product_id) if isinstance(index_id, int): del merchant['productIdList'][index_id] MongodbUtil.save('shopping', 'merchant', merchant) logger.info('Update merchant productIdList successfully!!!') #删除product的image及图片文件 images = MongodbUtil.find('shopping', 'image', {'productId': product_id}) for image in images: MongodbUtil.remove(filename=image.get('fileName')) MongodbUtil.delete('shopping', 'image', {'productId': product_id})
def save_keyword_index(keyword_set, product): """遍历keywordList更新倒排索引""" try: for keyword in keyword_set: existingKeyword = MongodbUtil.find_one('shopping', 'keywordIndex', {'keyword': keyword}) if existingKeyword: if not existingKeyword['invertedIndex'].__contains__(product.get('_id').__str__()): existingKeyword['invertedIndex'][product.get('_id').__str__()] = 100.0 status_id = MongodbUtil.save('shopping', 'keywordIndex', existingKeyword) if status_id: logger.info('Update keywordIndex: %s successfully!!!' % keyword) else: save_keyword_index(keyword_set, product) logger.info('Try to update keywordIndex: %s again!!!' % keyword) else: keywordIndex = { 'keyword': keyword, 'searchTimes': 0, 'invertedIndex': {product.get('_id').__str__(): 100.0}, } status_id = MongodbUtil.insert('shopping', 'keywordIndex', keywordIndex) if status_id: logger.info('Save keywordIndex: %s successfully!!!' % keyword) else: save_keyword_index(keyword_set, product) logger.info('Try to update keywordIndex: %s again!!!' % keyword) except Exception as e: logger.error(e.message)
def do_update_priority(product): """更新产品优先级 优先级=1/关键词数 + 商品价格*点击数 + 1/keywordList.index""" try: keyword_list = product.get('keywordList') product_price = float(product.get('price')) #获取该商品总的点击数 cpc_list = MongodbUtil.find('shopping', 'cpc', {'cpcProductId': product.get('_id')}) cpc_count = 1 cpc_active_count = 1 for cpc in cpc_list: cpc_count += int(cpc.get('cpcCount')) cpc_active_count += int(cpc.get('cpcActiveCount')) cpc_average_count = float(cpc_count + cpc_active_count) / 2.0 # 更新每个产品的keywordList的每个keyword中产品的优先级 index = 1 for keyword in keyword_list: priority = 1.0 / float(len(keyword_list)) + MathUtil.parse2percent(product_price * cpc_average_count) + 1.0 / float(index) index += 1 keyword_index = MongodbUtil.find_one('shopping', 'keywordIndex', {'keyword': keyword}) keyword_index['invertedIndex'][product.get('_id').__str__()] = priority obj_id = MongodbUtil.save('shopping', 'keywordIndex', keyword_index) if obj_id: logger.info('Update %s\'s priority successfully!!!' % keyword) else: logger.info('Update %s\'s priority failed!!!' % keyword) except Exception as e: logger.error(e.message)
def replenish_glossary(file_path, tag_name, obj_id=None): """根据文件补充词汇表""" try: glossary = MongodbUtil.find_one('shopping', 'glossary', obj_id) category_list = XMLUtil.read_2_list(file_path, tag_name) category_set = XMLUtil.get_obj_set(category_list) for category in category_set: clean_word_list = StringUtil.clean_list( category.get(tag_name).split(':')) for word in clean_word_list.__iter__(): word = word.lower() if not glossary.get('used').__contains__(word): glossary.get('used').append(word) id = MongodbUtil.save('shopping', 'glossary', glossary) if id: message = 'Replenish glossary(id:%s) successfully!!!' % id logger.info(message) except Exception as e: logger.error(e.message)
def import_product_id_2_merchant(merchant_name): """导入广告主下所有产品的id集合""" try: merchant = MongodbUtil.find_one('shopping', 'merchant', {'name': merchant_name}) product_list = MongodbUtil.find('shopping', 'product', {'merchantId': merchant.get('_id')}) product_id_list = list() for product in product_list: product_id_list.append(product.get('_id')) merchant['productIdList'] = product_id_list merchant_id = MongodbUtil.save('shopping', 'merchant', merchant) if merchant_id: logger.info('Import product id list to %s successfully!!!' % merchant_name) else: logger.info('Import product id list to %s failed!!!' % merchant_name) except Exception as e: logger.error(e.message)