- .NET Clients: other versions:
- Introduction
- Breaking changes
- API Conventions
- Elasticsearch.Net - Low level client
- NEST - High level client
- Troubleshooting
- Search
- Query DSL
- Full text queries
- Term level queries
- Exists Query Usage
- Fuzzy Date Query Usage
- Fuzzy Numeric Query Usage
- Fuzzy Query Usage
- Ids Query Usage
- Prefix Query Usage
- Date Range Query Usage
- Numeric Range Query Usage
- Term Range Query Usage
- Regexp Query Usage
- Term Query Usage
- Terms List Query Usage
- Terms Lookup Query Usage
- Terms Query Usage
- Type Query Usage
- Wildcard Query Usage
- Compound queries
- Joining queries
- Geo queries
- Geo Bounding Box Query Usage
- Geo Distance Query Usage
- Geo Distance Range Query Usage
- Geo Hash Cell Query Usage
- Geo Polygon Query Usage
- Geo Shape Circle Query Usage
- Geo Shape Envelope Query Usage
- Geo Shape Geometry Collection Query Usage
- Geo Shape Indexed Shape Query Usage
- Geo Shape Line String Query Usage
- Geo Shape Multi Line String Query Usage
- Geo Shape Multi Point Query Usage
- Geo Shape Multi Polygon Query Usage
- Geo Shape Point Query Usage
- Geo Shape Polygon Query Usage
- Specialized queries
- Span queries
- NEST specific queries
- Aggregations
- Metric Aggregations
- Average Aggregation Usage
- Cardinality Aggregation Usage
- Extended Stats Aggregation Usage
- Geo Bounds Aggregation Usage
- Geo Centroid Aggregation Usage
- Max Aggregation Usage
- Min Aggregation Usage
- Percentile Ranks Aggregation Usage
- Percentiles Aggregation Usage
- Scripted Metric Aggregation Usage
- Stats Aggregation Usage
- Sum Aggregation Usage
- Top Hits Aggregation Usage
- Value Count Aggregation Usage
- Bucket Aggregations
- Adjacency Matrix Usage
- Children Aggregation Usage
- Date Histogram Aggregation Usage
- Date Range Aggregation Usage
- Filter Aggregation Usage
- Filters Aggregation Usage
- Geo Distance Aggregation Usage
- Geo Hash Grid Aggregation Usage
- Global Aggregation Usage
- Histogram Aggregation Usage
- Ip Range Aggregation Usage
- Missing Aggregation Usage
- Nested Aggregation Usage
- Range Aggregation Usage
- Reverse Nested Aggregation Usage
- Sampler Aggregation Usage
- Significant Terms Aggregation Usage
- Terms Aggregation Usage
- Pipeline Aggregations
- Average Bucket Aggregation Usage
- Bucket Script Aggregation Usage
- Bucket Selector Aggregation Usage
- Cumulative Sum Aggregation Usage
- Derivative Aggregation Usage
- Extended Stats Bucket Aggregation Usage
- Max Bucket Aggregation Usage
- Min Bucket Aggregation Usage
- Moving Average Ewma Aggregation Usage
- Moving Average Holt Linear Aggregation Usage
- Moving Average Holt Winters Aggregation Usage
- Moving Average Linear Aggregation Usage
- Moving Average Simple Aggregation Usage
- Percentiles Bucket Aggregation Usage
- Serial Differencing Aggregation Usage
- Stats Bucket Aggregation Usage
- Sum Bucket Aggregation Usage
- Matrix Aggregations
- Metric Aggregations
WARNING: Version 5.x has passed its EOL date.
This documentation is no longer being maintained and may be removed. If you are running this version, we strongly advise you to upgrade. For the latest information, see the current release documentation.
Modifying the default serializer
editModifying the default serializer
editIn Changing serializers, you saw how it is possible to provide your own serializer implementation to NEST. A more common scenario is the desire to change the settings on the default JSON.Net serializer.
There are a couple of ways in which this can be done, depending on what it is you need to change.
Modifying settings using SerializerFactory
editThe default implementation of ISerializerFactory
allows a delegate to be passed that can change
the settings for JSON.Net serializers created by the factory
var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200")); var connection = new HttpConnection(); var connectionSettings = new ConnectionSettings(pool, connection, new SerializerFactory((settings, values) => { settings.NullValueHandling = NullValueHandling.Include; settings.TypeNameHandling = TypeNameHandling.Objects; })); var client = new ElasticClient(connectionSettings);
Here, the JSON.Net serializer is configured to always serialize null
values and
include the .NET type name when serializing to a JSON object structure.
Modifying settings using a custom ISerializerFactory
editIf you need more control than passing a delegate to SerializerFactory
provides, you can also
implement your own ISerializerFactory
and derive an IElasticsearchSerializer
from the
default JsonNetSerializer
.
Here’s an example of doing so that effectively achieves the same configuration as in the previous example. First, the custom factory and serializer are implemented
public class CustomJsonNetSerializerFactory : ISerializerFactory { public IElasticsearchSerializer Create(IConnectionSettingsValues settings) { return new CustomJsonNetSerializer(settings); } public IElasticsearchSerializer CreateStateful(IConnectionSettingsValues settings, JsonConverter converter) { return new CustomJsonNetSerializer(settings, converter); } } public class CustomJsonNetSerializer : JsonNetSerializer { public CustomJsonNetSerializer(IConnectionSettingsValues settings) : base(settings) { base.OverwriteDefaultSerializers(ModifyJsonSerializerSettings); } public CustomJsonNetSerializer(IConnectionSettingsValues settings, JsonConverter statefulConverter) : base(settings, statefulConverter) { base.OverwriteDefaultSerializers(ModifyJsonSerializerSettings); } private void ModifyJsonSerializerSettings(JsonSerializerSettings settings, IConnectionSettingsValues connectionSettings) { settings.NullValueHandling = NullValueHandling.Include; settings.TypeNameHandling = TypeNameHandling.Objects; } }
Then, create a new instance of the factory to ConnectionSettings
var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200")); var connection = new HttpConnection(); var connectionSettings = new ConnectionSettings(pool, connection, new CustomJsonNetSerializerFactory()); var client = new ElasticClient(connectionSettings);
Any custom serializer that derives from JsonNetSerializer
wishing to change the settings for the JSON.Net
serializer, must do so using the OverwriteDefaultSerializers
method in the constructor of the derived
serializer.
NEST includes many custom changes to the IContractResolver
that the JSON.Net serializer uses to resolve
serialization contracts for types. Examples of such changes are:
- Allowing contracts for concrete types to be inherited from interfaces that they implement
- Special handling of dictionaries to ensure dictionary keys are serialized verbatim
- Explicitly implemented interface properties are serialized in requests
It’s important therefore that these changes to IContractResolver
are not overwritten by a serializer derived
from JsonNetSerializer
.
Adding contract JsonConverters
editIf you want to register custom json converters without attributing your classes you can register Functions that given a type return a JsonConverter. This is cached as part of the types json contract so once Json.NET knows a type has a certain converter it won’t ask anymore for the duration of the application.
Override ContractConverters
getter property and have it return a list of these functions
public class CustomContractsJsonNetSerializer : CustomJsonNetSerializer { public CustomContractsJsonNetSerializer(IConnectionSettingsValues settings) : base(settings) { } public CustomContractsJsonNetSerializer(IConnectionSettingsValues settings, JsonConverter statefulConverter) : base(settings, statefulConverter) { } protected override IList<Func<Type, JsonConverter>> ContractConverters { get; } = new List<Func<Type, JsonConverter>> { ((t) => t == typeof(Project) ? new MyCustomJsonConverter() : null) }; } public class MyCustomJsonConverter : JsonConverter { public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { } public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) => null; public override bool CanConvert(Type objectType) => false; }
On this page