Package 'openalexAPI'

Title: Local OpenAlex-Compatible Web API over a Parquet Corpus
Description: Serves a local Parquet corpus of OpenAlex works through a web API that answers like the OpenAlex '/works' endpoint, so the same classic filter URLs, OQL (OpenAlex Query Language) and OQO (OpenAlex Query Object) queries, including 'group_by', can be run against local data. Queries are translated to OQO by the free OpenAlex '/query' endpoint and compiled locally to DuckDB SQL. A small static web page is included for running searches in the browser.
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.1.6
Built: 2026-12-01 11:38:19 UTC
Source: https://github.com/openalexPro/openalexAPI

Help Index


Compile an OQO into DuckDB SQL

Description

Walks the filter tree of an OQO (OpenAlex Query Object): the top-level filter_rows are joined with AND, a branch with its join (and / or), and is_negated negates a leaf or branch. Each leaf goes through the works column registry. A leaf whose value is missing never matches, and its negation does, as with ! in OpenAlex; a null leaf value matches works where the field is missing. The SQL refers to the works table built by prepare_corpus().

Usage

oqo_to_sql(oqo, con, columns = NULL)

Arguments

oqo

An OQO as a list (e.g. from jsonlite::fromJSON(x, simplifyVector = FALSE)).

con

A DuckDB connection (used to quote values).

columns

The corpus columns; registry columns that need others are refused. NULL skips the check.

Value

A list with where (SQL condition, "true" without filters), order (SQL ⁠ORDER BY⁠ terms from sort_by, or NULL; with group_by terms on n (count), k (key) and u (1 for the unknown group)) and group (NULL, or the group_by dimension: column, pairs (SQL giving each work a list of ⁠{g, k, d}⁠ identity / key / display-name structs) and include_unknown), score (the BM25 relevance of the search leaves that are not negated, or NULL without a search), and prelude (SQL statements to run first: each search term's matches and scores as a temp table, computed once) with temp (their names, to drop afterwards). Run with .api_run_prelude().


Prepare a Parquet corpus for serving

Description

Reads all Parquet files below corpus into one DuckDB database file: one row per work (duplicates across partitions removed), with helper columns for ID and DOI matching and the abstract as text. The Hive partition keys of the folders (⁠query=a/⁠, ⁠year=2020/⁠, any key and depth) are kept per work, with all values of a work that is in several partitions; they are listed by the ⁠/corpus⁠ route, not served as fields of the works. The database is cached and reused while the corpus files are unchanged (same paths, sizes and modification times). When a new database is built, the databases of earlier states of the same corpus in cache_dir are deleted.

Usage

prepare_corpus(
  corpus,
  rebuild = FALSE,
  cache_dir = file.path(dirname(corpus), "API")
)

Arguments

corpus

Directory with the corpus Parquet files (searched recursively).

rebuild

Rebuild the database even if a cached one exists.

cache_dir

Where the database files are kept. Defaults to an API folder next to corpus, i.e. in the project folder (as in openalexPro), so the database stays with the corpus it was built from.

Details

The corpus can come from openalexPro::pro_request_parquet() (keeps abstract_inverted_index and adds abstract), from the JSONL path (only abstract), from a snapshot extraction, or from any source with the OpenAlex works schema.

The link table of the ecosystem standard, ⁠links/⁠ next to corpus (all ⁠*.parquet⁠ below it), is read into a second table and served by ⁠/links⁠. Without it, ⁠/links⁠ has no rows.

Value

The path of the DuckDB database file, invisibly.


Serve a corpus as an OpenAlex-compatible API

Description

Prepares the corpus (see prepare_corpus()) and serves it like the OpenAlex API, so openalexPro::pro_query(endpoint = "http://127.0.0.1:8000") and the download functions of openalexPro work against it unchanged:

Usage

serve_corpus(
  corpus,
  port = 8000L,
  host = "127.0.0.1",
  api_key = NULL,
  root = "https://api.openalex.org",
  block = TRUE,
  ...
)

Arguments

corpus

Directory with the corpus Parquet files.

port, host

Where to serve. The default host is only reachable from this computer; another host makes the corpus (and translation with api_key) available to the network.

api_key

OpenAlex API key for translation requests that bring none. Not needed: translation is free.

root

The OpenAlex API used for translation.

block

Block the R session while serving.

...

Passed to prepare_corpus().

Details

  • GET /works?filter=...&sort=...&per_page=...&cursor=..., also with ⁠group_by=⁠ (one dimension; ⁠:include_unknown⁠ shows works without a value as the group unknown)

  • GET /works/{id} (any ID form, or a DOI)

  • GET /?oql=... and GET /?oqo=..., ⁠POST /⁠ with oqo or oql and the view parameters select, sort, per_page, page, cursor

  • GET /links?filter=...: the corpus's link table (⁠links/⁠ next to corpus), shaped like OpenAlex's ⁠/links⁠. Filters origin, target (any ID form, or a DOI), link_type (part or the full URL), relation, origin_in_corpus, target_in_corpus; | for any of several values, ! to negate, null for missing. Unlike OpenAlex, no work needs to be named, and group_by takes link_type, relation or the ⁠*_in_corpus⁠ flags. Records are returned as stored, with all their columns (e.g. relation, which OpenAlex does not return)

  • POST /query: forwarded to OpenAlex (translation)

  • GET /corpus: what is served (filters, group_by columns) and the partition keys of the corpus folders with the number of works per value

  • /: a web page for running queries in the browser (⁠/app⁠)

Classic URLs and OQL are translated to OQO by OpenAlex's free ⁠/query⁠ endpoint (the network is needed, no credits are spent); an OQO runs fully offline. Requests are handled one at a time.

Value

The plumber2 api object, invisibly.