legacy search
This commit is contained in:
64
fet2020/search/legacy.py
Normal file
64
fet2020/search/legacy.py
Normal file
@@ -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)
|
||||
@@ -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 %}
|
||||
<div class="text-lg text-gray-800 dark:text-gray-200 text-center my-8">
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
<a class="flex gap-x-2 rounded group" href="{{ result.get_absolute_url }}">
|
||||
<a class="flex gap-x-2 rounded group" href="https://{{ result.url }}">
|
||||
<div class="hidden sm:block flex-none rounded aspect-video w-48 bg-center bg-no-repeat bg-cover bg-scale-100 group-hover:bg-scale-110 transition-all ease-in-out duration-300 bg-gray-300 dark:bg-gray-700" style="background-image: url('{{ result.imageurl }}');"></div>
|
||||
<article class="flex-grow-0 sm:m-2">
|
||||
<h2 class="line-clamp-1 hover:underline decoration-1 text-gray-800 dark:text-gray-200 sm:font-medium"><i class="fa-solid fa-up-right-from-square mr-2"></i>{{ result.title|truncatechars:50 }}</h2>
|
||||
|
||||
Reference in New Issue
Block a user