Home / Docs / Search

Search: exact text and regex search API

Find the documents that contain some text or match a pattern. Each result quotes the match and its line, so an agent can answer without reading the whole document.

The call

GET /api/collections/{name}/search

NameTypeWhat it does
qstringRequired. What to find: plain text, matched literally and ignoring case; with regex=1, a regular expression (syntax).
regex1Optional. Read q as a regular expression.
filterstringOptional. Only documents whose metadata has these values: key:value terms separated by spaces, all of which must hold.
limitintegerOptional. How many documents to return: 20 unless you ask, up to your plan's most (plans).
Shell
curl -G https://stackgrep.com/api/collections/contracts/search \
  -H "Authorization: Bearer $STACKGREP_KEY" \
  --data-urlencode 'q=terminat\w+ for convenience' -d regex=1 \
  -d filter=customer:acme -d limit=5
Response
{"collection": "contracts", "took_ms": 6.4, "docs": 48210,
 "hits": [
  {"id": "acme/msa-2024.txt", "match": "terminate for convenience",
   "line": "12.2 Either party may terminate for convenience on 90 days' written notice.",
   "meta": {"customer": "acme", "year": "2024", "signed": "true"}}
 ]}

The answer

NameTypeWhat it does
hits[].idstringThe document's id: read it whole with it.
hits[].matchstringThe text that matched.
hits[].linestringThe line around the match, so an agent can often answer from the result alone.
hits[].metaobjectThe document's metadata.
docsintegerDocuments in the collection's index, older versions included until compaction.
took_msnumberTime spent searching, in milliseconds.

Each document shows up once, with its first match; the newest version of each document is the only one searched. A search costs 1 credit.

Need to know how many documents match, not just the first few? Use count.

Try it on your own documentsExact and regex search for your agents, from one API. Or see the engine on npm's source first.