104 lines
3.6 KiB
Markdown
104 lines
3.6 KiB
Markdown
# 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`:
|
|
|
|
```json
|
|
{
|
|
"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
|
|
|
|
```bash
|
|
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`):
|
|
|
|
```bash
|
|
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:
|
|
|
|
```bash
|
|
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
|
|
```
|
|
|
|
DFG (keyword is the `fach` subject classification, potentially several per
|
|
project, project id in the `project_id` field). `--text-field` takes a dotted
|
|
path into the `subject_classifications` array of objects:
|
|
|
|
```bash
|
|
python map_project_topics.py \
|
|
--source-index dfg_projects_2.0 \
|
|
--text-field subject_classifications.fach \
|
|
--project-id-field project_id \
|
|
--output-index projects_dfg_openalex_topics
|
|
```
|
|
|
|
## 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.
|
|
- **Multi-valued text fields**: a dotted `--text-field` (e.g.
|
|
`subject_classifications.fach`) descends into arrays of objects and produces
|
|
one output document per distinct value. Documents with a single value keep
|
|
the source `_id` as output id; documents with several values get
|
|
`<source_id>::<sha1(value)[:12]>` per value, so resumability works per
|
|
(project, keyword) pair.
|
|
- **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-run` matches without creating/writing the output index, `--output
|
|
file.json` writes a local JSON backup, `--max-docs N` limits the scan for
|
|
test runs, `--top-k` sets matches per keyword (default 3).
|