feat(backend): add centralized elasticsearch query function
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
from typing import Any, Optional
|
||||
import httpx
|
||||
from .. import config
|
||||
|
||||
|
||||
def search(
|
||||
index: str,
|
||||
query_field: str,
|
||||
query_value: str
|
||||
) -> dict[str, Any]:
|
||||
|
||||
headers = {
|
||||
"Authorization": f"ApiKey {config.settings.elasticsearch_api_key}",
|
||||
"Content-Type": "application/json"
|
||||
}
|
||||
|
||||
payload = {
|
||||
"query": {
|
||||
"query_string": {
|
||||
"default_field": query_field,
|
||||
"query": query_value
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
with httpx.Client() as client:
|
||||
response = client.post(
|
||||
f"{config.settings.elasticsearch_url}/{index}/_search",
|
||||
json=payload,
|
||||
headers=headers
|
||||
)
|
||||
response.raise_for_status()
|
||||
return response.json()
|
||||
Reference in New Issue
Block a user