【问题标题】:Exclude property from being indexed将属性排除在索引之外
【发布时间】:2014-05-28 15:13:07
【问题描述】:

我在下面创建了将映射到 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; }
}

【问题讨论】:

    标签: c# elasticsearch nest


    【解决方案1】:

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

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

    【讨论】:

    • OMG,多么糟糕的属性名称,我也花了一段时间才弄明白。谢谢
    • opput和[ElasticProperty(Index = FieldIndexOption.No)]有什么区别?他们做同样的工作吗?
    • 只为永远来到这里的人们。此网址上解释了这两者之间的区别。 stackoverflow.com/questions/34748258/…
    • 有谁知道如何在 Nest 2 中做到这一点?
    • @beruic, stackoverflow.com/a/35992605/443310 Alexandre B 刚刚给出了一些启示
    【解决方案2】:

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

    字符串示例:

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

    【讨论】:

    • 谢谢你!顺便说一句,您知道如何忽略代码库映射中的嵌套属性(在 NEST 2.0 中)吗?
    • @I.G.Pascual 查看自动映射文档以了解忽略属性的方法 - elastic.co/guide/en/elasticsearch/client/net-api/current/…
    • @RussCam 他们什么时候更新了这些文档??太棒了!
    【解决方案3】:

    如果使用 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; }
    

    此外,可以在 ConnectionSettings 上使用 .DefaultMappingFor<... 忽略映射,而不是在属性上使用显式属性(有关详细信息,请参阅 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# 类进行索引并避免覆盖索引中的现有值。

    【讨论】:

      猜你喜欢
      • 2010-11-18
      • 2018-04-19
      • 1970-01-01
      • 2016-02-19
      • 2015-05-20
      • 2016-12-13
      • 2012-11-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多