Skip to content

Commit e722a16

Browse files
committed
Add documentation for client type serialization
1 parent 2717607 commit e722a16

File tree

3 files changed

+145
-1
lines changed

3 files changed

+145
-1
lines changed
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
---
2+
mapped_pages:
3+
- https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/request-response-serialization.html
4+
---
5+
6+
# Serialization of Elasticsearch types [request-response-serialization]
7+
8+
Normally, it is not necessary to manually serialize or deserialize internal `Elastic.Clients.Elasticsearch` types. In rare cases, however, it may still be useful to do so.
9+
10+
:::{warning}
11+
12+
We strongly advise against persisting serialized Elasticsearch types, as their structure may vary between different versions of the client.
13+
14+
Instead, you should create your own POCO type and map/assign the desired properties to it.
15+
16+
:::
17+
18+
## Serializing Elasticsearch types
19+
20+
The default `System.Text.Json` serializer does not know how to serialize `Elastic.Clients.Elasticsearch` types. Therefore, (de-)serialization must be delegated to the built in `RequestResponseSerializer` of the client.
21+
22+
### Serialization
23+
24+
```csharp
25+
using Elastic.Clients.Elasticsearch;
26+
using Elastic.Transport.Extensions;
27+
28+
var query = new Query
29+
{
30+
Match = new MatchQuery
31+
{
32+
Field = Infer.Field<Person>(p => p.FirstName),
33+
Query = "Florian"
34+
}
35+
};
36+
37+
var json = client.RequestResponseSerializer.SerializeToString(query); <1>
38+
```
39+
40+
1. Use the `RequestResponseSerializer` to serialize the `Query` instance to a JSON string.
41+
42+
### Deserialization
43+
44+
```csharp
45+
using Elastic.Clients.Elasticsearch;
46+
using Elastic.Transport.Extensions;
47+
48+
var json = """
49+
{
50+
"match": {
51+
"firstName": {
52+
"query": "Florian"
53+
}
54+
}
55+
}
56+
""";
57+
58+
var query = client.RequestResponseSerializer.Deserialize<Query>(json)!; <1>
59+
```
60+
61+
1. Use the `RequestResponseSerializer` to deserialize the JSON string to a `Query` instance.
62+
63+
### Remarks
64+
65+
In addition to `SerializeToString()` and `Deserialize<T>()`, the `Elastic.Transport.Extensions` namespace offers several other extension methods related to (de)serialization.
66+
67+
### Limitations for request and response types
68+
69+
The (de)serialization of Elasticsearch request and response types is subject to certain limitations:
70+
71+
In addition to *body*-related properties, request types also have properties whose values are used in the HTTP *path* or *query* segment.
72+
73+
These properties are **not** taken into account during serialization. The result of serialization represents only the *body* of the request.
74+
75+
::::{warning}
76+
77+
When deserializing a request type, it may happen that otherwise required path or query parameters are not initialized. These properties must be manually assigned before using the request.
78+
79+
::::
80+
81+
Two prominent examples are the `Bulk` or `MultiGet` APIs, which use NDJSON bodies. Binary responses also occur in some cases.
82+
83+
## Serializing Elasticsearch descriptor types
84+
85+
Descriptor types do not support direct (de-)serialization.
86+
87+
However, starting with client versions 8.19 and 9.0, descriptor types support bidirectional conversion to/from the corresponding object representation.
88+
89+
We can use this functionality to indirectly (de)serialize descriptor types.
90+
91+
### Serialization
92+
93+
```csharp
94+
using Elastic.Clients.Elasticsearch;
95+
using Elastic.Transport.Extensions;
96+
97+
var requestDescriptor = new SearchRequestDescriptor<Person>()
98+
.Query(q => q
99+
.Match(m => m
100+
.Field(p => p.FirstName)
101+
.Query("Florian")
102+
)
103+
);
104+
105+
var request = (SearchRequest)requestDescriptor; <1>
106+
var json = client.RequestResponseSerializer.SerializeToString(request); <2>
107+
```
108+
109+
1. Unwrap the request by casting the descriptor to the related object representation.
110+
2. Serialize the unwrapped request instance.
111+
112+
### Deserialization
113+
114+
```csharp
115+
using Elastic.Clients.Elasticsearch;
116+
using Elastic.Transport.Extensions;
117+
118+
var json = """
119+
{
120+
"query": {
121+
"match": {
122+
"firstName": {
123+
"query": "Florian"
124+
}
125+
}
126+
}
127+
}
128+
""";
129+
130+
var request = client.RequestResponseSerializer.Deserialize<SearchRequest>(json)!; <1>
131+
var requestDescriptor = new SearchRequestDescriptor<Person>(request); <2>
132+
```
133+
134+
1. Deserialize JSON payload into the object representation.
135+
2. Create the related descriptor by wrapping the deserialized request object.

docs/reference/serialization.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ mapped_pages:
55

66
# Serialization [serialization]
77

8-
By default, the .NET client for {{es}} uses the Microsoft System.Text.Json library for serialization. The client understands how to serialize and deserialize the request and response types correctly. It also handles (de)serialization of user POCO types representing documents read or written to {{es}}.
8+
By default, the .NET client for {{es}} uses the Microsoft `System.Text.Json` library for serialization. The client understands how to serialize and deserialize the request and response types correctly. It also handles (de)serialization of user POCO types representing documents read or written to {{es}}.
99

1010
The client has two distinct serialization responsibilities - serialization of the types owned by the `Elastic.Clients.Elasticsearch` library and serialization of source documents, modeled in application code. The first responsibility is entirely internal; the second is configurable.
11+
12+
# Source Serialization [source-serialization]
13+
14+
For detailed information on customizing the serialization of user POCO types, please refer to the relevant documentation on[source serialization](https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/serialization.html).
15+
16+
# Serialization of Elasticsearch types [request-response-serialization]
17+
18+
TBD

docs/reference/toc.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ toc:
1111
- file: serialization.md
1212
children:
1313
- file: source-serialization.md
14+
- file: request-response-serialization.md
1415
- file: using-net-client.md
1516
children:
1617
- file: aggregations.md

0 commit comments

Comments
 (0)