elasticsearch,nest,C#,elasticsearch,Nest" /> elasticsearch,nest,C#,elasticsearch,Nest" />

C# 基于第三方ElasticSearch解决方案在解决方案中创建对象模型

C# 基于第三方ElasticSearch解决方案在解决方案中创建对象模型,c#,elasticsearch,nest,C#,elasticsearch,Nest,处理JSON时,制作C#模型非常容易。您可以在VisualStudio中粘贴“特殊”,或者使用许多可用的在线工具之一 ElasticSearch响应显然是JSON,这意味着,如果您能够获得响应的JSON,就可以开始了。但是,如果您只有一个连接字符串,并且只想将所有ElasticSearch对象“映射”到您的C#代码中,那么您该如何做呢 我的问题: 有没有一种方法可以查看ElasticSearch实例中的所有字段/数据,然后轻松获取JSON,这样您就可以获取强类型模型?您可以查询ElasticSe

处理JSON时,制作C#模型非常容易。您可以在VisualStudio中粘贴“特殊”,或者使用许多可用的在线工具之一

ElasticSearch响应显然是JSON,这意味着,如果您能够获得响应的JSON,就可以开始了。但是,如果您只有一个连接字符串,并且只想将所有ElasticSearch对象“映射”到您的C#代码中,那么您该如何做呢

我的问题:


有没有一种方法可以查看ElasticSearch实例中的所有字段/数据,然后轻松获取JSON,这样您就可以获取强类型模型?

您可以查询ElasticSearch的映射。映射将包含在C#中构建模型所需的所有信息(但我认为您仍然需要手工构建)。使用上一个问题中的数据库的示例:

var settings = new ConnectionSettings(new Uri("http://distribution.virk.dk/cvr-permanent"));
var client = new ElasticClient(settings);
// get mappings for all indexes and types
var mappings = client.GetMapping<JObject>(c => c.AllIndices().AllTypes());
foreach (var indexMapping in mappings.Indices) {
    Console.WriteLine($"Index {indexMapping.Key.Name}"); // index name
    foreach (var typeMapping in indexMapping.Value.Mappings) {
        Console.WriteLine($"Type {typeMapping.Key.Name}"); // type name
        foreach (var property in typeMapping.Value.Properties) { 
            // property name and type. There might be more useful info, check other properties of `typeMapping`
            Console.WriteLine(property.Key.Name + ": " + property.Value.Type);
            // some properties are themselves objects, so you need to go deeper
            var subProperties = (property.Value as ObjectProperty)?.Properties;
            if (subProperties != null) {
                // here you can build recursive function to get also sub-properties
            }
        }
    }
}
var设置=新连接设置(新Uri(“http://distribution.virk.dk/cvr-permanent"));
var客户端=新的ElasticClient(设置);
//获取所有索引和类型的映射
var-mappings=client.GetMapping(c=>c.AllIndices().AllTypes());
foreach(映射中的var indexMapping.index){
Console.WriteLine($“Index{indexMapping.Key.Name}”);//索引名
foreach(indexMapping.Value.Mappings中的变量类型映射){
Console.WriteLine($“Type{typeMapping.Key.Name}”);//类型名称
foreach(typeMapping.Value.Properties中的var属性){
//属性名称和类型。可能有更有用的信息,请检查`typeMapping的其他属性`
Console.WriteLine(property.Key.Name+“:”+property.Value.Type);
//有些属性本身就是对象,因此需要深入了解
var子属性=(property.Value作为ObjectProperty)?.Properties;
if(子属性!=null){
//在这里,您可以构建递归函数来获取子属性
}
}
}
}