add cluster parameter to elasticsearch integration (nes/inse) #135

Merged
tischer merged 1 commits from feat/add_inse_elasticsearch_cluster into dev 2026-06-10 16:34:41 +02:00
3 changed files with 28 additions and 7 deletions
+3
View File
@@ -1,6 +1,9 @@
ELASTICSEARCH_URL=https://project.nes.uni-due.de/elastic/
ELASTICSEARCH_API_KEY=am1wWXBaMEJEVWJoZkE5MnBaUlk6TWlvbW1LRlMzNjdDUHgzYmVkUmI5UQ
ELASTICSEARCH_INSE_URL=https://inse.nes.uni-due.de/elastic/
ELASTICSEARCH_INSE_API_KEY=bG1NSWc1NEJPZ3FnTGZCZXRSSEg6VnJXRFd5SnpSTTNWc1VPT01FMEhMZw==
LLM_API_URL=https://project.nes.uni-due.de/llm2/v1/
LLM_API_KEY=c2d5e7ccf7a4760dd3d19bed2caed9d85ff7437400e0ea82a6d6f912ebc2f212
LLM_MODEL=gemma4-e4b
+2
View File
@@ -47,6 +47,8 @@ class Settings(BaseSettings):
# elasticsearch config
elasticsearch_url: Optional[str] = None
elasticsearch_api_key: Optional[str] = None
elasticsearch_inse_url: Optional[str] = None
elasticsearch_inse_api_key: Optional[str] = None
model_config = SettingsConfigDict(env_file=".env")
@@ -3,6 +3,16 @@ import httpx
from .. import config
def _cluster_credentials(cluster: str) -> tuple[str, str]:
if cluster == "nes":
return config.settings.elasticsearch_url, config.settings.elasticsearch_api_key
elif cluster == "inse":
return config.settings.elasticsearch_inse_url, config.settings.elasticsearch_inse_api_key
else:
raise ValueError(f"Unbekannter Cluster: {cluster!r} (erlaubt: 'nes', 'inse')")
def search(
index: str,
query_field: str = None,
@@ -13,11 +23,14 @@ def search(
offset:int = None,
sort: list = None,
body: dict = None,
track_total_hits = False
track_total_hits = False,
cluster: str = "nes",
) -> dict[str, Any]:
elasticsearch_url, api_key = _cluster_credentials(cluster)
headers = {
"Authorization": f"ApiKey {config.settings.elasticsearch_api_key}",
"Authorization": f"ApiKey {api_key}",
"Content-Type": "application/json"
}
@@ -47,20 +60,25 @@ def search(
with httpx.Client(timeout=httpx.Timeout(30.0)) as client:
response = client.post(
f"{config.settings.elasticsearch_url}/{index}/_search",
f"{elasticsearch_url}/{index}/_search",
json=payload,
headers=headers
)
response.raise_for_status()
return response.json()
def get(
index: str,
doc_id: str,
source: list = None,
cluster: str = "nes",
) -> dict[str, Any]:
elasticsearch_url, api_key = _cluster_credentials(cluster)
headers = {
"Authorization": f"ApiKey {config.settings.elasticsearch_api_key}",
"Authorization": f"ApiKey {api_key}",
"Content-Type": "application/json"
}
@@ -70,11 +88,9 @@ def get(
with httpx.Client() as client:
response = client.get(
f"{config.settings.elasticsearch_url}/{index}/_doc/{doc_id}",
f"{elasticsearch_url}/{index}/_doc/{doc_id}",
params=params,
headers=headers
)
response.raise_for_status()
return response.json()