基于第三方 ElasticSearch 解决方案在解决方案中制作对象模型

Making object models in solution based on third party ElasticSearch solution

当你处理 JSON 时,制作 C# 模型真的很容易。您要么 Paste special Visual Studio,要么使用众多可用的在线工具之一。

ElasticSearch 响应显然是 JSON,这意味着,如果您可以获得响应 JSON,那么您就可以开始了。但是,如果您只有一个连接字符串并且只是想 "map" 所有 ElasticSearch 对象到您的 C# 代码中 - 您该怎么做?

我的问题:

有没有办法在 ElasticSearch 实例中查看所有 fields/data,然后轻松获得 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
            }
        }
    }
}