> Complete content index: https://narsil.sondelali.com/llms.txt

# Upgrading to 0.4

> Update a Narsil 0.3 application to 0.4.1, with the changes that 0.4 makes to whole-value filters on text fields, filters on undeclared fields, groups, snapshots, 0.3 indexes on disk, partition limits, and the inputs that it refuses.

URL: https://narsil.sondelali.com/docs/upgrading
Section: Getting started
Version: 0.4 (@delali/narsil@0.4.3, latest)

In Narsil 0.4, a filter tests a `string` field in a different way, `snapshot` returns a stream, and the engine stores an index on disk in a new layout, so a 0.3 application can need code changes before you move it to 0.4. Every change between 0.3.0 and 0.4.1 that can break a 0.3 application or change its results has a section below. Narsil has no Python, Go, or Rust package for 0.3, so only applications that use `@delali/narsil` need these steps. The [getting started](/docs/getting-started) query returns the same three bicycles with the same scores on 0.3.0 and 0.4.1.

## Update the package

Update `@delali/narsil` to 0.4.1. Both releases require Node.js 22 or later.

```bash
pnpm add -E @delali/narsil@0.4.1
```

Every export and every HTTP route from 0.3 is in 0.4 as well. The 0.4 package declares two new optional peer dependencies, `@aws-sdk/client-s3` and `@google-cloud/storage`, which you need to install only when you use the S3 adapter or the Google Cloud Storage adapter. If you serve your indexes over HTTP, install `uWebSockets.js` v20.70.0, which is the release in the install command on [HTTP server](/docs/http-server).

## Mark the text fields that you filter by whole value

In 0.3, a filter can test a plain `string` field with `eq`, `ne`, `in`, `nin`, `startsWith`, or `endsWith`, and a plain `string[]` field with `eq`, `containsAll`, or `matchesAny`. In 0.4, the engine refuses each of those tests on a plain field with `SEARCH_INVALID_FILTER`, because the engine finds whole values through a pattern index, which it keeps only for a field whose type includes `pattern`. Declare each such field as `string:pattern` or `string[]:pattern` in a new index and insert your documents into it, since an index keeps the schema that you give it at creation. A pattern field also accepts `contains`, which 0.3 refuses on a `string` field.

On Node.js, the engine loads the pattern code by itself. In a browser, call `registerPatternSearch(patternSearch)` from `@delali/narsil/pattern` before you create an index with a pattern field, since the engine refuses that index with `CONFIG_INVALID` until you register pattern search. Every pattern test is on [Filters and facets](/docs/filters-and-facets).

The `gt`, `gte`, `lt`, `lte`, and `between` bounds of the `StringFilter` type are numbers in 0.3 and strings in 0.4, because the 0.4 engine compares those bounds as text against a `string` field whose type includes `sortable`.

## Filter only on fields that the schema declares

In 0.3, a filter on a field that the schema doesn't declare matches nothing. In 0.4, the engine refuses the same filter with `SEARCH_INVALID_FIELD` on every index, whether or not the index is strict. As in 0.3, the engine accepts a sort, a facet, or a group on such a field when the index has no `strict` setting, while it refuses them on a strict index. In 0.4, the engine also refuses a group on a vector field with `SEARCH_INVALID_FIELD`. Search your code for filters on fields that the schema doesn't declare, and either add each field to the schema of a new index or remove the filter. A list of what the engine does with an undeclared field in each part of a query is on the [errors](/docs/errors#turn-a-misspelt-field-name-into-an-error) page.

## Read a missing group value as null

When the hits of a group lack a field that you group by, the `values` of that group in 0.3 and 0.4.0 hold no entry for the field, as in `{}`. In 0.4.1, `values` holds `null` for the field, as in `{ category: null }`, which a cluster returns as well. Update any code that tests whether such a field is in `values`, such as `'category' in group.values`, so that it tests for `null`. There is more on groups on [Sorting, grouping, and pagination](/docs/sorting-grouping-and-pagination#collapse-the-hits-into-groups).

## Read a snapshot as a stream

In 0.3, `snapshot(indexName)` returns a `Uint8Array`. In 0.4, both `snapshot(indexName)` and the HTTP client's `snapshot` return a `ReadableStream<Uint8Array>` of a tar archive. `writeFile` from `node:fs/promises` accepts the stream as it is, while you will have to change any code that uses the `length` of the snapshot or indexes into it as bytes. `restore` accepts a `Uint8Array`, a `ReadableStream`, or an async iterable of byte chunks, such as a file stream.

The 0.4 engine restores a snapshot from 0.3; however, the 0.3 engine refuses a snapshot from 0.4 with `ENVELOPE_INVALID_MAGIC`. Keep your 0.3 snapshots until you are sure that you will stay on 0.4.

## Back up your data directory before the first start

The 0.4 engine opens an index that 0.3 stores through the filesystem persistence adapter or through `durability: { directory }`. Before it serves the first request for that index, the engine rewrites the index in the 0.4 layout. The engine emits a `layoutUpgrade` event when each rewrite starts, completes, or fails. Because `createNarsil` finishes those rewrites before its promise resolves, a handler that you add with `on` afterwards receives none of those events, so pass your handler in the `events` setting, as in `createNarsil({ durability, events: { layoutUpgrade: report } })`. There is more on the rewrite in [Persistence and durability](/docs/persistence-and-durability).

The rewrite works in one direction only. Once the 0.4 engine opens and checkpoints a 0.3 store, the 0.3 engine refuses that store with `PERSISTENCE_LOAD_FAILED` and the message 'Unsupported segment manifest version 7; expected 6'. Copy the data directory before you first start 0.4, so that you can roll back by starting 0.3 on the copy.

Every built-in language has a new revision in 0.4, so every index that 0.3 stores opens as stale. Until the engine finishes rebuilding the terms of a stale index in the background from the documents that the index holds, `listIndexes()` returns `analysisStale: true` for that index and every query on it returns `analysisStale: true` as well. The engine keeps answering queries on a stale index while the rebuild goes on. There is more on the rebuild in [Language support](/docs/language-support).

The 0.3 engine accepts a `vectorPromotion.hnswConfig.m` of 1, while the 0.4 engine refuses that value for a new index with `CONFIG_INVALID`. When the 0.4 engine opens an existing index whose `m` is 1, it uses an `m` of 2 and builds the vector graphs of that index again. The 0.4 engine refuses an index in a layout from before 0.3 with `ENVELOPE_VERSION_MISMATCH`. From 0.4 onwards, you can move an index across more than one release if you export its documents with `exportDocuments` on the release that stores it and import them with `importDocumentExport` on the new release.

## Raise the partition limit before you rebalance

In 0.3, `rebalance` can raise the partition count of an index above `partitions.maxPartitions`, which defaults to 1. In 0.4, the engine refuses that rebalance with `PARTITION_CAPACITY_EXCEEDED` and a message such as 'Target partition count (2) is above the maximum of 1 partitions'. Raise the limit with `updatePartitionConfig` before you call `rebalance`, as in the example on [Partitions and workers](/docs/partitions-and-workers).

## Check the inputs that 0.4 refuses

The 0.4 engine refuses these inputs, which 0.3 accepts:

- The engine refuses a text value that holds half of a surrogate pair with `DOC_VALIDATION_FAILED`.
- The engine refuses a filter list of more than 65,536 values, or a filter of more than 1,024 tests, with `SEARCH_INVALID_FILTER`.
- The engine refuses a vector field of more than 4,096 dimensions in a new index with `SCHEMA_INVALID_VECTOR_DIMENSION`.
- The engine refuses a `hnswConfig.m` of 1 in a new index with `CONFIG_INVALID`.

The 0.4 engine also blocks inserts and updates while its memory use is above the `memory.blockWritesAbove` fraction of its limit, which is 0.9 by default. It accepts writes again once its memory use falls below `memory.resumeWritesBelow`, which is 0.8 by default. The engine refuses a blocked write with `MEMORY_WRITE_BLOCKED`, which the HTTP server returns as status 429, while it keeps accepting removals, searches, and reads. If your process comes close to its memory limit on 0.3, the 0.4 engine can refuse its writes, so watch for that code after the upgrade.

## Update code that depends on changed types or codes

`ErrorCodes` in 0.4 has no `SNAPSHOT_SYNC_TOO_LARGE`, so TypeScript will refuse to compile code that compares an error with that constant. `ErrorCodes` holds 43 new codes in 0.4, such as `MEMORY_WRITE_BLOCKED`, `SEARCH_INVALID_PHRASE`, and `SEARCH_WORK_CAP_EXCEEDED`, and `ClientErrorCodes` holds the new `SYNC_ANALYSIS_UNAVAILABLE`. Every code from 0.3 maps to the same HTTP status in 0.4. The common codes are on the [errors](/docs/errors) page.

A few types change in ways that can break code that implements or builds them:

- `ClusterCoordinator.putAllocation` takes a `writerNodeId` after `expectedVersion`, and the interface requires two new methods, `delete` and `watchIndexMetadata`, so you will have to make all three changes to a custom coordinator before it compiles.
- `NarsilClientOptions.url` accepts an array of addresses as well as one address.
- `NarsilProviderProps.client` is optional.
- `HttpIndexConfig.stopWords` accepts a string as well as an array of strings.

## Upgrade the nodes of a cluster

The cluster specification is at version 1.0 in 0.3 and at version 1.5 in 0.4. A 0.4 node joins a cluster only when the cluster's controller uses the same major version of the specification. Both releases use major version 1, so a 0.4 node can join a cluster of 0.3 nodes. Each cluster feature that 0.4 adds requires a specification version: phrase search and BM25 tuning require 1.1, backups require 1.2, device sync requires 1.3, query suggestions require 1.4, and change capture requires 1.5. For example, while a 0.3 node holds a copy of an index, the cluster refuses a phrase query on that index with `SEARCH_PHRASE_NOT_READY`, so finish upgrading every node before you use a feature that 0.4 adds.
