Unable to insert dynamic object to Elastic Search

2019-09-09 19:39发布

问题:

I am creating a dynamic object. I assign the values via IDictionary. Add the collections of the IDictionary to the object. Then I add the dynamic object to Elastic Search using NEST code. It throws me stackoverflow exception."An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll"

Here is what I have tried.

 var node = new Uri("http://localhost:9200");
            var settings = new ConnectionSettings(node,defaultIndex: "test-index");
            var client = new ElasticClient(settings);

            try
            {
                dynamic x = new ExpandoObject();
                Dictionary<string, object> dic = new Dictionary<string, object>();
                dic.Add("NewProp", "test1");
                dic.Add("NewProp3", "test1");
                x = dic;
                var index3 = client.Index(x);
            }
            catch (Exception ex)
            {
                string j = ex.StackTrace;
            }

I need to create an index in ElasticSearch, using a dynamic object, because I will be having an excel work book consisting of over 300 worksheet, and each and every sheet will be named as type, and the contents inside the worksheet will be the _source.

In the above example 'x' the dynamic object created is the name of the worksheet, and the values added into the dictionary are the rows and columns of excel sheet.

Where am I going wrong.

Regards, Hema

回答1:

I belive you can skip ExpandoObject and just index Dictionary<string, object>().

var dictionary = new Dictionary<string, object>();
dictionary.Add("NewProp", "test1");
dictionary.Add("NewProp3", "test1");

client.Index(dictionary);