This repository has been archived by the owner on Nov 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#30]implement flask api for forward-search
- Loading branch information
Showing
1 changed file
with
35 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |