diff --git a/app.py b/app.py index dbbab1e63cd9d7c4dd08eed30a89889c31aab9b8..43580d749e0194a62a8f04d714785129954625f9 100644 --- a/app.py +++ b/app.py @@ -1,45 +1,61 @@ -from flask import Flask, render_template, request, redirect, send_file +from flask import Flask, render_template, request, redirect, send_file, jsonify from elasticsearch_dsl import connections, Search -import os +import os app = Flask(__name__) connections.create_connection(hosts=[os.getenv('ES_HOST', 'localhost')], timeout=20) -@app.route('/search') +@app.route('/test') +def test_page(): + return render_template('index.html') + + +def make_text_response(text): + print(text[:100]) + return jsonify({ + "type": "text", + "text": text + }) + + +@app.route('/search', methods=['POST']) def index(): - term = request.args.get('term') + term = request.form.get('term') + print(term) s = Search(index="en_wiki") \ .query("match", title=term) \ .exclude("match", description="beta") response = s.execute() if len(response) > 0: - return response[0].text + return make_text_response(response[0].text) s = Search(index="en_wiki") \ .query("match_phrase", title=term) \ .exclude("match", description="beta") response = s.execute() if len(response) > 0: - return response[0].text + return make_text_response(response[0].text) s = Search(index="en_wiki") \ .query("match", text=term) \ .exclude("match", description="beta") response = s.execute() if len(response) > 0: - return response[0].text + return make_text_response(response[0].text) s = Search(index="en_wiki") \ .query("match_phrase", text=term) \ .exclude("match", description="beta") response = s.execute() if len(response) > 0: - return response[0].text + return make_text_response(response[0].text) - return "No hits" + return jsonify({ + "type": "miss" + }) if __name__ == '__main__': diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000000000000000000000000000000000000..b2323508d1aae3510610760cf700306d34ec2602 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,15 @@ +<!DOCTYPE HTML> +<html> +<head> + <title>Slide index</title></head> +<body> + +<h1>Query</h1> +<form action="search" method="post"> + <input name="term" placeholder="term"><br> + <input name="context" placeholder="context"><br> + <input type="submit" value="Query"> +</form> + +</body> +</html>