Search papers
Search the content of every paper in your workspace.
Search looks through the content of all processed papers in your workspace and returns the passages that match.
Search
const { items, total } = await paperful.search.query("quarterly revenue growth");
for (const result of items) {
console.log(result.paper.fileName, result.relevance);
console.log(result.content);
} Each result includes:
| Field | Description |
|---|---|
content | The matching passage. |
relevance | How relevant the passage is to the query, when the results are reranked. Each page is sorted by it. |
score | The keyword match score, when the search mode uses keywords. |
distance | The semantic distance to the query, when the search mode uses meaning. |
paper | The paper the passage belongs to, including its metadata. |
nodes | The nodes the passage came from, with their pages. |
matches | The matched terms and where they appear in the passage. |
The response also includes the total number of results and a cursor to paginate.
To expand a result into the content around it, see Search to context.
Choose a search mode
const { items } = await paperful.search.query("revenue",
{
mode: "fulltext",
}
); | Mode | Description |
|---|---|
hybrid | Combines keyword and semantic search. The default. |
fulltext | Matches the exact words in your query. |
semantic | Matches by meaning, even when the words differ. |
Expansion and reranking
By default, the query is expanded with synonyms before searching, and the results of each page are reranked by relevance to the query. Both are skipped in fulltext mode.
Turn them off for faster results, at the cost of relevance.
const { items } = await paperful.search.query("revenue",
{
expand: false,
rerank: false,
}
); Reranking reorders the results within each page, pages themselves keep the search order. Without reranking, results don't include relevance.
Filter by metadata
Only search papers whose metadata matches. Filters work the same way as when listing papers.
const { items } = await paperful.search.query("total amount",
{
metadata: {
category: ["invoice", "receipt"],
amount: { gte: 100 },
},
}
); Paginate
Results are paginated. Pass the returned cursor, along with the same query, to get the next page.
const query = "quarterly revenue growth";
let page = await paperful.search.query(query);
while (page.cursor?.next) {
page = await paperful.search.query(query, { next: page.cursor.next });
} Use cursor.prev with the prev option to go back a page. An invalid cursor throws a validation error.
Stream search progress
Reranking takes a moment. Pass onEvent to receive each step of the search as it happens, for example to show results before they are reranked. The final, reranked results are still returned.
const results = await paperful.search.query("quarterly revenue growth", {
onEvent: ({ event, data }) => {
if (event === "results") console.log(data.items);
},
});
console.log(results.items); | Event | Data | Description |
|---|---|---|
started | {} | The search started. |
expanded | {} | The query was expanded with synonyms. |
results | SearchResult | The results before reranking. |
reranking | { count } | The results are being reranked. |
completed | SearchResult | The final results. |
expanded is only sent when the query is expanded, and results and reranking only when there are results to rerank. In fulltext mode, only started and completed are sent. If the search fails while streaming, the error is thrown like any other error.
Options
Prop
Type