From b454653d51444181c97311c6b5c5b3f834de6834 Mon Sep 17 00:00:00 2001 From: simon987 Date: Sun, 15 Apr 2018 20:53:05 -0400 Subject: [PATCH] Search by path --- indexer.py | 4 +- run.py | 13 ++- search.py | 53 ++++++---- templates/search.html | 234 ++++++++++++++++++++++++------------------ thumbnail.py | 4 +- 5 files changed, 181 insertions(+), 127 deletions(-) diff --git a/indexer.py b/indexer.py index 679dc9d..7e36e43 100644 --- a/indexer.py +++ b/indexer.py @@ -68,7 +68,7 @@ class Indexer: "analysis": {"tokenizer": {"my_nGram_tokenizer": {"type": "nGram", "min_gram": 3, "max_gram": 3}}}}, index=self.index_name) self.es.indices.put_settings(body={ - "analysis": {"analyzer": {"path_analyser": {"tokenizer": "path_tokenizer"}}}}, + "analysis": {"analyzer": {"path_analyser": {"tokenizer": "path_tokenizer", "filter": ["lowercase"]}}}}, index=self.index_name) self.es.indices.put_settings(body={ "analysis": {"analyzer": {"my_nGram": {"tokenizer": "my_nGram_tokenizer", "filter": ["lowercase", @@ -80,7 +80,7 @@ class Indexer: "suggest-path": {"type": "completion", "analyzer": "keyword"}, "mime": {"type": "text", "analyzer": "path_analyser", "copy_to": "mime_kw"}, "mime_kw": {"type": "keyword"}, - "directory": {"type": "keyword"}, + "directory": {"type": "short"}, "name": {"analyzer": "my_nGram", "type": "text"}, "album": {"analyzer": "my_nGram", "type": "text"}, "artist": {"analyzer": "my_nGram", "type": "text"}, diff --git a/run.py b/run.py index 52cfed6..76afa9d 100644 --- a/run.py +++ b/run.py @@ -8,7 +8,6 @@ import humanfriendly from search import Search from PIL import Image from io import BytesIO -from collections import defaultdict app = Flask(__name__) app.secret_key = "A very secret key" @@ -18,8 +17,6 @@ tm = TaskManager(storage) search = Search("changeme") - - def get_dir_size(path): size = 0 @@ -33,6 +30,12 @@ def get_dir_size(path): return size +@app.route("/suggest") +def suggest(): + + return json.dumps(search.suggest(request.args.get("prefix"))) + + @app.route("/document/") def document(doc_id): @@ -116,8 +119,10 @@ def search_route(): size_max = request.json["size_max"] mime_types = request.json["mime_types"] must_match = request.json["must_match"] + directories = request.json["directories"] # todo: make sure dir exists and is enabled + path = request.json["path"] - page = search.search(query, size_min, size_max, mime_types, must_match) + page = search.search(query, size_min, size_max, mime_types, must_match, directories, path) return json.dumps(page) diff --git a/search.py b/search.py index fbf5d08..5803e36 100644 --- a/search.py +++ b/search.py @@ -90,13 +90,19 @@ class Search: return mime_map - def search(self, query, size_min, size_max, mime_types, must_match): - - print(query) - print(size_min) - print(size_max) + def search(self, query, size_min, size_max, mime_types, must_match, directories, path): condition = "must" if must_match else "should" + print(directories) + + filters = [ + {"range": {"size": {"gte": size_min, "lte": size_max}}}, + {"terms": {"mime": mime_types}}, + {"terms": {"directory": directories}} + ] + + if path != "": + filters.append({"term": {"path": path}}) page = self.es.search(body={ "query": { @@ -109,10 +115,7 @@ class Search: "operator": "and" } }, - "filter": [ - {"range": {"size": {"gte": size_min, "lte": size_max}}}, - {"terms": {"mime": mime_types}} - ] + "filter": filters } }, "sort": [ @@ -124,16 +127,6 @@ class Search: "name": {"pre_tags": [""], "post_tags": [""]}, } }, - "suggest": { - "path": { - "prefix": query, - "completion": { - "field": "suggest-path", - "skip_duplicates": True, - "size": 4000 - } - } - }, "aggs": { "total_size": {"sum": {"field": "size"}} }, @@ -141,6 +134,28 @@ class Search: return page + def suggest(self, prefix): + + suggestions = self.es.search(body={ + "suggest": { + "path": { + "prefix": prefix, + "completion": { + "field": "suggest-path", + "skip_duplicates": True, + "size": 10000 + } + } + } + }) + + path_list = [] + + for option in suggestions["suggest"]["path"][0]["options"]: + path_list.append(option["_source"]["path"]) + + return path_list + def scroll(self, scroll_id): page = self.es.scroll(scroll_id=scroll_id, scroll="3m") diff --git a/templates/search.html b/templates/search.html index b58116b..ecdb876 100644 --- a/templates/search.html +++ b/templates/search.html @@ -7,6 +7,7 @@ {% block body %}