diff --git a/backend/__init__.py b/backend/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/backend/app.py b/backend/app.py index b3a75b9..0d7dc0f 100644 --- a/backend/app.py +++ b/backend/app.py @@ -1,62 +1,26 @@ -from datetime import datetime from flask import Flask, jsonify, request -from elasticsearch_dsl import Document, Date, Integer, Keyword, Text, FacetedSearch, TermsFacet, DateHistogramFacet +from elasticsearch_dsl import Search from elasticsearch_dsl.connections import connections from tqdm import tqdm +from beeprint import pp import csv import click -import re -class Question(Document): - title = Text(analyzer="snowball") - body = Text(analyzer="snowball") - category = Keyword() - date = Date() +from .question import Question +from .question_search import QuestionSearch - class Index: - name = "goeievraag" - - def save(self, **kwargs): - return super(Question, self).save(**kwargs) - - @property - def url(self): - id_ = self.meta.id - if not self.category: - return f"https://www.startpagina.nl/v/vraag/{id_}" - - category = self.category.lower() - category = re.sub("[^A-Za-z ]", "", category).replace(" ", "-") - return f"https://www.startpagina.nl/v/{category}/vraag/{id_}" - - @property - def summary(self, length=128): - if len(self.body) > length: - return self.body[:length - 3] + "..." - - return self.body - - - -class QuestionSearch(FacetedSearch): - doc_types = Question, - fields = "category", "title", "body" - - facets = { - "date_frequency": DateHistogramFacet(field="date", interval="month"), - "category": TermsFacet(field="category") - } app = Flask(__name__) connections.create_connection(hosts=["localhost"]) Question.init() -def round_sigfig(value, figures): - return float(format(value, f".{figures}g")) @app.route("/api/") def index(): + def round_sigfig(value, figures): + return float(format(value, f".{figures}g")) + query = request.args.get("q") categories = request.args.get("categories", None) @@ -65,19 +29,41 @@ def index(): category_list = categories.split(",") facets["category"] = category_list - search = QuestionSearch(query, facets) + search = Search.from_dict({ + "query": { + "query_string": { + "query": query, + }, + }, + "aggregations": { + "category": { + "terms": {"field": "category"}, + }, + }, + }) response = search.execute() - date_facets = [{"timestamp": date.timestamp(), "count": count} - for date, count, _ in response.facets.date_frequency] - category_facets = [{"category": category, "count": round_sigfig(count, 3)} - for category, count, _ in response.facets.category] + #date_facets = [{"timestamp": date.timestamp(), "count": count} + #for date, count, _ in response.facets.date_frequency] + category_facets = [ + {"category": bucket.key, "count": round_sigfig(bucket.doc_count, 3)} + for bucket in response.aggregations.category.buckets + ] - results = [{"id": hit.meta.id, "score": hit.meta.score, "title": hit.title, - "body": hit.summary, "category": hit.category, - "date": hit.date, "url": hit.url} - for hit in response] + date_facets = [] + + results = [] + for hit in response: + summary = Question.summary(hit) + url = Question.url(hit) + + results.append({ + "id": hit.meta.id, "score": hit.meta.score, + "title": hit.title, "body": summary, + "category": hit.category, "date": hit.date, + "url": url, + }) return jsonify( facets={"months": date_facets, "categories": category_facets}, diff --git a/backend/question.py b/backend/question.py new file mode 100644 index 0000000..762f3ca --- /dev/null +++ b/backend/question.py @@ -0,0 +1,24 @@ +from elasticsearch_dsl import Document, Date, Keyword, Text + + +class Question(Document): + title = Text(analyzer="snowball") + body = Text(analyzer="snowball") + category = Keyword() + date = Date() + + class Index: + name = "goeievraag" + + def save(self, **kwargs): + return super(Question, self).save(**kwargs) + + def url(self): + id_ = self.meta.id + return f"https://www.startpagina.nl/v/vraag/{id_}/" + + def summary(self, length=128): + if len(self.body) > length: + return self.body[:length - 3] + "..." + + return self.body diff --git a/backend/question_search.py b/backend/question_search.py new file mode 100644 index 0000000..e385e9b --- /dev/null +++ b/backend/question_search.py @@ -0,0 +1,12 @@ +from elasticsearch_dsl import FacetedSearch, TermsFacet, DateHistogramFacet +from .question import Question + + +class QuestionSearch(FacetedSearch): + doc_types = Question, + fields = "title", "body" + + facets = { + "date_frequency": DateHistogramFacet(field="date", interval="month"), + "category": TermsFacet(field="category"), + } diff --git a/frontend/index.html b/frontend/index.html index 308ce2a..fe69e1e 100644 --- a/frontend/index.html +++ b/frontend/index.html @@ -3,7 +3,7 @@ - zoekmachine + Goeievraagle
diff --git a/frontend/src/App.vue b/frontend/src/App.vue index 6308a11..5791f76 100644 --- a/frontend/src/App.vue +++ b/frontend/src/App.vue @@ -1,6 +1,6 @@ @@ -14,6 +14,10 @@ body { font-size: 13px; } +a { + text-decoration: none; +} + #app { font-family: Arial, sans-serif; color: #2c3e50; diff --git a/frontend/src/components/Index.vue b/frontend/src/components/Index.vue index ee3c4a4..2377dee 100644 --- a/frontend/src/components/Index.vue +++ b/frontend/src/components/Index.vue @@ -4,15 +4,33 @@
- +
- - + +
+ + diff --git a/frontend/src/components/Results.vue b/frontend/src/components/Results.vue index 79b561a..bb4ea3d 100644 --- a/frontend/src/components/Results.vue +++ b/frontend/src/components/Results.vue @@ -1,202 +1,25 @@ - - diff --git a/frontend/src/components/TopBar.vue b/frontend/src/components/TopBar.vue new file mode 100644 index 0000000..3d9f1d5 --- /dev/null +++ b/frontend/src/components/TopBar.vue @@ -0,0 +1,44 @@ + + + + + diff --git a/frontend/src/router/index.js b/frontend/src/router/index.js index aab2dd4..021a6b0 100644 --- a/frontend/src/router/index.js +++ b/frontend/src/router/index.js @@ -7,6 +7,7 @@ import Results from "@/components/Results"; Vue.use(Router); export default new Router({ + mode: "history", routes: [ { path: "/search",