Skip to content
Snippets Groups Projects
Commit 5b4a2a55 authored by felixwelter's avatar felixwelter
Browse files

Switch to new request format

parent 20a6127d
Branches
Tags
No related merge requests found
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 from elasticsearch_dsl import connections, Search
import os import os
...@@ -7,39 +7,55 @@ app = Flask(__name__) ...@@ -7,39 +7,55 @@ app = Flask(__name__)
connections.create_connection(hosts=[os.getenv('ES_HOST', 'localhost')], timeout=20) 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(): def index():
term = request.args.get('term') term = request.form.get('term')
print(term)
s = Search(index="en_wiki") \ s = Search(index="en_wiki") \
.query("match", title=term) \ .query("match", title=term) \
.exclude("match", description="beta") .exclude("match", description="beta")
response = s.execute() response = s.execute()
if len(response) > 0: if len(response) > 0:
return response[0].text return make_text_response(response[0].text)
s = Search(index="en_wiki") \ s = Search(index="en_wiki") \
.query("match_phrase", title=term) \ .query("match_phrase", title=term) \
.exclude("match", description="beta") .exclude("match", description="beta")
response = s.execute() response = s.execute()
if len(response) > 0: if len(response) > 0:
return response[0].text return make_text_response(response[0].text)
s = Search(index="en_wiki") \ s = Search(index="en_wiki") \
.query("match", text=term) \ .query("match", text=term) \
.exclude("match", description="beta") .exclude("match", description="beta")
response = s.execute() response = s.execute()
if len(response) > 0: if len(response) > 0:
return response[0].text return make_text_response(response[0].text)
s = Search(index="en_wiki") \ s = Search(index="en_wiki") \
.query("match_phrase", text=term) \ .query("match_phrase", text=term) \
.exclude("match", description="beta") .exclude("match", description="beta")
response = s.execute() response = s.execute()
if len(response) > 0: 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__': if __name__ == '__main__':
......
<!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>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment