bsition/backend/elastic.py

50 lines
1.2 KiB
Python

from os import getenv
from elasticsearch import Elasticsearch
def get_client():
return Elasticsearch(getenv("ELASTIC_URL"))
def add_document(index, id, doc):
client = get_client()
client.index(index=index, id=id, document=doc)
def refresh_index(index):
client = get_client()
client.indices.refresh(index=index)
def search(index):
client = get_client()
resp = client.search(
index=index,
query={
# "query_string": {
# "query": "*puan*",
# "default_field": "data"
# }
"bool": {
"must": [
{"query_string": {"query": "*new*", "default_field": "data"}},
# {
# "match": {
# "id": "1",
# }
# }
]
}
},
highlight={"fields": {"data": {}}},
)
print("Got %d hit(s):" % resp["hits"]["total"]["value"])
for hit in resp["hits"]["hits"]:
print(resp["hits"]["total"])
print(resp["hits"])
print(hit["_source"])
print("%(name)s: %(data)s" % hit["_source"])