first_commit
This commit is contained in:
68
httpdemo/__init__.py
Normal file
68
httpdemo/__init__.py
Normal file
@@ -0,0 +1,68 @@
|
||||
|
||||
from fastapi import FastAPI
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
from fastapi.templating import Jinja2Templates
|
||||
from fastapi import Request
|
||||
from elasticsearch import Elasticsearch
|
||||
import sys
|
||||
import elastic_transport
|
||||
|
||||
ELASTIC_HOST = "http://localhost:9200"
|
||||
client = Elasticsearch(ELASTIC_HOST, verify_certs=False)
|
||||
|
||||
app = FastAPI(debug=True)
|
||||
|
||||
templates = Jinja2Templates(directory="./httpdemo")
|
||||
#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("/")
|
||||
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:
|
||||
resp = client.search(
|
||||
index = "posts2",
|
||||
size=10,
|
||||
#analyze_wildcard=True,
|
||||
#q="sdf*",
|
||||
query= query
|
||||
)
|
||||
except (elastic_transport.ConnectionError, elastic_transport.ConnectionTimeout) as e:
|
||||
print(e,sys.stderr)
|
||||
results=[]
|
||||
else:
|
||||
results=resp["hits"]["hits"]
|
||||
return templates.TemplateResponse("index.html", context= {"request": request,"results": results})
|
||||
10
httpdemo/index.html
Normal file
10
httpdemo/index.html
Normal file
@@ -0,0 +1,10 @@
|
||||
<body>
|
||||
<h1>Hello Index httpdemo</h1>
|
||||
<p><pre>{{request}}</pre></p>
|
||||
<ul>{% for r in results %}
|
||||
<li><a href="{{r['url'}}">
|
||||
{{r["body"]|safe}}
|
||||
</li></a>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
</body>
|
||||
10909
httpdemo/jquery.js
vendored
Normal file
10909
httpdemo/jquery.js
vendored
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user