| Title: | OpenAlex Bulk Snapshot Conversion, Indexing, and Record Extraction |
|---|---|
| Description: | Provides tools for working with the OpenAlex bulk snapshot: converting .json.gz files to Parquet format, building ID, DOI and inverted-citation indexes over the resulting corpus, and extracting records by OpenAlex ID or DOI. Also provides an offline view of the citation graph, so the works citing or cited by a given paper can be retrieved without network access. Indexing and extraction are implemented in R over DuckDB. The package is pure R: no compiled code, and no Rust toolchain is needed to install it. |
| Authors: | Rainer M Krug [aut, cre] (ORCID: <https://orcid.org/0000-0002-7490-0066>) |
| Maintainer: | Rainer M Krug <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.3.6 |
| Built: | 2026-11-20 08:30:14 UTC |
| Source: | https://github.com/openalexPro/openalexSnapshot |
Inverts the referenced_works column of the works corpus into an edge set
keyed by the cited work, so that "which works cite X?" becomes a point
lookup instead of a full-corpus scan. This is what makes get_citing()
possible offline: the snapshot's cited_by_api_url is a URL and useless
without network access, but the citing direction is recoverable by
inverting the references every work already carries.
build_citation_index( root_dir = NULL, data_sets = "works", workers = NULL, memory_limit = NULL, retry_memory_limit = "12GB", temp_dir = NULL, block_size = 1e+07, batch_bytes = 1e+09, compression = "zstd", overwrite = FALSE, verbose = TRUE, corpus_dir = NULL )build_citation_index( root_dir = NULL, data_sets = "works", workers = NULL, memory_limit = NULL, retry_memory_limit = "12GB", temp_dir = NULL, block_size = 1e+07, batch_bytes = 1e+09, compression = "zstd", overwrite = FALSE, verbose = TRUE, corpus_dir = NULL )
root_dir |
Root directory containing a |
data_sets |
Datasets to index. Only works carry |
workers |
Number of parallel workers. |
memory_limit |
DuckDB memory limit per worker, e.g. |
retry_memory_limit |
Memory limit for the sequential retry of batches
that exceeded |
temp_dir |
Directory for Stage-1 shards and DuckDB spill. Defaults to a
subdirectory of The default matters. Spilling beside the index puts those writes on the same device the corpus is being read from, and on an external USB SSD that measured 8.18 s/batch versus 0.76 s/batch – a 10.8x difference from this setting alone. Override it only to point at a different fast disk, or if the default lacks room: peak usage is roughly the size of the finished index plus its transient shards. |
block_size |
Width of a |
batch_bytes |
Approximate bytes of source parquet per Stage-1 batch.
Batching is by byte budget over the flat file list rather than by hive
partition, because the corpus is extremely skewed – one The default of 1 GB is measured, not guessed. Unnesting a batch and
writing it across hundreds of partitions costs far more memory than the
few MB it emits, so the batch size governs spill. On the dense files of
the real corpus, at |
compression |
Parquet compression codec. |
overwrite |
Rebuild an existing index. |
verbose |
Print progress. |
corpus_dir |
Explicit path to a single dataset Parquet directory. |
get_cited() does not need this index – the works a paper references
are simply its own referenced_works – so only one index is built, not a
pair.
Each part file holds:
The cited work's OpenAlex ID, long form VARCHAR
The citing work's OpenAlex ID, long form VARCHAR
sorted by (cited_id, citing_id), matching the long-form convention of
<dataset>_id_idx/ and <dataset>_doi_idx.parquet so every index in the
family stores IDs the same way. Note this costs real space at three
billion rows: citing_id is effectively random within a block and so does
not dictionary-compress, unlike the sorted cited_id.
Invisibly, the path to the index directory (corpus_dir mode) or
root_dir.
Like <dataset>_id_idx/, this index is a hive-partitioned directory
(only <dataset>_doi_idx.parquet remains a single file):
works_cite_idx/ _index_meta.parquet cited_block=0/part-0.parquet cited_block=1/part-0.parquet ...
The reason is scale. The full OpenAlex works corpus holds roughly three billion reference edges; a single sorted file would need a global sort of that many rows, and its footer alone would cost tens of megabytes to parse on every query. Partitioning by a coarse block of the cited ID keeps each part small enough to sort in memory and lets a lookup open only the handful of parts it needs.
_index_meta.parquet is written last; its presence is what marks the
index complete, and it records the block_size the query side must use.
Builds a <dataset>_id_idx/ index from the Parquet corpus produced
by the snapshot download and conversion pipeline, enabling fast record retrieval by OpenAlex ID
using lookup_by_id().
build_corpus_index( root_dir = NULL, data_sets = NULL, workers = NULL, memory_limit = NULL, temp_dir = NULL, batch_bytes = 1e+09, block_size = NULL, overwrite = FALSE, verbose = TRUE, corpus_dir = NULL, backend = c("auto", "r", "rust") )build_corpus_index( root_dir = NULL, data_sets = NULL, workers = NULL, memory_limit = NULL, temp_dir = NULL, batch_bytes = 1e+09, block_size = NULL, overwrite = FALSE, verbose = TRUE, corpus_dir = NULL, backend = c("auto", "r", "rust") )
root_dir |
Root directory containing a |
data_sets |
Character vector of dataset names to index (e.g.
|
workers |
Number of parallel workers for Stage 1 indexing. Default is
|
memory_limit |
DuckDB memory limit (e.g., |
temp_dir |
Directory for Stage-1 shards and DuckDB spill. Defaults to a
subdirectory of The default matters. Spilling beside the index puts those writes on the same device the corpus is being read from, and on an external USB SSD that measured 8.18 s/batch versus 0.76 s/batch – a 10.8x difference from this setting alone. Override it only to point at a different fast disk, or if the default lacks room: peak usage is roughly the size of the finished index plus its transient shards. |
batch_bytes |
Approximate bytes of source parquet per Stage-1 batch. |
block_size |
Width of an A fixed width does not suit every dataset, because entity id ranges differ
by orders of magnitude. At |
overwrite |
If |
verbose |
Print progress messages. Default is |
corpus_dir |
Explicit path to a single dataset Parquet directory (e.g.
|
backend |
Retained only so that existing calls passing
|
The function uses a two-stage approach:
Index each Parquet file individually (bounded memory, parallel, with resume support).
Combine the per-file shard indexes into a single Parquet index.
Paths can be supplied as a single root_dir (which iterates over all
requested data_sets) or as an explicit corpus_dir pointing to a single
dataset directory.
A hive-partitioned directory, not a single file:
works_id_idx/ _index_meta.parquet id_block=0/part-0.parquet ...
with id_block = floor(numeric_id / block_size), each part sorted by id,
holding id (long form), parquet_file (relative to the parquet root) and
file_row_number (0-indexed).
A single file measured 3,992 row groups whose footer cost 0.192 s to parse before reading any data, on every query, and needed a global sort of 492M rows to build. Partitioning removes both: blocks sort independently, and a lookup opens only the blocks its ids fall in.
_index_meta.parquet is written last; its presence marks the index
complete, and it records the block_size the query side must recompute.
When corpus_dir is provided, invisibly returns the path to the
created index file. When root_dir is used, invisibly returns root_dir.
lookup_by_id() for ID-based record retrieval,
build_citation_index() and build_doi_index().
Builds a <dataset>_doi_idx.parquet index mapping normalised DOIs to
OpenAlex IDs and to their location in the Parquet corpus, so DOIs can be
resolved without network access.
build_doi_index( root_dir = NULL, data_sets = "works", workers = NULL, memory_limit = NULL, temp_dir = NULL, batch_bytes = 8e+09, compression = "zstd", overwrite = FALSE, verbose = TRUE, corpus_dir = NULL )build_doi_index( root_dir = NULL, data_sets = "works", workers = NULL, memory_limit = NULL, temp_dir = NULL, batch_bytes = 8e+09, compression = "zstd", overwrite = FALSE, verbose = TRUE, corpus_dir = NULL )
root_dir |
Root directory containing a |
data_sets |
Datasets to index. Only |
workers |
Number of parallel workers for Stage 1. |
memory_limit |
DuckDB memory limit, e.g. |
temp_dir |
Directory for Stage-1 shards and DuckDB spill. Defaults to a
subdirectory of The default matters. Spilling beside the index puts those writes on the same device the corpus is being read from, and on an external USB SSD that measured 8.18 s/batch versus 0.76 s/batch – a 10.8x difference from this setting alone. Override it only to point at a different fast disk, or if the default lacks room: peak usage is roughly the size of the finished index plus its transient shards. |
batch_bytes |
Approximate bytes of source parquet per Stage-1 batch. |
compression |
Parquet compression codec. |
overwrite |
Rebuild an existing index. |
verbose |
Print progress. |
corpus_dir |
Explicit path to a single dataset Parquet directory. When
given, |
The corpus stores DOIs as full lower-cased resolver URLs
(https://doi.org/10.1097/...). The index key is the bare DOI, so that
a caller may supply any of these and get the same answer:
10.1016/j.joi.2017.08.007 https://doi.org/10.1016/j.joi.2017.08.007 http://dx.doi.org/10.1016/j.joi.2017.08.007 doi:10.1016/j.joi.2017.08.007 DOI: 10.1016/J.JOI.2017.08.007
Carrying parquet_file and file_row_number alongside the ID means
lookup_by_doi() can extract full records from one index read rather than
two.
The index contains:
Normalised bare lower-case DOI, no resolver prefix
OpenAlex ID in long form
floor(numeric_id / 10000), as in the ID index
Path relative to the parquet root
Row number within that file (0-indexed)
Rows are sorted by doi, so a point lookup reads the footer plus one row
group rather than scanning the file.
Invisibly, the path to the index file (corpus_dir mode) or
root_dir.
doi_to_id(), lookup_by_doi(), build_corpus_index()
Looks DOIs up in the index built by build_doi_index(). DOIs may be given
with or without a resolver prefix; all of these resolve identically:
doi_to_id( doi, root_dir = NULL, index_file = NULL, data_set = "works", verbose = TRUE )doi_to_id( doi, root_dir = NULL, index_file = NULL, data_set = "works", verbose = TRUE )
doi |
Character vector of DOIs. |
root_dir |
Root directory containing a |
index_file |
Explicit path to a |
data_set |
Dataset name used to locate the index under |
verbose |
Print progress. |
10.1016/j.joi.2017.08.007 https://doi.org/10.1016/j.joi.2017.08.007 http://dx.doi.org/10.1016/j.joi.2017.08.007 doi:10.1016/j.joi.2017.08.007 DOI: 10.1016/J.JOI.2017.08.007
A data frame with one row per input, in input order:
The string as supplied
Its normalised key
The OpenAlex ID in long form, or NA if unresolved
A data frame rather than a bare vector, because the caller needs to know which inputs failed to resolve.
build_doi_index(), lookup_by_doi()
get_citing() returns the works that cite the keypapers; get_cited()
returns the works the keypapers reference. Both work entirely offline
against a local snapshot.
get_citing( keypaper, root_dir = NULL, return = c("edges", "ids", "records"), output = NULL, columns = NULL, max_results = 100000L, depth = 1L, citation_index = NULL, id_index = NULL, doi_index = NULL, data_set = "works", workers = NULL, memory_limit = NULL, temp_dir = NULL, verbose = TRUE ) get_cited( keypaper, root_dir = NULL, return = c("edges", "ids", "records"), output = NULL, columns = NULL, max_results = 100000L, depth = 1L, id_index = NULL, doi_index = NULL, data_set = "works", workers = NULL, memory_limit = NULL, temp_dir = NULL, verbose = TRUE )get_citing( keypaper, root_dir = NULL, return = c("edges", "ids", "records"), output = NULL, columns = NULL, max_results = 100000L, depth = 1L, citation_index = NULL, id_index = NULL, doi_index = NULL, data_set = "works", workers = NULL, memory_limit = NULL, temp_dir = NULL, verbose = TRUE ) get_cited( keypaper, root_dir = NULL, return = c("edges", "ids", "records"), output = NULL, columns = NULL, max_results = 100000L, depth = 1L, id_index = NULL, doi_index = NULL, data_set = "works", workers = NULL, memory_limit = NULL, temp_dir = NULL, verbose = TRUE )
keypaper |
Character vector of one or more keypapers. Forms may be mixed freely in a single call:
All keypapers are resolved and queried in one pass, not in a loop. |
root_dir |
Root directory containing a |
return |
What to return:
|
output |
Directory to write records to when |
columns |
Column projection passed to |
max_results |
Error if more edges than this are found. A single heavily
cited work can have hundreds of thousands of citing works, and the
follow-on record extraction would then touch essentially every file in the
corpus. Set |
depth |
Reserved for multi-hop snowballing; only |
citation_index |
Explicit path to a |
id_index |
Explicit path to a |
doi_index |
Explicit path to a |
data_set |
Dataset name used to locate indexes under |
workers |
Parallel workers for record extraction. |
memory_limit |
DuckDB memory limit. |
temp_dir |
Root for DuckDB's spill files. |
verbose |
Print progress. |
The two are not symmetric in cost. get_cited() only needs the keypaper's
own referenced_works column, so it uses the ID index alone. get_citing()
asks the opposite question, which the snapshot does not answer directly –
cited_by_api_url is a URL – so it needs the inverted index from
build_citation_index().
A data frame of edges, a character vector of IDs, or records –
see return.
build_citation_index(), build_doi_index(), lookup_by_id()
Convenience wrapper: doi_to_id() followed by lookup_by_id().
lookup_by_doi( doi, root_dir = NULL, index_file = NULL, id_index = NULL, data_set = "works", verbose = TRUE, ... )lookup_by_doi( doi, root_dir = NULL, index_file = NULL, id_index = NULL, data_set = "works", verbose = TRUE, ... )
doi |
Character vector of DOIs. |
root_dir |
Root directory containing a |
index_file |
Explicit path to a |
id_index |
Explicit path to a |
data_set |
Dataset name used to locate the index under |
verbose |
Print progress. |
... |
Passed to |
Whatever lookup_by_id() returns.
Uses a pre-built index (created by build_corpus_index()) to locate records
efficiently and extract them from the Parquet corpus.
lookup_by_id( root_dir = NULL, ids, project_dir = NULL, data_sets = NULL, workers = NULL, progress = TRUE, verbose = TRUE, index_file = NULL, output = NULL, backend = c("auto", "r", "rust"), columns = NULL, add_columns = NULL, memory_limit = NULL, temp_dir = NULL )lookup_by_id( root_dir = NULL, ids, project_dir = NULL, data_sets = NULL, workers = NULL, progress = TRUE, verbose = TRUE, index_file = NULL, output = NULL, backend = c("auto", "r", "rust"), columns = NULL, add_columns = NULL, memory_limit = NULL, temp_dir = NULL )
root_dir |
Root directory containing |
ids |
Character vector of OpenAlex IDs to retrieve. Can be long form
(e.g. |
project_dir |
Project output directory. Extracted Parquet files are
written to |
data_sets |
Character vector of dataset names to search (e.g.
|
workers |
Number of parallel workers for reading corpus files. Default
is |
progress |
Ignored (kept for backward compatibility). |
verbose |
Print progress messages. Default is |
index_file |
Explicit path to an index Parquet file created by
|
output |
Path to an output directory for writing results as Parquet
files when using |
backend |
Retained only so that existing calls passing
|
columns |
Character vector of columns to return. |
add_columns |
Named list of constant columns to add to every returned
row, e.g. |
memory_limit |
DuckDB |
temp_dir |
Root for DuckDB's spill files. |
Paths can be supplied as a root_dir + data_sets pair (which
automatically locates the correct index files and writes output into
project_dir) or as an explicit index_file for direct use.
index_file mode, output not NULL: invisibly returns output.
index_file mode, output is NULL: returns a data frame of matching
records.
root_dir mode: invisibly returns project_dir.
build_corpus_index() for building the required index.
## Not run: # root_dir mode (searches multiple datasets) lookup_by_id( root_dir = "/Volumes/openalex", ids = c("W2741809807", "W1234567890"), project_dir = "my_project", data_sets = "works" ) # index_file mode (direct access, returns data frame) records <- lookup_by_id( index_file = "works_id_index.parquet", ids = c("W2741809807", "W1234567890") ) # index_file mode (write to parquet) lookup_by_id( index_file = "works_id_index.parquet", ids = large_id_vector, output = "filtered_works", workers = 3 ) ## End(Not run)## Not run: # root_dir mode (searches multiple datasets) lookup_by_id( root_dir = "/Volumes/openalex", ids = c("W2741809807", "W1234567890"), project_dir = "my_project", data_sets = "works" ) # index_file mode (direct access, returns data frame) records <- lookup_by_id( index_file = "works_id_index.parquet", ids = c("W2741809807", "W1234567890") ) # index_file mode (write to parquet) lookup_by_id( index_file = "works_id_index.parquet", ids = large_id_vector, output = "filtered_works", workers = 3 ) ## End(Not run)