rest.py 523 B

12345678910111213141516171819202122
  1. #!/usr/bin/python3
  2. from flask import Flask
  3. from flask import jsonify
  4. from whoosh.qparser import MultifieldParser
  5. from whoosh import index
  6. from . import api
  7. app = Flask(__name__)
  8. ix = index.open_dir("data")
  9. @app.route("/search/<query>")
  10. def search(query):
  11. with ix.searcher() as searcher:
  12. query = MultifieldParser(["title", "description"], ix.schema).parse(query)
  13. results = searcher.search(query, limit=10)
  14. return jsonify([dict(r) for r in results])
  15. if __name__ == '__main__':
  16. app.run()