C# 从索引中排除属性
我已经创建了下面的对象,它将映射到ElasticSearch类型。我想将C# 从索引中排除属性,c#,
elasticsearch,nest,C#,
elasticsearch,Nest,我已经创建了下面的对象,它将映射到ElasticSearch类型。我想将UnivId属性从索引中排除: [ElasticType(Name = "Type1")] public class Type1 { // To be ignored public string UnivId { get; set; } [ElasticProperty(Name="Id")] public int Id { get; set; } [ElasticProperty
UnivId
属性从索引中排除:
[ElasticType(Name = "Type1")]
public class Type1
{
// To be ignored
public string UnivId { get; set; }
[ElasticProperty(Name="Id")]
public int Id { get; set; }
[ElasticProperty(Name = "descSearch")]
public string descSearch { get; set; }
}
您应该能够设置
ElasticProperty
属性的OptOut
值,如下所示:
[弹性属性(OptOut=true)]
公共字符串UnivId{get;set;}
在NEST 2.0 ElasticPropertyAttribute中,每个类型的属性(StringAttribute、DateAttribute…)将被替换。我使用Ignore参数排除属性
字符串示例:
[String(Ignore = true)]
public string Id {get;set;}
如果使用Nest 5.0+,有几种忽略字段的方法:
Ignore
属性应起作用:
using Nest;
[Ignore]
public string UnivId { get; set; }
JsonIgnore
也可以使用,因为Newtonsoft.Json
是Nest使用的默认序列化程序
另一种方法是使用与属性关联的特定类型。例如,由于它是
字符串
,因此使用文本
属性:
[Text(Ignore = true)]
public string UnivId { get; set; }
或者如果一个int
使用Number
:
[Number(Ignore = true)]
public int Id { get; set; }
此外,可以使用
忽略映射,而不是在属性上使用显式属性。DefaultMappingForOMG,多么糟糕的属性名称,我也花了一段时间才弄清楚。。感谢oput和[ElasticProperty(Index=FieldIndexOption.No)]之间的区别是什么?他们做同样的工作吗?只是为了那些最终来到这里的人。在这个url上解释了这两种方法之间的区别。有谁知道如何在Nest 2中做到这一点吗?@beruic,Alexandre B刚刚透露了一些关于它的信息,汉克人!顺便说一句,你知道如何在代码库映射(NEST 2.0)中忽略嵌套属性吗?@I.G.Pascual查看automap文档以了解忽略属性的方法-@RussCam他们什么时候才更新这些文档??太棒了!
var connectionSettings = new ConnectionSettings()
.DefaultMappingFor<Type1>(m => m.Ignore(p => p.UnivId);
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public string UnivId { get; set; }