Elasticsearch-net: Unable to set ConnectionSettings SetJsonSerializerSettingsModifier in latest version.

Created on 17 Feb 2016  路  10Comments  路  Source: elastic/elasticsearch-net

In my app (using v.1.7.1) I have been setting JSON TypeNameHandling through the settings as shown:

"settings.SetJsonSerializerSettingsModifier(m => m.TypeNameHandling = Newtonsoft.Json.TypeNameHandling.Auto);"

This does not work in v.2+ and I have yet to find the documentation on how to accomplish this, so I've had to revert back to the previous version. How is this done in the new version?

All 10 comments

I'm trying to do the same thing, and it looks like documentation is pretty sparse. How do I retain a loosely-structured document store in NEST 2.0.2 ?

Concrete Example:

public abstract class MyMetadataClass
{
    public string Id { get; set; }
    public string MyData { get; set; }
}

public class MyConcreteClass1 : MyMetadataClass
{
    public string MyConcreteData1 { get; set; }
}

public class MyConcreteClass2 : MyMetadataClass
{
    public string MyConcreteData2 { get; set; }
}

class MyProgram
{
    static void Main(string[] args)
    {
        List<MyMetadataClass> myList = new List<MyMetadataClass>(){
            new MyConcreteClass1(){
                Id = "one",
                MyData = "blah1",
                MyConcreteData1 = "concrete1"
            },
            new MyConcreteClass2(){
                Id = "two",
                MyData = "blah2",
                MyConcreteData2 = "concrete2"
            }
        };

        //NEST 1.7 code to make serialization and deserialization work:
        var settings = new ConnectionSettings(node);
        settings.SetJsonSerializerSettingsModifier(m => m.TypeNameHandling = Newtonsoft.Json.TypeNameHandling.Auto);
        var client = new ElasticClient(settings);

        //Serialize and create: (essentially)
        client.Index(new { Test = myList });

        //Deserialize and read: (essentially)
        var result = client.Search<MyMetadataClass>(s => s
            .From(0)
            .Size(10));

        //Data looks like this:
        //"hits": {
        //"total": 1,
        //"max_score": 1,
        //"hits": [
        //  {
        //    "_index": "dev_test",
        //    "_type": "aNewTypeGoesHere",
        //    "_id": "AVLNB0t2aT52lACEq5HX",
        //    "_score": 1,
        //    "_source": {
        //      "id": "AVLNB0t2aT52lACEq5HX",
        //      "revision": 0,
        //      "Test": [
        //        {
        //          "$type": "ConsoleApplication1.MyConcreteClass1, ConsoleApplication1",
        //          "id": "one",
        //          "myData": "blah1",
        //          "myConcreteData1": "concrete1"
        //        },
        //        {
        //          "$type": "ConsoleApplication1.MyConcreteClass2, ConsoleApplication1",
        //          "id": "two",
        //          "myData": "blah2",
        //          "myConcreteData2": "concrete2"
        //        }
        //      ]
        //   }
        // ]}


        //NEST 2.0.2 no longer exposes ConnectionSettings(node).SetJsonSerializerSettingsModifier

        // How do I retain a loosely-structured document store in NEST 2.0.2 ??
    }
}

Hi @jguziejka, @TadKaput take a look here.

Also take a look at the Connecting tests documentation for creating a derived JsonSerializer to modify serializer settings and register custom contract converters

``` c#
public class MyJsonNetSerializer : JsonNetSerializer
{
public MyJsonNetSerializer(IConnectionSettingsValues settings) : base(settings) { }

protected override void ModifyJsonSerializerSettings(JsonSerializerSettings settings)
{
    // make any modifications to settings here
}


protected override IList<Func<Type, JsonConverter>> ContractConverters => new List<Func<Type, JsonConverter>>()
{
    // register any additional contract converters here
};

}

then to use, pass a `Func<ConnectionSettings, IElasticsearchSerializer>` to `ConnectionSettings` that will be called to construct an instance of your serializer

``` c#
var connectionPool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
var settings = new ConnectionSettings(connectionPool, s => new MyJsonNetSerializer(s));
var client = new ElasticClient(settings);

Hi, I'm updating some code to work with Nest 5 and this method isn't working anymore. Error is:

CS0115
'MyJsonNetSerializer.ModifyJsonSerializerSetting(JsonSerializerSettings)': no suitable method found to override

Hmmm, it looks like ModifyJsonSerializerSetting(JsonSerializerSettings) method was replaced with OverwriteDefaultSerializers(Action settingsModifier)?

Looking at the tests for 5.x I think I solved my own problem, in case someone else wanders into here.
https://github.com/elastic/elasticsearch-net/blob/5.x/src/Tests/ClientConcepts/LowLevel/Connecting.doc.cs#L289

@gnudle Glad you found it; the file you found is also used the generate the documentation on the Elastic site for the .NET client 馃槃

I want to achieve what is presented in this outdated document, but I can't.
http://forloop.co.uk/blog/geospatial-search-with-elasticsearch-and-nest#sthash.EEyOr67Z.dpbs
Can some one point me to the right direction?

@HarelM do you remember if you managed to achieve / make it work the example from that doc?
Is there any updated version of that article, @russcam ?

Thank you very much!

I managed to make it work and am using NTS with NEST for quite some time in production.
Feel free to copy from here:
https://github.com/IsraelHikingMap/Site

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kaaresylow picture kaaresylow  路  14Comments

niemyjski picture niemyjski  路  13Comments

rianjs picture rianjs  路  18Comments

Mpdreamz picture Mpdreamz  路  21Comments

anuragdewangan20 picture anuragdewangan20  路  27Comments