How it works
After this page you will know how a search goes from a Python expression to an HTTP request, which parts
run in Python, and which endpoints the client uses. You do not need this to use scixplorer, but it helps
to read the output of explain().
1. Expressions are trees
Comparing a field builds a small, immutable tree of nodes. Nothing is sent and nothing is compiled yet:
from scixplorer import Document, Journal, get_default_client, search
condition = (Document.author[0] == "Kewley, L") & Document.year.between(2015, 2020)
condition
# -> And(operands=(Func(name='pos', args=(Match(field=<NameField Document.author>, value='Kewley, L', exact=False), 1, 1)), Range(field=<IntegerField Document.year>, lo=2015, hi=2020, include_lo=True, include_hi=True)))Each field type offers only the operations that make sense for it (ranges on numbers and dates, wildcards
on text), and checks values as the tree is built. Expressions have no truth value, so and, or and
not fail loudly instead of dropping a condition.
2. Two interpreters
The same tree can be read two ways, by two internal modules:
solr.compile()turns it into a Solr query string. It is the only place that knows Solr’s quoting, escaping and range syntax.evaluate.matches()evaluates it in Python against an object.
from scixplorer import evaluate, solr
solr.compile(condition)
# -> pos(author:"Kewley, L", 1) AND year:[2015 TO 2020]
evaluate.matches(Journal.title.contains("astrophysical"), Journal.lookup("ApJ"))
# -> TrueJournals, affiliations, vocabulary concepts and features, and your list of libraries are searched with the Python interpreter, because they are held in memory. Documents are searched by SciX.
3. The planner
Before a document search is compiled, a planner (search.plan) rewrites it, in this order:
- A condition on another model’s field, such as
Journal.title == "Icarus", is attached to the document relation that leads to that model. - Each relation condition is evaluated in Python against the related model, and replaced by the matching
identifiers: journal titles become
bibstemvalues, affiliations becomeaff_idvalues, concepts and features becomeuat_idandplanetary_feature_idvalues. At most 1000 are allowed. - Library membership is left to SciX (
docs(library/<id>)), or, withconfig.library_membership = "bibcodes", replaced by the library’s bibcodes. - The tree is simplified. A condition that can never match ends the planning: no request is sent. Then each per-author condition is translated into a condition on the document’s per-author lists, exactly where possible; otherwise a necessary condition is sent and the real one is kept for the client to check on each result.
- The default collection is added to open searches.
- Sets of more than 50 bibcodes are taken out of the query string: uploaded to
/search/bigquery, or excluded from the results as they arrive. A search that would then leave SciX with nothing to narrow it is refused. - The tree is compiled, and the sort gets a final
idkey so that pages are stable.
The plan is pure: given the same query and configuration, it gives the same result without any I/O.
query = (
Document
.select(Document.bibcode)
.where(condition)
.limit(5)
)
plan = search.plan(query)
plan.q
# -> pos(author:"Kewley, L", 1) AND year:[2015 TO 2020]
plan.request(0, 5)
# -> Request(method='GET', path='/search/query'explain() shows the first request of the plan and its notes, which record each of these decisions.
4. Sending requests
The code that runs a search never performs I/O itself. It yields request descriptions and receives
responses, one page at a time. A small driver sends each request with a Client, either
synchronously or asynchronously, so paging and result handling are written once and work both ways. This
is why every query has both all() and aall().
The client adds your token, retries transient failures, records rate limits and turns error responses into exceptions (see Errors and limits).
Endpoints
| Endpoint | Method | Used for |
|---|---|---|
/search/query | GET | document searches, counts and lazy loading |
/search/bigquery | POST | document searches with more than 50 bibcodes |
/biblib/libraries, /biblib/libraries/<id> | GET | listing and fetching libraries |
/biblib/libraries | POST | creating a library |
/biblib/documents/<id> | POST, PUT, DELETE | adding and removing documents, changing metadata, deleting |
/biblib/libraries/operations/<id> | POST | union, intersection, difference, copy_to, empty |
/biblib/permissions/<id>, /biblib/transfer/<id> | GET, POST | permissions and ownership |
/export/<format> | POST | exports |
/resolver/<bibcode>[/<link type>] | GET | external resources |
/vis/author-network, /vis/paper-network | POST | network data |
/citation_helper | POST | citation suggestions |
Paths are relative to config.api_url, https://api.scixplorer.org/v1 by default.
Sending your own requests
For an endpoint scixplorer does not wrap, send a Request from scixplorer.client with a client. You get the token,
retries, rate-limit tracking and error handling, and a Response whose data is the parsed
JSON:
from scixplorer.client import Request
request = Request("GET", "/resolver/2006MNRAS.372..961K")
response = get_default_client().send(request)
response.status_code
# -> 200Request takes method, path, and optionally params, json (a JSON body), content (a raw body),
headers and idempotent (whether it is safe to retry; by default GET, PUT and DELETE are and POST is
not). await client.asend(request) is the asynchronous form.