/** * A custom serializer would not know how to serialize `QueryContainer` or other NEST types that could appear as part of * the `_source` of a document, therefore a custom serializer needs to have a way to delegate serialization of NEST types * back to NEST's built-in serializer. */ /** * ==== JsonNetSerializer * * We ship a separate {nuget}/NEST.JsonNetSerializer[NEST.JsonNetSerializer] package that helps in composing a custom `SourceSerializer` * using `Json.NET`, that is smart enough to delegate the serialization of known NEST types back to the built-in * `RequestResponseSerializer`. This package is also useful if * * . You want to control how your documents and values are stored and retrieved from Elasticsearch using `Json.NET` * . You want to use `Newtonsoft.Json.Linq` types such as `JObject` within your documents * * The easiest way to hook this custom source serializer up is as follows */ public void DefaultJsonNetSerializer() { var pool = new SingleNodePool(new Uri("http://localhost:9200")); var connectionSettings = new ElasticsearchClientSettings(pool, sourceSerializer: JsonNetSerializer.Default); var client = new ElasticsearchClient(connectionSettings); }
/** * Hooking up the serializer is performed in the `ConnectionSettings` constructor */ public void TheNewContract() { var pool = new SingleNodePool(new Uri("http://localhost:9200")); var connectionSettings = new ElasticsearchClientSettings( pool, sourceSerializer: (builtin, settings) => new VanillaSerializer()); // <1> what the Func? var client = new ElasticsearchClient(connectionSettings); }
public void CanDeserialize() { var json = @"{ ""took"": 61, ""errors"": false, ""items"": [{ ""update"": { ""_index"": ""test"", ""_type"": ""_doc"", ""_id"": ""1"", ""_version"": 2, ""result"": ""updated"", ""_shards"": { ""total"": 2, ""successful"": 1, ""failed"": 0 }, ""_seq_no"": 3, ""_primary_term"": 1, ""get"": { ""_seq_no"": 3, ""_primary_term"": 1, ""found"": true, ""_source"": { ""field1"": ""value1"", ""field2"": ""value2"" } }, ""status"": 200 } }] }" ; var pool = new SingleNodePool(new Uri("http://localhost:9200")); var connection = new InMemoryConnection(Encoding.UTF8.GetBytes(json)); var settings = new ElasticsearchClientSettings(pool, connection); var client = new ElasticsearchClient(settings); var bulkResponse = client.Bulk(new BulkRequest()); bulkResponse.ShouldBeValid(); var item1 = bulkResponse.Items[0].Get; item1.SeqNo.Should().Be(3); item1.PrimaryTerm.Should().Be(1); item1.Found.Should().Be(true); var simpleObject = bulkResponse.Items[0].Get.Source.As <SimpleObject>(); simpleObject.Field1.Should().Be("value1"); simpleObject.Field2.Should().Be("value2"); }
/** * `JsonNetSerializer`'s constructor takes several methods that allow you to control the `JsonSerializerSettings` and modify * the contract resolver from `Json.NET`. */ public void DefaultJsonNetSerializerFactoryMethods() { var pool = new SingleNodePool(new Uri("http://localhost:9200")); var connectionSettings = new ElasticsearchClientSettings(pool, sourceSerializer: (builtin, settings) => new JsonNetSerializer( builtin, settings, () => new JsonSerializerSettings { NullValueHandling = NullValueHandling.Include }, resolver => resolver.NamingStrategy = new SnakeCaseNamingStrategy() )); var client = new ElasticsearchClient(connectionSettings); }
public static ElasticsearchClientSettings CreateConnectionSettings( object response, int statusCode = 200, Func <ElasticsearchClientSettings, ElasticsearchClientSettings> modifySettings = null, string contentType = RequestData.MimeType, Exception exception = null, Serializer serializer = null ) { serializer ??= TestClient.Default.RequestResponseSerializer; byte[] responseBytes; switch (response) { case string s: responseBytes = Encoding.UTF8.GetBytes(s); break; case byte[] b: responseBytes = b; break; default: { responseBytes = contentType == RequestData.MimeType ? serializer.SerializeToBytes(response, TestClient.Default.ElasticsearchClientSettings.MemoryStreamFactory) : Encoding.UTF8.GetBytes(response.ToString()); break; } } var headers = new Dictionary <string, IEnumerable <string> > { { "x-elastic-product", new[] { "Elasticsearch" } } }; var connection = new InMemoryConnection(responseBytes, statusCode, exception, contentType, headers); var nodePool = new SingleNodePool(new Uri("http://localhost:9200")); var defaultSettings = new ElasticsearchClientSettings(nodePool, connection) .DefaultIndex("default-index"); var settings = modifySettings != null?modifySettings(defaultSettings) : defaultSettings; return(settings); }
/** * Hooking up the serializer and using it is as follows */ [U] public void UsingJsonNetSerializer() { var pool = new SingleNodePool(new Uri("http://localhost:9200")); var connectionSettings = new ElasticsearchClientSettings( pool, connection: new InMemoryConnection(), // <1> an _in-memory_ connection is used here for example purposes. In your production application, you would use an `ITransportClient` implementation that actually sends a request. sourceSerializer: (builtin, settings) => new MyFirstCustomJsonNetSerializer(builtin, settings)) .DefaultIndex("my-index"); //hide connectionSettings.DisableDirectStreaming(); var client = new ElasticsearchClient(connectionSettings); /** Now, if we index an instance of our document type */ var document = new MyDocument { Id = 1, Name = "My first document", OwnerId = 2 }; var indexResponse = client.Index(document, d => { }); /** it serializes to */ //json var expected = new { id = 1, name = "My first document", file_path = (string)null, owner_id = 2, sub_documents = (object)null }; /** * which adheres to the conventions of our configured `MyCustomJsonNetSerializer` serializer. */ // hide Expect(expected, preserveNullInExpected: true).FromRequest(indexResponse); }