From 8167ecded52751124aa0a4bfd5b6231d31beae98 Mon Sep 17 00:00:00 2001 From: Sijmen Schoon Date: Fri, 26 Oct 2018 15:15:49 +0200 Subject: [PATCH] Finishing touches before presentation --- backend/app.py | 47 +++++++++++++---- frontend/package.json | 5 +- frontend/src/components/ResultBody.vue | 72 ++++++++++++++++++++------ frontend/src/components/TopBar.vue | 6 +-- frontend/yarn.lock | 43 +++++++++++++++ 5 files changed, 143 insertions(+), 30 deletions(-) diff --git a/backend/app.py b/backend/app.py index 39647e9..2eb84dd 100644 --- a/backend/app.py +++ b/backend/app.py @@ -4,6 +4,7 @@ from elasticsearch_dsl.connections import connections from elasticsearch.exceptions import NotFoundError from tqdm import tqdm from beeprint import pp +from datetime import datetime import csv import click import requests @@ -23,6 +24,7 @@ def index(): query = request.args.get("q") categories = request.args.get("categories", None) + years = request.args.get("years", None) page = int(request.args.get("page", 1)) - 1 search_dict = { @@ -31,6 +33,7 @@ def index(): "bool": { "must": [ {"query_string": {"query": query}}, + {"term": {"dead": False}}, ] } }, @@ -38,28 +41,55 @@ def index(): "category": { "terms": {"field": "category"}, }, + "date": { + "date_histogram": { + "field": "date", "interval": "year", + }, + }, "chips": { "significant_terms": { "field": "body", - "mutual_information": { - "include_negatives": True, - }, + "mutual_information": {"include_negatives": True}, "size": 40, }, } }, } + if categories is not None or years is not None: + search_dict["post_filter"] = {"bool": {"must": []}} + if categories is not None: category_list = categories.split(",") - search_dict["post_filter"] = { + search_dict["post_filter"]["bool"]["must"].append({ "terms": {"category": category_list}, - } + }) + + if years is not None: + year_list = years.split(",") + search_dict["post_filter"]["bool"]["must"].append({ + "bool": { + "should": [ + { + "range": { + "date": { + "gte": f"{year}||/y", + "lte": f"{year}||/y", + "format": "yyyy" + } + } + } for year in year_list if year + ] + } + }) search = Search.from_dict(search_dict) response = search.execute() pp(response.to_dict()) + date_facets = [{"key": datetime.fromtimestamp(bucket.key / 1000).year, + "count": bucket.doc_count} + for bucket in response.aggregations.date.buckets] category_facets = [ {"category": bucket.key, "count": round_sigfig(bucket.doc_count, 3)} for bucket in response.aggregations.category.buckets @@ -68,8 +98,6 @@ def index(): chips = [{"key": bucket.key, "count": bucket.doc_count} for bucket in response.aggregations.chips.buckets] - date_facets = [] - results = [] for hit in response: summary = Question.summary(hit) @@ -87,10 +115,7 @@ def index(): }) return jsonify( - facets={ - "months": date_facets, - "categories": category_facets, - }, + facets={"dates": date_facets, "categories": category_facets}, chips=chips, results=results, hits=round_sigfig(response.hits.total, 4), diff --git a/frontend/package.json b/frontend/package.json index bd4c558..d759c04 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -12,10 +12,13 @@ }, "dependencies": { "babel-plugin-transform-decorators-legacy": "^1.3.5", + "chart.js": "^2.7.3", "vue": "^2.5.2", + "vue-chartjs": "^3.4.0", "vue-class-component": "^6.3.2", "vue-property-decorator": "^7.2.0", - "vue-router": "^3.0.1" + "vue-router": "^3.0.1", + "vuetrend": "^0.3.2" }, "devDependencies": { "autoprefixer": "^7.1.2", diff --git a/frontend/src/components/ResultBody.vue b/frontend/src/components/ResultBody.vue index 1154a86..9d51ae7 100644 --- a/frontend/src/components/ResultBody.vue +++ b/frontend/src/components/ResultBody.vue @@ -1,8 +1,8 @@