diff --git a/service/src/main/inse/.env b/service/src/main/inse/.env index f2fc4762..2b84a5ea 100644 --- a/service/src/main/inse/.env +++ b/service/src/main/inse/.env @@ -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 \ No newline at end of file diff --git a/service/src/main/inse/config.py b/service/src/main/inse/config.py index 4d83fe03..bfa60cfc 100644 --- a/service/src/main/inse/config.py +++ b/service/src/main/inse/config.py @@ -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") diff --git a/service/src/main/inse/integrations/elasticsearch.py b/service/src/main/inse/integrations/elasticsearch.py index 654cf455..870d833f 100644 --- a/service/src/main/inse/integrations/elasticsearch.py +++ b/service/src/main/inse/integrations/elasticsearch.py @@ -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() - -