diff --git a/service/scripts/jobs-image/README.md b/service/scripts/jobs-image/README.md index e3cb74e3..dfe8da7e 100644 --- a/service/scripts/jobs-image/README.md +++ b/service/scripts/jobs-image/README.md @@ -72,7 +72,6 @@ and bakes its script as the `ENTRYPOINT`. Most jobs then run with no `command` override — the job definition just points `image` at the matching tag. Where several jobs share one script (the demos), they point at the same image and differ only by their `command`. -here every script ## Images @@ -81,7 +80,7 @@ here every script | News-Crawler | `jobs/news-crawl` | `run_pipeline.py` | scrapy<2.13, Twisted<24.7; uploads to `NEWS_UPLOAD_INDEX` | | Bund-Crawler | `jobs/bund-crawl` | `gpc.data.py --max-rows 200` | headless Chrome; uploads to `BUND_UPLOAD_INDEX`; capped, full export is ~300k rows | | Project-Topic-Mapping | `jobs/project-topics` | `map_project_topics --output-index docker-job_test` | reads projects_bund_v1; needs host backend | -| News-Topic-Mapping | `jobs/news-topics` | `map_news_topics --output-index docker-job_test` | bundles both topic_mapper pkgs; needs host backend | +| News-Topic-Mapping | `jobs/news-topics` | `map_news_topics --output-index aktuell_openalex_topics` | bundles both topic_mapper pkgs; needs host backend | | News-Keyword-Extraction | `jobs/news-keywords` | `sync.py --update-keywords --target-index docker-job_test` | ES + LLM creds; writes to test index | | Demo-Job A / B / C | `jobs/demo` | `demo.py` (seconds via command: `20` / `25` / `30`) | no dependencies; one image, three job rows | @@ -105,7 +104,7 @@ docker build -f service/scripts/jobs-image/news-crawl/Dockerfile \ docker push docker.nes.uni-due.de/inse/jobs/news-crawl:latest ``` -Each job's Dockerfile header lists its own build command. +Each image's Dockerfile header lists its own build command. ## Running locally without pushing @@ -129,9 +128,10 @@ Because every tag lives under the `docker.nes.uni-due.de` registry host, the backend treats these as registry images and injects the INSE Elasticsearch / LLM credentials automatically (plus any per-job `env`). -No image writes to a production index by default: the crawlers upload into the +Most images write to the test index by default: the crawlers upload into the index named by `NEWS_UPLOAD_INDEX` / `BUND_UPLOAD_INDEX` (both baked to -`docker-job_test` in their Dockerfile), and the mappers and the keyword +`docker-job_test` in their Dockerfile), and the project mapper and the keyword extraction write to `docker-job_test` via `--output-index` / `--target-index`. +The exception is News-Topic-Mapping, which writes to `aktuell_openalex_topics`. To not contact Elasticsearch at all, pass `--skip-upload` (crawlers) or `--dry-run` (mappers) through the job's command field. diff --git a/service/scripts/jobs-image/news-topics/Dockerfile b/service/scripts/jobs-image/news-topics/Dockerfile index 0c14852e..916a9d82 100644 --- a/service/scripts/jobs-image/news-topics/Dockerfile +++ b/service/scripts/jobs-image/news-topics/Dockerfile @@ -15,10 +15,10 @@ RUN pip install --no-cache-dir -r requirements.txt COPY service/scripts/news/topic_mapper/ ./service/scripts/news/topic_mapper/ COPY service/scripts/projects/topic_mapper/ ./service/scripts/projects/topic_mapper/ -# Writes topic mappings into docker-job_test. Only maps (article, keyword) pairs -# not already present in the output index; pass --overwrite to re-map all. +# Writes topic mappings into aktuell_openalex_topics. Only maps (article, keyword) +# pairs not already present in the output index; pass --overwrite to re-map all. # Uncapped: limit a run by passing --max-docs N through the job's command field. # host.docker.internal reaches the host backend for vector search. ENTRYPOINT ["python", "-m", "service.scripts.news.topic_mapper.map_news_topics", \ - "--output-index", "docker-job_test", \ + "--output-index", "aktuell_openalex_topics", \ "--api-base-url", "http://host.docker.internal:8000"] diff --git a/service/scripts/news/topic_mapper/README.md b/service/scripts/news/topic_mapper/README.md index 213df178..048058ec 100644 --- a/service/scripts/news/topic_mapper/README.md +++ b/service/scripts/news/topic_mapper/README.md @@ -74,6 +74,7 @@ run retries them. ## As a background job -Registered in `background-jobs/entrypoint.sh` as `news-topics`, writing into -the test index `docker-job_test` and capped at a few articles. Both are -overridable via `NEWS_TOPICS_OUTPUT_INDEX` and `NEWS_TOPICS_MAX_DOCS`. +Packaged as the `news-topics` registry image (see +`service/scripts/jobs-image/news-topics/Dockerfile`), writing into +`aktuell_openalex_topics`. The run is uncapped; limit a run by passing +`--max-docs N` through the job's command field. diff --git a/service/src/main/inse/news/logic.py b/service/src/main/inse/news/logic.py index 42eedb57..edb9197f 100644 --- a/service/src/main/inse/news/logic.py +++ b/service/src/main/inse/news/logic.py @@ -17,11 +17,14 @@ from .model import MatchedTopic, MentionedPerson, NewsArticle, NewsArticleDetail logger = logging.getLogger(__name__) -TOPIC_LIMIT = 4 -TOPIC_QUERY_CONTENT_CHARS = 300 TOPIC_NUM_CANDIDATES = 100 TOPIC_SIMILARITY = 0.5 +TOPICS_INDEX = "aktuell_openalex_topics" +TOPICS_INDEX_MAX_PAIRS = 100 + +_SESSION_TOPIC_CACHE: dict[str, list] = {} + KEYWORD_LIMIT = 6 KEYWORD_CONTENT_CHARS = 4000 KEYWORD_SYSTEM_PROMPT = """\ @@ -251,9 +254,7 @@ async def get_news_by_id(news_id: str) -> NewsArticleDetail: source = response.get("_source", {}) - relevant, authors, cached_keywords, cached_topics, postproc_id = await _get_postproc_labels( - news_id - ) + relevant, authors, cached_keywords, postproc_id = await _get_postproc_labels(news_id) keywords, postproc_id = await _get_article_keywords( news_id, postproc_id, @@ -261,7 +262,7 @@ async def get_news_by_id(news_id: str) -> NewsArticleDetail: source.get("maintitle"), source.get("full_content"), ) - topics = await _get_article_topics(news_id, postproc_id, cached_topics, keywords) + topics = await _get_article_topics(news_id, keywords) return NewsArticleDetail( id=response.get("_id"), @@ -306,23 +307,58 @@ async def _get_article_keywords( return keywords, postproc_id -async def _get_article_topics( - news_id: str, - postproc_id: str | None, - cached_topics: list, - keywords: list, -) -> list: - if cached_topics: - return [MatchedTopic(**topic) for topic in cached_topics] +async def _get_article_topics(news_id: str, keywords: list) -> list: + indexed = await get_article_topics_from_index(news_id) + if indexed: + return indexed + + if news_id in _SESSION_TOPIC_CACHE: + return _SESSION_TOPIC_CACHE[news_id] topics = await _match_topics_for_keywords(keywords) if topics: - await _store_topics(news_id, postproc_id, topics) + _SESSION_TOPIC_CACHE[news_id] = topics return topics +async def get_article_topics_from_index(news_id: str) -> list: + try: + response = await elasticsearch.search( + index=TOPICS_INDEX, + query={"term": {"news_id.keyword": news_id}}, + source=["keyword", "topic_matches"], + size=TOPICS_INDEX_MAX_PAIRS, + cluster="inse", + ) + except Exception: + logger.exception(f"Topic-index lookup for {news_id} failed.") + return [] + + matched = [] + seen_ids = set() + for hit in response.get("hits", {}).get("hits", []): + source = hit.get("_source", {}) + keyword = source.get("keyword") + for match in source.get("topic_matches") or []: + topic_id = match.get("topic_id") + if not topic_id or topic_id in seen_ids: + continue + seen_ids.add(topic_id) + matched.append( + MatchedTopic( + id=topic_id, + display_name=match.get("topic_display_name"), + subfield_name=match.get("subfield"), + field_name=match.get("field"), + domain_name=match.get("domain"), + source_keyword=keyword, + ) + ) + return matched + + def _parse_keywords(raw: str) -> list: raw = (raw or "").strip() @@ -372,28 +408,6 @@ async def _store_keywords(news_id: str, postproc_id: str | None, keywords: list) return postproc_id -async def _store_topics(news_id: str, postproc_id: str | None, topics: list) -> None: - documents = [topic.model_dump() for topic in topics] - - try: - if postproc_id: - await elasticsearch.update( - index="aktuell_postproc", - doc_id=postproc_id, - doc={"topics": documents}, - cluster="inse", - ) - else: - await elasticsearch.index_doc( - index="aktuell_postproc", - doc_id=news_id, - document={"news_id": news_id, "topics": documents}, - cluster="inse", - ) - except Exception: - logger.exception(f"Failed to cache topics for {news_id}.") - - async def _match_topics_for_keywords(keywords: list) -> list: if not keywords: return [] @@ -475,31 +489,30 @@ async def _build_mentioned_persons(author_names: list) -> list: return ude_persons + other_persons -async def _get_postproc_labels(news_id: str) -> tuple[bool | None, list, list, list, str | None]: +async def _get_postproc_labels(news_id: str) -> tuple[bool | None, list, list, str | None]: try: response = await elasticsearch.search( index="aktuell_postproc", query={"term": {"news_id.keyword": news_id}}, - source=["relevant", "authors", "keywords", "topics"], + source=["relevant", "authors", "keywords"], size=1, cluster="inse", ) except Exception: logger.exception(f"Postproc-Lookup for {news_id} failed.") - return None, [], [], [], None + return None, [], [], None hits = response.get("hits", {}).get("hits", []) if not hits: - return None, [], [], [], None + return None, [], [], None hit = hits[0] postproc_source = hit.get("_source", {}) relevant = bool(postproc_source.get("relevant", 0)) authors = postproc_source.get("authors") or [] keywords = postproc_source.get("keywords") or [] - topics = postproc_source.get("topics") or [] - return relevant, authors, keywords, topics, hit.get("_id") + return relevant, authors, keywords, hit.get("_id") async def _run_search(query: dict, source: list, size: int, offset: int, sort, highlight: dict = None) -> dict: