From ffd9281df73e6d385e5e153214a474d1913a8146 Mon Sep 17 00:00:00 2001 From: Patrick Mayr Date: Thu, 23 Mar 2023 17:47:23 +0000 Subject: [PATCH] legacy search --- fet2020/search/legacy.py | 64 +++++++++++++++++++++++++++++ fet2020/templates/search/index.html | 2 + fet2020/templates/search/post.html | 2 +- 3 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 fet2020/search/legacy.py diff --git a/fet2020/search/legacy.py b/fet2020/search/legacy.py new file mode 100644 index 00000000..0bc3bc5a --- /dev/null +++ b/fet2020/search/legacy.py @@ -0,0 +1,64 @@ +import os +from elasticsearch import Elasticsearch, helpers +import contextlib +import logging +import elasticsearch +ELASTIC_HOST = os.environ.get("ELASTIC_HOST","http://elastic:9200") +ELASTIC_PASSWORD = os.environ.get("ELASTIC_PASSWORD","*l9qNGoojiCC4n9KcZhj") +ELASTIC_QUERY = os.environ.get("ELASTIC_QUERY","Anwesend") +ELASTIC_INDEX = os.environ.get("ELASTIC_INDEX","legacy") + +elasticsearch.logger.setLevel(elasticsearch.logging.WARN) + +# Verbinde mit Client +@contextlib.contextmanager +def es_client(): + logging.debug(f"ELASIC HOST:%s" % ELASTIC_HOST) + client = Elasticsearch(ELASTIC_HOST, verify_certs=False, basic_auth=('elastic', ELASTIC_PASSWORD)) + yield client + client.close() + + +def es_query(query:str): + query ={ + "multi_match":{ + "query": query, + #"fields": ["title^20","text^3"], + "fields": ["title^20","title.ngrams^5","text^3","text.ngrams"], + "type": "most_fields" + } + } + return query +def es_highlight(): + highlight = { + "fields": { + "title": {}, + "text": {} + } + } + return highlight + +class SearchObject: + def get_model_name(self): + return "SearchObject" + def __init__(self, res): + self.title= res.get("_source",{}).get("title","") + self.url= res.get("_source",{}).get("url","") + self.text= res.get("_source",{}).get("text","") + self.raw= res.get("_source",{}).get("raw","") + self.highlight=res.get("highlight",{}).get("title",[""]) + res.get("highlight",{}).get("text",[""]) + +def es_search(query:str): + logging.debug(f"Run Search for legacy :%s" % query) + with es_client() as client: + result = client.search( + index = ELASTIC_INDEX, + size=10, + query= es_query(query), + highlight = es_highlight() + ) + logging.debug(result.keys()) + return [SearchObject(r) for r in result["hits"]["hits"]] + +#for hit in resp["hits"]["hits"]: +# print(hit) \ No newline at end of file diff --git a/fet2020/templates/search/index.html b/fet2020/templates/search/index.html index 16b5b47b..d27876ae 100644 --- a/fet2020/templates/search/index.html +++ b/fet2020/templates/search/index.html @@ -25,6 +25,8 @@ {% include 'search/member.html' %} {% elif result.get_model_name == 'album' %} {% include 'search/album.html' %} + {% elif result.get_model_name == 'SearchObject' %} + {% include 'search/post.html' %} {% endif %} {% empty %}
diff --git a/fet2020/templates/search/post.html b/fet2020/templates/search/post.html index 1ca05843..9a84a320 100644 --- a/fet2020/templates/search/post.html +++ b/fet2020/templates/search/post.html @@ -1,4 +1,4 @@ - +

{{ result.title|truncatechars:50 }}