Closed ayonix closed 6 years ago
@ayonix would you be able to provide a small but complete example e.g. the classes that you're serializing, creating the index and indexing some sample documents?
https://gist.github.com/ayonix/ec8555c6e96ee4a05dc44c6c956ca92d
The problem seems to be related to deserializing with Newtonsoft.Json, not nested objects. Indexing the object directly works, but after deserializing it indexes properties as empty arrays.
Thanks for the example @ayonix 👍 OK, I can see what the issue is.
object doc = JsonConvert.DeserializeObject<object>(docString);
with Newtonsoft.Json, this actually returns a Newtonsoft.Json.Linq.JObject
.
In versions of NEST prior to 6.x, this would have worked fine because NEST has a dependency on Newtonsoft.Json for serialization, and it knows how to handle Newtonsoft.Json.Linq.JObject
in a special way.
In NEST 6.x+ however, the dependency on Newtonsoft.Json is IL-merged, internalized and re-namespaced to Nest.Json. When it comes to handling Newtonsoft.Json.Linq.JObject
in a special way however, it does not know how to because the internal serializer knows nothing about types in the Newtonsoft.Json namespace.
If you'd like to keep using Newtonsoft.Json as the serializer for your documents, you can install the NEST.JsonNetSerializer nuget package and hook up JsonNetSerializer
as the serializer to use
var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
var connectionSettings =
new ConnectionSettings(pool, sourceSerializer: JsonNetSerializer.Default);
var client = new ElasticClient(connectionSettings);
Your example will then serialize as expected.
When it comes to handling Newtonsoft.Json.Linq.JObject in a special way however, it does not know how to because the internal serializer knows nothing about types in the Newtonsoft.Json namespace.
Should this actually be fixed in the "special way?" I ran into this same issue as well with a nested object's array field deserialization. Is it at least documented somewhere that users should switch serializers when using nested objects and array fields?
NEST/Elasticsearch.Net version: 6.0.2
Elasticsearch version: 6.2.3
Description of the problem including expected versus actual behavior: NEST serializes the nested object in a wrong way and thus indexes incorrect values. Nested Values get serialized as empty Arrays instead of strings, serializing the same object with Newtonsoft.Json 11 yields the expected JSON representation.
Steps to reproduce:
Provide
ConnectionSettings
(if relevant): Standard ElasticClient with some default indexProvide
DebugInformation
(if relevant):JsonConvert.SerializeObject(entry):
The Mapping