gpt4 book ai didi

c# - 如何告诉 Nest ElasticSearch 只使用 InterfaceProperties

转载 作者:行者123 更新时间:2023-12-03 02:03:51 25 4
gpt4 key购买 nike

我们正在研究 ElasticSearch (Nest),但遇到了问题。
在我们的应用程序中,我们只想为我们的对象使用接口(interface)。在这些接口(interface)中,我希望能够设置要索引的属性和不索引的属性。

public interface ISomething {
string Name {get;set;}
int Id {get;set;}
}

public class Something: ISomething {
public string Name { get;set; }
public int Id {get;set;}
// This property should not be visible
public string VerySecretCode {get;set}
}

当我们通过创建一个新的 ElasticClient() 来索引一个接口(interface)时,该对象的所有属性都被放入 json 中。是否可以告诉 ElasticClient 我们只希望显示接口(interface)公开的属性?

希望任何人都可以提供帮助。

最佳答案

将数据模型与域模型分开是一种最佳实践。我建议您创建一个新的类/接口(interface) ISomethingForES。然后,您可以使用 Automapper 或类似的东西为您映射字段。优点是您可以使用不属于您的域模型的 ES 特定属性来注释 SomethingForES。这是一个例子:

[ElasticType(Name = "type123")]
public class SomethingForES: ISomethingForES {
public string Name { get;set; }
public int Id {get;set;}
}

关于c# - 如何告诉 Nest ElasticSearch 只使用 InterfaceProperties,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28744993/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com