Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用ElasticSearch嵌套索引动态对象-StackOverflow异常_C#_<img Src="//i.stack.imgur.com/RUiNP.png" Height="16" Width="18" Alt="" Class="sponsor Tag Img">elasticsearch_Nest - Fatal编程技术网 elasticsearch,nest,C#,elasticsearch,Nest" /> elasticsearch,nest,C#,elasticsearch,Nest" />

C# 使用ElasticSearch嵌套索引动态对象-StackOverflow异常

C# 使用ElasticSearch嵌套索引动态对象-StackOverflow异常,c#,elasticsearch,nest,C#,elasticsearch,Nest,我想使用ElasticSearch Nest客户端为动态对象的集合编制索引。 对象首先由NewtonsoftJson.NET从JSON文件具体化为动态对象,然后由程序进行操作。所有对象都有一个属性“Id”。这应该作为ElasticSearch的“_id”字段。 相同数据记录的“_id”字段必须相同,以便以后能够更新数据。 因为属性“IdProperty”不能添加到动态对象和 映射也无法使用,我被迫采用以下解决方案。 我想保留动态对象,因为我只操作一些属性,而其他属性对我来说不感兴趣 var va

我想使用ElasticSearch Nest客户端为动态对象的集合编制索引。 对象首先由NewtonsoftJson.NET从JSON文件具体化为动态对象,然后由程序进行操作。所有对象都有一个属性“Id”。这应该作为ElasticSearch的“_id”字段。 相同数据记录的“_id”字段必须相同,以便以后能够更新数据。 因为属性“IdProperty”不能添加到动态对象和 映射也无法使用,我被迫采用以下解决方案。 我想保留动态对象,因为我只操作一些属性,而其他属性对我来说不感兴趣

var values = new List<dynamic>();
dynamic obj = new System.Dynamic.ExpandoObject();
obj.Id = "ABC";
obj.SomeValue0 = "12";
obj.SomeValue1 = 99;

values.Add(obj);

var descriptor = new BulkDescriptor();

// Now i want to Index this List
foreach (var doc in values) {
    // Here the StackOverflowException will be thrown
    descriptor.Index<object>(i => i
        .Index("abc")
        .Id(doc.Id)
        .Document(doc));
}

client.Bulk(descriptor);
第一种可能我做错了什么?是否有可能在动态对象上设置“\u id”?

您需要

  • Id
    属性强制转换为
    string
    (或
    Nest.Id
    ,如果它可以是另一种类型,并隐式转换为
    Nest.Id
    ,例如,
    Guid
  • 将对象强制转换为
    对象
  • 这里有一个例子

    var client = new ElasticClient();
    
    var values = new List<dynamic>();
    dynamic obj = new System.Dynamic.ExpandoObject();
    obj.Id = "ABC";
    obj.SomeValue0 = "12";
    obj.SomeValue1 = 99;
    
    values.Add(obj);
    
    var descriptor = new BulkDescriptor();
    
    // Now i want to Index this List
    foreach (var doc in values)
    {
        descriptor.Index<object>(i => i
            .Index("abc")
            .Id((Id)doc.Id)
            .Document((object)doc));
    }
    
    client.Bulk(descriptor);
    
    和成员访问表达式,这看起来与运行时类型解析有关。在上面的示例中,我建议使用匿名类型和您需要的
    列表

  • Id
    属性强制转换为
    string
    (或
    Nest.Id
    ,如果它可以是另一种类型,并隐式转换为
    Nest.Id
    ,例如,
    Guid
  • 将对象强制转换为
    对象
  • 这里有一个例子

    var client = new ElasticClient();
    
    var values = new List<dynamic>();
    dynamic obj = new System.Dynamic.ExpandoObject();
    obj.Id = "ABC";
    obj.SomeValue0 = "12";
    obj.SomeValue1 = 99;
    
    values.Add(obj);
    
    var descriptor = new BulkDescriptor();
    
    // Now i want to Index this List
    foreach (var doc in values)
    {
        descriptor.Index<object>(i => i
            .Index("abc")
            .Id((Id)doc.Id)
            .Document((object)doc));
    }
    
    client.Bulk(descriptor);
    
    和成员访问表达式,这看起来与运行时类型解析有关。在上面的示例中,我建议使用匿名类型和
    列表

    POST http://localhost:9200/_bulk 
    {"index":{"_index":"abc","_type":"object","_id":"ABC"}}
    {"Id":"ABC","SomeValue0":"12","SomeValue1":99}