You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/reference/low-level-api/migration.md
+22-6Lines changed: 22 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -254,7 +254,7 @@ The same pattern applies to every typed endpoint package: `typedapi/indices/crea
254
254
255
255
### Get a full typed client from an existing low-level client [_to_typed]
256
256
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:
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).
281
281
282
282
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.
283
283
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).
285
285
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.
287
303
288
304
#### Shared lifecycle [_to_typed_close]
289
305
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`.
Copy file name to clipboardExpand all lines: docs/reference/typed-api/index.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -52,7 +52,7 @@ graph TB
52
52
OPTS --> TP
53
53
```
54
54
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.
0 commit comments