Skip to content
This repository has been archived by the owner on Nov 15, 2021. It is now read-only.

Commit

Permalink
[#30]implement flask api for forward-search
Browse files Browse the repository at this point in the history
  • Loading branch information
teang1995 committed Nov 10, 2021
1 parent bdf064c commit 310bb5f
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions main_search_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from storyteller.connectors import connect_to_es
from storyteller.elastic.searcher import Searcher
from storyteller.elastic.docs import Story
from flask import Flask
from flask import request
from flask import jsonify

app = Flask(__name__)


@app.route('/', methods=['GET'])
def check():
if request.method == 'GET':
return "connected"

@app.route('/search', methods=['POST'])
def search():
if request.method == 'POST':
req = json.loads(request.data)
wisdom = req['wisdom']
index_name = ",".join(Story.all_indices())
size = 10000
with connect_to_es() as es:
searcher = Searcher(es)
res = searcher(wisdom, index_name, size)

parsed = [
f"index: {hit['_index']}, highlight:{hit['highlight']['sents'][0]}"
for hit in res['hits']['hits']
]
return jsonify(parsed)


if __name__ == "__main__":
app.run(host="0.0.0.0", port="5000", debug=True)

0 comments on commit 310bb5f

Please sign in to comment.