# Search papers (/docs/sdks/search/query)



Search looks through the content of all [processed papers](/docs/core-concepts/papers#processing) in your workspace and returns the passages that match.

## Search [#search]

<Tabs groupId="language" items="[&#x22;JavaScript&#x22;]">
  <Tab value="JavaScript">
    ```ts
    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);
    }
    ```
  </Tab>
</Tabs>

Each result includes:

| Field       | Description                                                                                                                     |
| ----------- | ------------------------------------------------------------------------------------------------------------------------------- |
| `content`   | The matching passage.                                                                                                           |
| `relevance` | How relevant the passage is to the query, when the results are [reranked](#expansion-and-reranking). 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](/docs/core-concepts/graph) 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](#paginate).

To expand a result into the content around it, see [Search to context](/docs/sdks/search/context).

## Choose a search mode [#choose-a-search-mode]

<Tabs groupId="language" items="[&#x22;JavaScript&#x22;]">
  <Tab value="JavaScript">
    ```ts
    const { items } = await paperful.search.query("revenue",
      /* [!code ++:3] */
      {
        mode: "fulltext",
      }
    );
    ```
  </Tab>
</Tabs>

| 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 [#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.

<Tabs groupId="language" items="[&#x22;JavaScript&#x22;]">
  <Tab value="JavaScript">
    ```ts
    const { items } = await paperful.search.query("revenue",
      /* [!code ++:4] */
      {
        expand: false,
        rerank: false,
      }
    );
    ```
  </Tab>
</Tabs>

Reranking reorders the results within each page, pages themselves keep the search order. Without reranking, results don't include `relevance`.

## Filter by metadata [#filter-by-metadata]

Only search papers whose [metadata](/docs/core-concepts/papers#metadata) matches. Filters work the same way as when [listing papers](/docs/sdks/papers/list#filter-by-metadata).

<Tabs groupId="language" items="[&#x22;JavaScript&#x22;]">
  <Tab value="JavaScript">
    ```ts
    const { items } = await paperful.search.query("total amount",
      /* [!code ++:6] */
      {
        metadata: {
          category: ["invoice", "receipt"],
          amount: { gte: 100 },
        },
      }
    );
    ```
  </Tab>
</Tabs>

## Paginate [#paginate]

Results are [paginated](/docs/api#pagination). Pass the returned cursor, along with the same query, to get the next page.

<Tabs groupId="language" items="[&#x22;JavaScript&#x22;]">
  <Tab value="JavaScript">
    ```ts
    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 });
    }
    ```
  </Tab>
</Tabs>

Use `cursor.prev` with the `prev` option to go back a page. An invalid cursor throws a [validation error](/docs/sdks/errors).

## Stream search progress [#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.

<Tabs groupId="language" items="[&#x22;JavaScript&#x22;]">
  <Tab value="JavaScript">
    ```ts
    const results = await paperful.search.query("quarterly revenue growth", {
      /* [!code ++:3] */
      onEvent: ({ event, data }) => {
        if (event === "results") console.log(data.items);
      },
    });

    console.log(results.items);
    ```
  </Tab>
</Tabs>

| 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](/docs/sdks/errors).

## Options [#options]

<TypeTable
  type="{
  mode: {
    description: 'The search mode.',
    type: '&#x22;hybrid&#x22; | &#x22;fulltext&#x22; | &#x22;semantic&#x22;',
    default: '&#x22;hybrid&#x22;',
  },
  expand: {
    description: 'Expand the query with synonyms before searching. Not used in fulltext mode.',
    type: 'boolean',
    default: 'true',
  },
  rerank: {
    description: 'Rerank the results of each page by relevance to the query. Not used in fulltext mode.',
    type: 'boolean',
    default: 'true',
  },
  metadata: {
    description: 'Only search papers whose metadata matches.',
    type: 'MetadataFilter',
    default: undefined,
  },
  next: {
    description: 'Cursor for the next page.',
    type: 'string',
    default: undefined,
  },
  prev: {
    description: 'Cursor for the previous page.',
    type: 'string',
    default: undefined,
  },
  onEvent: {
    description: 'Receives the search steps as they happen. When set, the search is streamed.',
    type: '(event: SearchEvent) => void',
    default: undefined,
  },
}"
/>
