Exclude property from being indexed

后端 未结 3 690
失恋的感觉
失恋的感觉 2020-12-31 02:19

I have created below object which will be mapped to ElasticSearch type. I would like to exclude the UnivId property from being indexed:

[Elastic         


        
相关标签:
3条回答
  • 2020-12-31 03:02

    In NEST 2.0 ElasticPropertyAttribute is replaced by per type attributes (StringAttribute, DateAttribute...). I used Ignore parameter to exclude property.

    Exemple for string :

    [String(Ignore = true)]
    public string Id {get;set;}
    
    0 讨论(0)
  • 2020-12-31 03:11

    You should be able to set the OptOut value of the ElasticProperty attribute, like the following:

     [ElasticProperty(OptOut = true)]
     public string UnivId { get; set; }
    
    0 讨论(0)
  • 2020-12-31 03:20

    If using Nest 5.0+, per the docs several ways to ignore a field:


    Ignore attribute should work:

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

    JsonIgnore can also be used since Newtonsoft.Json is the default serializer used by Nest.


    Another way is to use type specific attribute mappings associated with the property. For example, since it's a string then use Text attribute:

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

    or if an int use Number:

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

    In addition, instead of using an explicit attribute on the property, the mapping can be ignored using .DefaultMappingFor<... on ConnectionSettings (see docs for more detail)

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

    However, if want to conditionally ignore an attribute if the value is null then use the following Newtonsoft.Json attribute with null handling setting:

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

    I found the above useful when doing partial updates on a doc but wanted to re-use the same C# class for indexing and avoid overwriting existing values in the index.

    0 讨论(0)
提交回复
热议问题