2.8 KiB
Project → OpenAlex topic mapping
Self-contained script that maps a keyword field of a project index to OpenAlex
topics via the backend's /api/topics/vector-search endpoint, and persists one
document per (project, keyword) pair into an output index with the explicit
mapping from mapping.py:
{
"project_id": "...",
"keyword": "<value of the source text field>",
"source_index": "...",
"source_field": "...",
"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.
Setup
pip install -r requirements.txt
Credentials are read from service/src/main/inse/.env by default (override
with --env-file or plain environment variables): ELASTICSEARCH_INSE_URL /
ELASTICSEARCH_INSE_API_KEY for the inse cluster, ELASTICSEARCH_URL /
ELASTICSEARCH_API_KEY for nes. The backend must be running (e.g.
./gradlew serve, default http://localhost:8000, override with
--api-base-url).
Runs
Bund (keyword is the Leistungsplansystematik full text, one per project,
project id = document _id):
python map_project_topics.py \
--source-index projects_bund_v1 \
--text-field Leistungsplansystematik_fullname \
--output-index projects_bund_openalex_topics
EU EuroSciVoc (keyword is the title term, several per project, project id in
the projectID field) — all three framework programmes into one output index:
for idx in cordis_eu_fp7_euroscivoc cordis_eu_horizon_2020_euroscivoc cordis_eu_horizon_europe_euroscivoc; do
python map_project_topics.py \
--source-index "$idx" \
--text-field title \
--project-id-field projectID \
--output-index projects_eu_openalex_topics
done
Behaviour
- Resumable: source documents whose id already exists in the output index
are skipped; the coverage is logged at startup (
Coverage: X/Y already in index, Z still to be processed). Just rerun after an interruption. - Incremental: each unique keyword is searched once, results are bulk-indexed chunk by chunk with progress logs.
- Fail-fast: exits immediately with a clear message if the backend or the Elasticsearch credentials are missing.
- Keywords whose search failed are not persisted (and are retried on the next run); a warning at the end tells how many documents remain.
--dry-runmatches without creating/writing the output index,--output file.jsonwrites a local JSON backup,--max-docs Nlimits the scan for test runs,--top-ksets matches per keyword (default 3).