def search(cls, expression, page, per_page):
ids, total = query_index(cls.__tablename__, expression, page, per_page)
...
when = []
for i in range(len(ids)):
when.append((ids[i], i))
return cls.query.filter(cls.id.in_(ids)).order_by(
db.case(when, value=cls.id)), total