refactor httpdemo 1
This commit is contained in:
@@ -3,66 +3,32 @@ from fastapi import FastAPI
|
|||||||
from fastapi.staticfiles import StaticFiles
|
from fastapi.staticfiles import StaticFiles
|
||||||
from fastapi.templating import Jinja2Templates
|
from fastapi.templating import Jinja2Templates
|
||||||
from fastapi import Request
|
from fastapi import Request
|
||||||
from elasticsearch import Elasticsearch
|
#from elasticsearch import Elasticsearch
|
||||||
import sys
|
import sys
|
||||||
import elastic_transport
|
import elastic_transport
|
||||||
|
from searching import es_search, es_query
|
||||||
ELASTIC_HOST = "http://localhost:9200"
|
import json
|
||||||
client = Elasticsearch(ELASTIC_HOST, verify_certs=False)
|
import yaml
|
||||||
|
|
||||||
app = FastAPI(debug=True)
|
app = FastAPI(debug=True)
|
||||||
|
|
||||||
templates = Jinja2Templates(directory="./httpdemo")
|
templates = Jinja2Templates(directory="./httpdemo")
|
||||||
#app.mount("/", StaticFiles(directory="/"))
|
#app.mount("/", StaticFiles(directory="/"))
|
||||||
|
|
||||||
|
|
||||||
#@app.get("/")
|
|
||||||
#def read_root():
|
|
||||||
# return {"Hello"}
|
|
||||||
|
|
||||||
|
|
||||||
query ={
|
|
||||||
"multi_match":{
|
|
||||||
"query": ELASTIC_QUERY,
|
|
||||||
"fields": ["title^20","title.ngrams^10","text","text.ngrams"],
|
|
||||||
"type": "most_fields"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
highlight = {
|
|
||||||
"fields": {
|
|
||||||
"title": {},
|
|
||||||
"text": {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@app.get("/")
|
@app.get("/")
|
||||||
def serve_home(request: Request, q: str):
|
def serve_home(request: Request, q: str=""):
|
||||||
query= {
|
|
||||||
"bool":{
|
|
||||||
"should": [{"wildcard": {"body": {"value": "*%s*"% q, "case_insensitive": True }}},
|
|
||||||
{"wildcard": {"title": {"value": "*%s*" % q, "case_insensitive": True }}}],
|
|
||||||
"minimum_should_match": 1
|
|
||||||
}}
|
|
||||||
query = {
|
|
||||||
"match": {
|
|
||||||
"body": q
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
resp = client.search(
|
resp = es_search(q)
|
||||||
index = "posts2",
|
query=es_query(q)
|
||||||
size=10,
|
message = "found ...?"
|
||||||
#analyze_wildcard=True,
|
|
||||||
#q="sdf*",
|
|
||||||
query= query
|
|
||||||
)
|
|
||||||
except (elastic_transport.ConnectionError, elastic_transport.ConnectionTimeout) as e:
|
except (elastic_transport.ConnectionError, elastic_transport.ConnectionTimeout) as e:
|
||||||
print(e,sys.stderr)
|
print(e,sys.stderr)
|
||||||
results=[]
|
results=[]
|
||||||
|
resp={}
|
||||||
|
query={}
|
||||||
|
message = f"cannot reach the search server! : %s" % e
|
||||||
else:
|
else:
|
||||||
results=resp["hits"]["hits"]
|
results=resp["hits"]["hits"]
|
||||||
return templates.TemplateResponse("index.html", context= {"request": request,"results": results})
|
|
||||||
|
templates.env.filters["json"]=lambda x: yaml.dump(dict(x))
|
||||||
|
return templates.TemplateResponse("index.html", context= {
|
||||||
|
"request": resp,"results": results,"message": message, "query": query})
|
||||||
@@ -1,10 +1,23 @@
|
|||||||
|
<html>
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
<h1>Hello Index httpdemo</h1>
|
<h1>Suche:</h1>
|
||||||
<p><pre>{{request}}</pre></p>
|
<p><form action="" href="#"><input name="q" /></form></p>
|
||||||
<ul>{% for r in results %}
|
<p><b>Message:</b>{{message}}</p>
|
||||||
<li><a href="{{r['url'}}">
|
|
||||||
{{r["body"]|safe}}
|
<ul style="list-style: none;">
|
||||||
</li></a>
|
{% for r in results %}
|
||||||
|
<li>
|
||||||
|
<a href="https://{{r['_source']['url']}}">
|
||||||
|
{{r["_source"]["title"]|safe }}
|
||||||
|
</a>
|
||||||
|
{% for hh in r["highlight"] %}
|
||||||
|
{% for h in r["highlight"][hh] %}
|
||||||
|
{{ h |safe}} <br />
|
||||||
|
{% endfor %}
|
||||||
|
{% endfor %}
|
||||||
|
</li>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</ul>
|
</ul>
|
||||||
</body>
|
</body>
|
||||||
|
</html>
|
||||||
Reference in New Issue
Block a user