Making Queries
Learn how to query Redshred segments using its full-featured query language — with text, attribute, boolean, and spatial query support.
This is a div block with a Webflow interaction that will be triggered when the heading is in the view.
Intro to the Query Language
Redshred provides a full-featured query language that lets you say things like “all the steps within this procedure” or “all the headings within this document”.
The query language provides:
- Queries for objects and attributes
- String, substring, and regular expression based matches
- Spatial queries based on the arrangement of elements in the document
- Boolean connectives to
AND,OR, andNOTsubclauses together to express complex constraints
Making Queries
The basics of executing a query are to send a q= URL parameter to a /segments/ endpoint. These endpoints are found in several locations across the list of endpoints, most notably off of /collections/$COLLECTION/segments/ and /documents/$DOCUMENT_ID/segments/. In each case, the query is implicitly scoped to the prefix of the URL: in the first case, the collection, and in the second case, just the specified document.
bash:
curl -H "Authorization: Token $REDSHRED_AUTH_TOKEN" \
-H "Content-Type: application/json" \
-X GET "https://api.redshred.com/v2/collections/$COLLECTION/documents/$DOCUMENT_ID/segments/?q=$EXPRESSION"
python:
from redshred import RedShredClient
# Create the client
client = RedShredClient(token=YOUR_USER_API_TOKEN, host="https://api.redshred.com")
# With the client access the desired document from your collection, then query that document
result = client.collection("my-collection").document(DOCUMENT_ID).q(SEARCH_TERM)
Search Results
You’ll receive a payload in reply that includes a list of search results of the form:
{
"next": "...",
"previous": "...",
"count": 123,
"results": [ "..." ], // a list of instances
}
next and previous are hyperlinks if a previous or next page exists, null otherwise. count is the total number of results across all pages and results is a list of segment instances.
Segment Basics
Segments contain, at a minimum:
- Metadata about their creation (who created them, when, which enrichment process)
text- the text under this segment on the page.regions- the unique region of the collection’s space occupied by this segmentenrichment_data- the metadata attached by this segment to the document at this region. The details of this vary from enrichment to enrichment.
The query language lets you make queries around these data in both as structured data using tests like less-than, greater-than, and equality testing as well as spatial queries that allow you to search for content based on where it appears on the page and how it relates to its surrounding content.
On the next pages let’s take a look at some example queries.