排除财产被编入索引

时间:2014-04-14 15:16:52

标签: 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(Name = "descSearch")]
    public string descSearch { get; set; }
}

3 个答案:

答案 0 :(得分:19)

您应该可以设置OptOut属性的ElasticProperty值,如下所示:

 [ElasticProperty(OptOut = true)]
 public string UnivId { get; set; }

答案 1 :(得分:17)

在NEST 2.0中,ElasticPropertyAttribute被每个类型的属性(StringAttribute,DateAttribute ...)替换。我使用Ignore参数来排除属性。

字符串的例子:

[String(Ignore = true)]
public string Id {get;set;}

答案 2 :(得分:0)

如果使用Nest 5.0+,则per the docs有几种忽略字段的方法:


Ignore属性应该起作用:

using Nest;

[Ignore]
public string UnivId { get; set; }

JsonIgnore也可以使用,因为Newtonsoft.Json是Nest使用的默认序列化程序。


另一种方法是使用与属性关联的类型特定的attribute mappings。例如,由于它是string,请使用Text属性:

[Text(Ignore = true)]
public string UnivId { get; set; }

或者如果int使用Number

[Number(Ignore = true)]
public int Id { get; set; }

此外,可以在.DefaultMappingFor<...上使用ConnectionSettings来忽略映射,而不必在属性上使用显式属性(有关更多详细信息,请参见docs

var connectionSettings = new ConnectionSettings()
    .DefaultMappingFor<Type1>(m => m.Ignore(p => p.UnivId);

但是,如果要有条件地忽略属性(如果该值为空),则使用以下Newtonsoft.Json attribute with null handling setting

[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public string UnivId { get; set; }

当我在文档上执行partial updates时,我发现上述方法很有用,但希望重新使用相同的C#类进行索引编制,并避免覆盖索引中的现有值。

相关问题