Elasticsearch.Net deserialize to my type

According to the documentation on Elasticsearch.Net you can provide your own type that Elasticsearch.Net will deserialize into, like this:

ElasticsearchLowLevelClient.Index<MyCustomType>(indexName, type, id, obj);

Which only works if the type is defined like this:

public class ElasticsearchResult
    {
        public string _index { get; set; }
        public string _type { get; set; }
        public string _id { get; set; }
        public int _version { get; set; }
        public string result { get; set; }
        public Shard _shards { get; set; }
        public bool created { get; set; }
    }

But I want this type to look like this:

public class ElasticsearchResult
    {
        public string Index { get; set; }
        public string Type { get; set; }
        public string Id { get; set; }
        public int Version { get; set; }
        public string Result { get; set; }
        public Shard Shards { get; set; }
        public bool Created { get; set; }
    }

How do I achieve that? I have tried to use the JsonProperty attribute [JsonProperty(PropertyName = "_shards")], but that does not work.

The low level client has no dependency on JSON.Net, so JsonProperty attributes will not work. It has a simple JSON serializer.

I'd strongly recommend going with NEST, the high level client for cases like this. NEST internally uses JSON.Net, so does understand JsonProperty attributes, although you probably don't need them as known response values are already mapped by NEST to strong types.

In using NEST, you also have access to the low level client in cases where you want to use it, through the .LowLevel property on the high level client. An advantage here is that the low level client will also use the internal serializer that uses JSON.Net so attributes will work, as well as returning strongly typed NEST responses from low level calls. Check out the note in the Getting Started "Search" section of the documentation.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.