【问题标题】:How to automap in Nest without writing to the elasticsearch index?如何在不写入弹性搜索索引的情况下在 Nest 中自动映射?
【发布时间】:2017-09-26 17:29:59
【问题描述】:

是否可以利用 NEST auto-mapping features 获取 Nest 属性和类型对象,而无需通过 PUT 映射和创建索引 API 将它们实际写入弹性索引?

例如,我想自动映射这个 CLR 类公司:

public class Company
{
    public string Name { get; set; }
}

并将弹性映射存储到如下变量中:

Nest.TypeMapping typeCo = null; // for the mapped Company type
Nest.IProperty propCoName = null;  // for the mapped Company Name property 

但不写公司映射到索引。

我可以写入临时索引作为解决方法,但我怀疑这不是必需的。

使用 elasticsearch 5.x 和 Nest 5。

【问题讨论】:

  • 我是否正确理解您的目标是通过已经存在并且具有与您的模型相关的结构的索引来查询和反序列化 Elastic 文档值?
  • @MikeMichaels 不。我想将 .NET 对象序列化为 Nest(也是 CLR)数据类型,但实际上并不写入索引。有点像用 NOOP 替换文字,但保留可能映射的结果。

标签: elasticsearch nest elasticsearch-net


【解决方案1】:

根据您的具体需要,您可以采取几种不同的方法

使用PropertyWalker

var walker = new PropertyWalker(typeof(Company), null);   
var properties = walker.GetProperties();

将提供由自动映射推断的 IProperty 类型。

使用TypeMappingDescriptor<T>

var descriptor = (ITypeMapping)new TypeMappingDescriptor<Company>()
    .AutoMap();

除了ITypeMapping 的任何其他属性外,还将在从自动映射推断的.Properties 属性中提供IProperty 类型。这里需要使用描述符而不是TypeMapping,因为描述符具有.AutoMap() 方法。还需要强制转换为接口,因为接口属性是显式实现的。

【讨论】:

    猜你喜欢
    • 2015-07-21
    • 1970-01-01
    • 2015-06-04
    • 2021-05-18
    • 1970-01-01
    • 2014-06-07
    • 2021-05-20
    • 1970-01-01
    • 2020-10-09
    相关资源
    最近更新 更多