From ba114cfa34043ba8c79686aa862279c0e658197f Mon Sep 17 00:00:00 2001 From: simon987 Date: Sun, 15 Apr 2018 16:10:15 -0400 Subject: [PATCH] Mime select server-side and must/should toggle for search query --- run.py | 40 +++++--------- search.py | 60 +++++++++++++++++---- templates/search.html | 122 ++++++++++++++++++++++++++++++------------ 3 files changed, 152 insertions(+), 70 deletions(-) diff --git a/run.py b/run.py index 016d9c1..52cfed6 100644 --- a/run.py +++ b/run.py @@ -64,7 +64,7 @@ def download(doc_id): extension = "" if doc["extension"] is None or doc["extension"] == "" else "." + doc["extension"] full_path = os.path.join(directory.path, doc["path"], doc["name"] + extension) - return send_file(full_path) + return send_file(full_path, mimetype=doc["mime"]) @app.route("/thumb/") @@ -96,31 +96,9 @@ def thumb(doc_id): @app.route("/") def search_page(): - mime_map = defaultdict(list) - mime_list = [] - mime_types = search.get_mime_types() + mime_map = search.get_mime_map() - for mime in mime_types: - splited_mime = os.path.split(mime["key"]) - mime_map[splited_mime[0]].append(splited_mime[1]) - - for mime in mime_map: - category = dict() - category["text"] = mime - - children = [] - for m in mime_map[mime]: - child = dict() - child["text"] = m - child["id"] = mime + "/" + m - children.append(child) - - if len(children) > 0: - category["children"] = children - - mime_list.append(category) - - return render_template("search.html", directories=storage.dirs(), mime_list=mime_list) + return render_template("search.html", directories=storage.dirs(), mime_map=mime_map) @app.route("/list") @@ -128,12 +106,18 @@ def search_liste_page(): return render_template("searchList.html") -@app.route("/search") +@app.route("/search", methods=['POST']) def search_route(): - query = request.args.get("q") + query = request.json["q"] query = "" if query is None else query - page = search.search(query) + + size_min = request.json["size_min"] + size_max = request.json["size_max"] + mime_types = request.json["mime_types"] + must_match = request.json["must_match"] + + page = search.search(query, size_min, size_max, mime_types, must_match) return json.dumps(page) diff --git a/search.py b/search.py index f9e5da6..fbf5d08 100644 --- a/search.py +++ b/search.py @@ -1,5 +1,5 @@ import json - +import os import elasticsearch import requests from elasticsearch import helpers @@ -56,23 +56,65 @@ class Search: query = self.es.search(body={ "aggs": { "mimeTypes": { - "terms": {"field": "mime_kw"} + "terms": { + "field": "mime_kw", + "size": 10000 + } } } }) return query["aggregations"]["mimeTypes"]["buckets"] - def search(self, query): + def get_mime_map(self): + + mime_map = [] + + for mime in self.get_mime_types(): + splited_mime = os.path.split(mime["key"]) + + child = dict() + child["text"] = splited_mime[1] + " (" + str(mime["doc_count"]) + ")" + child["id"] = mime["key"] + + mime_category_exists = False + + for category in mime_map: + if category["text"] == splited_mime[0]: + category["children"].append(child) + mime_category_exists = True + break + + if not mime_category_exists: + mime_map.append({"text": splited_mime[0], "children": [child]}) + + return mime_map + + def search(self, query, size_min, size_max, mime_types, must_match): print(query) + print(size_min) + print(size_max) - page = self.es.search(body={"query": - {"multi_match": { - "query": query, - "fields": ["name", "content", "album", "artist", "title", "genre", "album_artist"], - "operator": "and" - }}, + condition = "must" if must_match else "should" + + page = self.es.search(body={ + "query": { + "bool": { + condition: { + "multi_match": { + "query": query, + "fields": ["name", "content", "album", "artist", "title", "genre", + "album_artist"], + "operator": "and" + } + }, + "filter": [ + {"range": {"size": {"gte": size_min, "lte": size_max}}}, + {"terms": {"mime": mime_types}} + ] + } + }, "sort": [ "_score" ], diff --git a/templates/search.html b/templates/search.html index e7a1d60..b58116b 100644 --- a/templates/search.html +++ b/templates/search.html @@ -143,6 +143,12 @@ overflow: auto; } + .btn-xs { + padding: .1rem .3rem; + font-size: .875rem; + border-radius: .2rem; + } +
@@ -153,7 +159,13 @@
-
+
+
+
+ Must match  + +
+
@@ -172,7 +184,9 @@
- + + +
@@ -183,14 +197,20 @@ selection: { mode: 'checkbox' }, - data: {{ mime_list | tojson }} + data: {{ mime_map | tojson }} }); new InspireTreeDOM(tree, { target: '.tree' }); //Select all - tree.select() + tree.select(); + + tree.on("node.click", function(event, node, handler) { + event.preventTreeDefault(); + handler(); + searchQueued = true; + })
@@ -223,9 +243,28 @@