Files
kallweit e5283543a1
LocationBasedServices/smartguide/pipeline/head This commit looks good
feat(news): store matched OpenAlex topics in 'aktuell_openalex_topics' index
2026-07-25 13:48:57 +02:00

81 lines
2.7 KiB
Markdown

# News → OpenAlex topic mapping
Batch counterpart to `news.logic._match_topics_for_keywords`, which matches an
article's keywords on every detail request without persisting the result. This
script maps the LLM-extracted `keywords` of the enriched articles in
`aktuell_postproc` to OpenAlex topics via the backend's
`/api/topics/vector-search` endpoint, and persists one document per
(article, keyword) pair into an output index with the explicit mapping from
`news_mapping.py`:
```json
{
"news_id": "...",
"keyword": "<one of the article's keywords>",
"source_index": "aktuell_postproc",
"source_field": "keywords",
"topic_matches": [
{
"rank": 1,
"topic_id": "...",
"topic_display_name": "...",
"subfield": "...",
"field": "...",
"domain": "...",
"match_score": null
}
],
"match_method": "sentence_transformer",
"model_name": "...",
"built_at": "..."
}
```
`match_score` is `null` because the endpoint does not expose Elasticsearch
scores; `rank` preserves the search result order.
This script only ever **reads** `aktuell_postproc` and writes into the separate test index. Caching the topics back into `aktuell_postproc` is done by the news detail endpoint (`news/logic.py`), not here.
Keywords are **not** extracted here. `background-jobs/app/sync.py` (job `news`)
writes them into `aktuell_postproc` first, the same way the bund projects
already carry their keyword field before `map_project_topics.py` runs.
The Elasticsearch plumbing (point-in-time scan, batched topic search, bulk
indexing, retries) is imported from
`service/scripts/projects/topic_mapper/map_project_topics.py` rather than
duplicated — that directory must stay next to this one.
## Setup
```bash
pip install -r requirements.txt
```
Credentials are read from `service/src/main/inse/.env` by default (override
with `--env-file`), and the backend must be running (e.g. `./gradlew serve`)
because the topic search goes through its API.
## Usage
```bash
python map_news_topics.py --output-index docker-job_test
```
Test run against a few articles without writing anything:
```bash
python map_news_topics.py --output-index docker-job_test --dry-run --max-docs 50
```
Runs are resumable: (article, keyword) pairs already present in the output
index are skipped, and each unique keyword is searched only once across all
articles that use it. Pairs whose search failed are left unwritten so the next
run retries them.
## As a background job
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.