def reindex(id, kind, op_type): target_cls = TARGET_MAPPER.get(kind) if not target_cls: logger.info(f"Reindex error : unexpected kind {kind}") return target = target_cls.get(id) if not target: logger.info( f"Reindex error: unexpected {target.__class__.__name__}<id={id}>") return logger.info(f"Reindex {target.__class__.__name__}<id={id}>") if kind != K_POST: return item = None if op_type == "create": item = Item.add(target) elif op_type == "update": item = Item.update_item(target) elif op_type == "delete": item = Item.get(target.id, target.kind) if item: logger.info(f"Reindex finish {target.__class__.__name__}<id={id}>") else: logger.info(f"Reindex failed {target.__class__.__name__}<id={id}>")
def reindex(id, kind, op_type): """ 重建索引的任务,通过op_type来执行对应的操作 """ target_cls = TARGET_MAPPER.get(kind) if not target_cls: logger.info(f'Reindex Error: Unexpected kind {kind}') return target = target_cls.get(id) if not target: logger.info( f'Reindex Error: Unexpected {target.__class__.__name__}<id={id}>' ) # noqa return logger.info(f'Reindex: {target.__class__.__name__}<id={id}>') if kind != K_POST: # 暂时不支持Post之外的类型 return item = None if op_type == 'create': item = Item.add(target) elif op_type == 'update': item = Item.update_item(target) elif op_type == 'delete': item = Item.get(target.id, target.kind) if item: logger.info(f'Reindex Finished: {target.__class__.__name__}<id={id}>') else: logger.info(f'Reindex Failed: {target.__class__.__name__}<id={id}>')
def reindex(id, kind, op_type): target_cls = TARGET_MAPPER.get(kind) if not target_cls: logger.info(f'Reindex Error: Unexpected kind {kind}') return target = target_cls.get(id) if not target: logger.info( f'Reindex Error: Unexpected {target.__class__.__name__}<id={id}>' ) # noqa return logger.info(f'Reindex: {target.__class__.__name__}<id={id}>') if kind != K_POST: # at present, only support `Post` return item = None if op_type == 'create': item = Item.add(target) elif op_type == 'update': item = Item.update_item(target) elif op_type == 'delete': item = Item.get(target.id, target.kind) # ??? if item: item.delete() if item: logger.info(f'Reindex Finished: {target.__class__.__name__}<id={id}>') else: logger.info(f'Reindex Failed: {target.__class__.__name__}<id={id}>')
def main(): with app.test_request_context(): Item._index.delete(ignore=404) # 删除Elasticsearch索引,销毁全部数据 Item.init() for model in (Post, Tag, PostTag): model.query.delete() # 数据库操作要通过SQLAlchemy,不要直接链接数据库操作 db.session.commit() for site in ('https://imys.net/atom.xml', ): fetch(site)
def main(): with app.test_request_context(): Item._index.delete(ignore=404) # 删除Elasticsearch索引,销毁全部数据 Item.init() for model in (Post, Tag, PostTag): model.query.delete() # 数据库操作要通过SQLAlchemy,不要直接链接数据库操作, 这样才能正确的触发事件(如清3缓存, 清es索引) db.session.commit() for site in ( 'http://www.dongwm.com/atom.xml',): fetch(site)
def main(): with app.test_request_context(): Item._index.delete( ignore=404) # delete Elasticsearch index, remove all data Item.init( ) # create the index and populate the mappings in elasticsearch for model in (Post, Tag, PostTag): model.query.delete( ) # by means of sqlalchemy to maneuver database. Do NOT directly operate on database. db.session.commit() for site in ('http://www.dongwm.com/atom.xml', ): fetch(site)
def search(): # At present, only `Post` is searchable. query = request.args.get('q', '') page = request.args.get('page', default=1, type=int) posts = Item.new_search( query, page) # `Item.new_search()` returns a pagination object. return render_template('search.html', query=query, posts=posts)
def searchPost(self, query=None, page=None, limit=None, _self=False): if not _self: data = request.json query, page, limit = get_params_to_dict(data, ['query', 'page', 'limit']) else: query, page, limit = query, page, limit res = Item.new_search(query, page=page, per_page=limit) total = res.total res = marshal(res.items, PostListSchema()) for i in res: i['value'] = i.get('title') return {'items': res, 'total': total}
def tag(identifier): identifier = identifier.lower() tag = Tag.get_by_name(identifier) if not tag: tag = Tag.get_or_404(identifier) page = request.args.get("page", type=int, default=1) type = request.args.get("type", default="latest") if type == "latest": posts = PostTag.get_post_by_tag(identifier, page) elif type == "hot": posts = Item.get_post_ids_by_tag(tag, page, order_by="hot") posts.items = Post.get_multi(posts.items) else: posts = [] return render_template("tag.html", tag=tag, identifier=identifier, posts=posts, type=type)
def tag(ident): ident = ident.lower() tag = Tag.get_by_name(ident) if not tag: tag = Tag.get(ident) if not tag: abort(404) page = request.args.get('page', default=1, type=int) type = request.args.get('type', default='hot') # hot/latest if type == 'latest': posts = PostTag.get_posts_by_tag(ident, page) elif type == 'hot': posts = Item.get_post_ids_by_tag(ident, page, type) # 从Elasticsearch中查找 # noqa posts.items = Post.get_multi(posts.items) else: # 未知类型 posts = [] return render_template('tag.html', tag=tag, ident=ident, posts=posts, type=type) # 模板能忽略post类型的错误,即使传入posts=[]
def tag(ident): ident = ident.lower() tag = Tag.get_by_name(ident) if not tag: tag = Tag.get(ident) if not tag: abort(404) page = request.args.get('page', default=1, type=int) type = request.args.get('type', default='hot') # hot/latest if type == 'latest': posts = PostTag.get_posts_by_tag(ident, page) # paginate elif type == 'hot': posts = Item.get_post_ids_by_tag(ident, page, type) # via Elasticsearch posts.items = Post.get_multi(posts.items) else: # Unknown type posts = [] return render_template('tag.html', tag=tag, ident=ident, posts=posts, type=type) # `posts` here is pagination object.
def search(): query = request.args.get("q", "") page = request.args.get("page", default=1, type=int) posts = Item.new_search(query, page) tags = Tag.all_tags() return render_template("search.html", query=query, posts=posts, tags=tags)
def search(): query = request.args.get('q', '') page = request.args.get('page', default=1, type=int) posts = Item.new_search(query, page) return render_template('search.html', query=query, posts=posts)
def _create_environ(): db.create_all() Item.init()