Skip to content

Commit df7cf6d

Browse files
authored
docs(reference): switch typed-API migration guide to elasticsearch.NewTypedFrom (#1475)
1 parent 5284fda commit df7cf6d

2 files changed

Lines changed: 23 additions & 7 deletions

File tree

docs/reference/low-level-api/migration.md

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ The same pattern applies to every typed endpoint package: `typedapi/indices/crea
254254

255255
### Get a full typed client from an existing low-level client [_to_typed]
256256

257-
If you want the full typed API surface (not just one endpoint package at a time) but the existing `*elasticsearch.Client` must keep working for other call sites, use `ToTyped`. It returns a `*elasticsearch.TypedClient` that reuses the source client's transport, connection pool, and configuration, without building a second transport:
257+
If you want the full typed API surface (not just one endpoint package at a time) but the existing `*elasticsearch.Client` must keep working for other call sites, use `elasticsearch.NewTypedFrom`. It returns a `*elasticsearch.TypedClient` that reuses the source client's transport, connection pool, and configuration, without building a second transport:
258258

259259
```go
260260
client, err := elasticsearch.New(
@@ -266,7 +266,7 @@ if err != nil {
266266
}
267267
defer client.Close(context.Background())
268268

269-
typed := client.ToTyped() // <1>
269+
typed := elasticsearch.NewTypedFrom(client) // <1>
270270

271271
// Low-level call sites keep working.
272272
res, err := client.Indices.Exists([]string{"my-index"})
@@ -277,17 +277,33 @@ info, err := typed.Info().Do(context.Background())
277277
// ...
278278
```
279279

280-
1. `ToTyped` copies the source client's compatibility mode, auto-drain-body, and disable-meta-header settings into the new `*TypedClient`, and shares the underlying transport (and its connection pool).
280+
1. `NewTypedFrom` copies the source client's compatibility mode, auto-drain-body, and disable-meta-header settings into the new `*TypedClient`, and shares the underlying transport (and its connection pool).
281281

282282
This is the most common choice when the `*Client` is constructed in a shared infrastructure package you don't own, and you can't swap the constructor for `elasticsearch.NewTyped` without coordinating with other teams.
283283

284-
#### Call `ToTyped` once and reuse it [_to_typed_reuse]
284+
`NewTypedFrom` is a free function rather than a method on `*Client` on purpose: a method on the concrete `*Client` type would keep the entire typed API tree linker-reachable in every binary that imports the functional API, even when the typed API is never used (this was the cause of [#1472](https://github.com/elastic/go-elasticsearch/issues/1472) in v9.4.0).
285285

286-
`ToTyped` allocates a new typed API tree, and the returned client re-runs the product check on its first request. Call it once at setup and cache the result; do not call it per request or per handler invocation.
286+
#### Migrating from v9.4.0 [_to_typed_v940_migration]
287+
288+
In v9.4.0, this conversion was exposed as `(*elasticsearch.Client).ToTyped()`. That method has been removed in v9.4.1 because, as a method on `*Client`, it kept the entire typed API tree reachable for the Go linker in every binary that imported `*Client` (typically adding ~40 MB / ~36% to binary size for functional-API-only consumers). The replacement is a one-line change with no new import:
289+
290+
```go
291+
// v9.4.0
292+
typed := client.ToTyped()
293+
294+
// v9.4.1
295+
typed := elasticsearch.NewTypedFrom(client)
296+
```
297+
298+
The returned `*elasticsearch.TypedClient` is identical to what `ToTyped()` returned previously.
299+
300+
#### Call `NewTypedFrom` once and reuse it [_to_typed_reuse]
301+
302+
`NewTypedFrom` allocates a new typed API tree, and the returned client re-runs the product check on its first request. Call it once at setup and cache the result; do not call it per request or per handler invocation.
287303

288304
#### Shared lifecycle [_to_typed_close]
289305

290-
Because the `*TypedClient` returned by `ToTyped` shares the transport with the source `*Client`, **closing either one closes the connection pool used by both**. After `typed.Close(ctx)` returns, subsequent requests on the original `*Client` will fail with a closed-connection error, and vice versa. In a partial-migration setup, treat the source `*Client` as the lifecycle owner: let whichever code owns the `*Client` call `Close`, and do not call `Close` on the `*TypedClient` returned by `ToTyped`.
306+
Because the `*TypedClient` returned by `NewTypedFrom` shares the transport with the source `*Client`, **closing either one closes the connection pool used by both**. After `typed.Close(ctx)` returns, subsequent requests on the original `*Client` will fail with a closed-connection error, and vice versa. In a partial-migration setup, treat the source `*Client` as the lifecycle owner: let whichever code owns the `*Client` call `Close`, and do not call `Close` on the `*TypedClient` returned by `NewTypedFrom`.
291307

292308
### Strategy [_partial_migration_strategy]
293309

docs/reference/typed-api/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ graph TB
5252
OPTS --> TP
5353
```
5454

55-
Because both clients implement the same transport interface, individual typed endpoint packages (`typedapi/core/search`, `typedapi/indices/create`, ...) accept either client type. That means you can mix the two styles in a single application, and you can migrate existing low-level code to the typed API one endpoint at a time. If you already hold a `*elasticsearch.Client` and want the full typed surface without rebuilding the transport, call `client.ToTyped()`. See the [migration guide](../low-level-api/migration.md) for details on both approaches.
55+
Because both clients implement the same transport interface, individual typed endpoint packages (`typedapi/core/search`, `typedapi/indices/create`, ...) accept either client type. That means you can mix the two styles in a single application, and you can migrate existing low-level code to the typed API one endpoint at a time. If you already hold a `*elasticsearch.Client` and want the full typed surface without rebuilding the transport, call `elasticsearch.NewTypedFrom(client)`. See the [migration guide](../low-level-api/migration.md) for details on both approaches.
5656

5757
## API namespaces [_typed_api_namespaces]
5858

0 commit comments

Comments
 (0)