|
| 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 | +A few request and response types in Elasticsearch do not support JSON bodies and therefore cannot be serialized in the above described way. |
| 82 | + |
| 83 | +Two prominent examples are the `Bulk` or `MultiGet` APIs, which use NDJSON bodies. Binary responses also occur in some cases. |
| 84 | + |
| 85 | +## Serializing Elasticsearch descriptor types |
| 86 | + |
| 87 | +Descriptor types do not support direct (de-)serialization. |
| 88 | + |
| 89 | +However, starting with client versions 8.19 and 9.0, descriptor types support bidirectional conversion to/from the corresponding object representation. |
| 90 | + |
| 91 | +We can use this functionality to indirectly (de)serialize descriptor types. |
| 92 | + |
| 93 | +### Serialization |
| 94 | + |
| 95 | +```csharp |
| 96 | +using Elastic.Clients.Elasticsearch; |
| 97 | +using Elastic.Transport.Extensions; |
| 98 | + |
| 99 | +var requestDescriptor = new SearchRequestDescriptor<Person>() |
| 100 | + .Query(q => q |
| 101 | + .Match(m => m |
| 102 | + .Field(p => p.FirstName) |
| 103 | + .Query("Florian") |
| 104 | + ) |
| 105 | + ); |
| 106 | + |
| 107 | +var request = (SearchRequest)requestDescriptor; <1> |
| 108 | +var json = client.RequestResponseSerializer.SerializeToString(request); <2> |
| 109 | +``` |
| 110 | + |
| 111 | +1. Unwrap the request by casting the descriptor to the related object representation. |
| 112 | +2. Serialize the unwrapped request instance. |
| 113 | + |
| 114 | +### Deserialization |
| 115 | + |
| 116 | +```csharp |
| 117 | +using Elastic.Clients.Elasticsearch; |
| 118 | +using Elastic.Transport.Extensions; |
| 119 | + |
| 120 | +var json = """ |
| 121 | +{ |
| 122 | + "query": { |
| 123 | + "match": { |
| 124 | + "firstName": { |
| 125 | + "query": "Florian" |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | +} |
| 130 | +"""; |
| 131 | + |
| 132 | +var request = client.RequestResponseSerializer.Deserialize<SearchRequest>(json)!; <1> |
| 133 | +var requestDescriptor = new SearchRequestDescriptor<Person>(request); <2> |
| 134 | +``` |
| 135 | + |
| 136 | +1. Deserialize JSON payload into the object representation. |
| 137 | +2. Create the related descriptor by wrapping the deserialized request object. |
0 commit comments