Paperful Icon
Paperfuldocs
Search

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.

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:

FieldDescription
contentThe matching passage.
relevanceHow relevant the passage is to the query, when the results are reranked. Each page is sorted by it.
scoreThe keyword match score, when the search mode uses keywords.
distanceThe semantic distance to the query, when the search mode uses meaning.
paperThe paper the passage belongs to, including its metadata.
nodesThe nodes the passage came from, with their pages.
matchesThe 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",
  }
);
ModeDescription
hybridCombines keyword and semantic search. The default.
fulltextMatches the exact words in your query.
semanticMatches 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);
EventDataDescription
started{}The search started.
expanded{}The query was expanded with synonyms.
resultsSearchResultThe results before reranking.
reranking{ count }The results are being reranked.
completedSearchResultThe 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

On this page