Skip to content

Commit 6c456f9

Browse files
authored
Add documentation for percolated query use case (#8680)
1 parent e3fb2a6 commit 6c456f9

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

docs/reference/source-serialization.md

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@ mapped_pages:
88
Source serialization refers to the process of (de)serializing POCO types in consumer applications as source documents indexed and retrieved from {{es}}. A source serializer implementation handles serialization, with the default implementation using the `System.Text.Json` library. As a result, you may use `System.Text.Json` attributes and converters to control the serialization behavior.
99

1010
- [Modelling documents with types](#modeling-documents-with-types)
11+
- [Default behavior](#default-behaviour)
12+
- [Using Elasticsearch types in documents](#elasticsearch-types-in-documents)
1113
- [Customizing source serialization](#customizing-source-serialization)
14+
- [Using `System.Text.Json` attributes](#system-text-json-attributes)
15+
- [Configuring custom `JsonSerializerOptions`](#configuring-custom-jsonserializeroptions)
16+
- [Registering custom `System.Text.Json` converters](#registering-custom-converters)
17+
- [Creating a custom `Serializer`](#creating-custom-serializers)
1218
- [Native AOT](#native-aot)
1319

1420
## Modeling documents with types [modeling-documents-with-types]
@@ -51,6 +57,34 @@ The index request is serialized, with the source serializer handling the `MyDocu
5157
}
5258
```
5359

60+
### Using Elasticsearch types in documents [elasticsearch-types-in-documents]
61+
62+
There are various cases where you might have a POCO type that contains an `Elastic.Clients.Elasticsearch` type as one of its properties.
63+
64+
For example, consider if you want to use percolation; you need to store {{es}} queries as part of the `_source` of your document, which means you need to have a POCO that looks like this:
65+
66+
```csharp
67+
using Elastic.Clients.Elasticsearch.QueryDsl;
68+
using Elastic.Clients.Elasticsearch.Serialization;
69+
70+
public class MyPercolationDocument
71+
{
72+
[JsonConverter(typeof(RequestResponseConverter<Query>))] <2>
73+
public Query Query { get; set; } <2>
74+
75+
public string Category { get; set; }
76+
}
77+
```
78+
79+
1. The `Query` property uses the `Elastic.Clients.Elasticsearch.QueryDsl.Query` Elasticsearch type.
80+
2. The `JsonConverter` attribute specifies the `RequestResponseConverter<T>` to be used for the `Query` property. This special converter instructs the `DefaultSourceSerializer` to delegate (de-)serialization to the `RequestResponseSerializer`.
81+
82+
::::{warning}
83+
84+
Failure to strictly use `RequestResponseConverter<T>` for `Elastic.Clients.Elasticsearch` types will most likely result in problems with document (de-)serialization.
85+
86+
::::
87+
5488
## Customizing source serialization [customizing-source-serialization]
5589

5690
The built-in source serializer handles most POCO document models correctly. Sometimes, you may need further control over how your types are serialized.
@@ -348,14 +382,17 @@ var client = new ElasticsearchClient(settings);
348382

349383
1. If implementing `Serializer` is enough, why must we provide an instance wrapped in a factory `Func`?
350384

351-
There are various cases where you might have a POCO type that contains an `Elastic.Clients.Elasticsearch` type as one of its properties. The `SourceSerializerFactory` delegate provides access to the default built-in serializer so you can access it when necessary. For example, consider if you want to use percolation; you need to store {{es}} queries as part of the `_source` of your document, which means you need to have a POCO that looks like this.
385+
There are various cases where you might have a POCO type that contains an `Elastic.Clients.Elasticsearch` type as one of its properties (see [Elasticsearch types in documents](#elasticsearch-types-in-documents)). The `SourceSerializerFactory` delegate provides access to the default built-in serializer so you can access it when necessary. For example, consider if you want to use percolation; you need to store {{es}} queries as part of the `_source` of your document, which means you need to have a POCO that looks like this.
352386

353387
```csharp
354388
using Elastic.Clients.Elasticsearch.QueryDsl;
389+
using Elastic.Clients.Elasticsearch.Serialization;
355390

356391
public class MyPercolationDocument
357392
{
393+
[JsonConverter(typeof(RequestResponseConverter<Query>))]
358394
public Query Query { get; set; }
395+
359396
public string Category { get; set; }
360397
}
361398
```

0 commit comments

Comments
 (0)