18

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

[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; }
}
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
vish.Net
  • 962
  • 2
  • 10
  • 21

3 Answers3

19

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

 [ElasticProperty(OptOut = true)]
 public string UnivId { get; set; }
gsk
  • 1,233
  • 15
  • 32
Paige Cook
  • 22,415
  • 3
  • 57
  • 68
  • 7
    OMG, what a poor property name, took me a while to figure it out too.. Thanks – Funbit Aug 18 '14 at 07:22
  • what is the difference between oput and [ElasticProperty(Index = FieldIndexOption.No)]? do they do the same job? – Emil Jan 12 '16 at 15:50
  • 1
    just for the people ever end up here. The difference between these 2 explained on this url. http://stackoverflow.com/questions/34748258/index-fieldindexoption-no-vs-optout-true – Emil Jan 12 '16 at 22:44
  • 1
    Anyone who knows how to do this in Nest 2? – beruic Mar 03 '16 at 11:41
  • @beruic, http://stackoverflow.com/a/35992605/443310 Alexandre B just gave some light on it – I.G. Pascual Mar 15 '16 at 11:14
17

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;}
Alexandre B
  • 361
  • 3
  • 6
  • Thanks man! Btw, do you know how to ignore nested properties in code base mapping (in NEST 2.0)? – I.G. Pascual Mar 15 '16 at 11:10
  • 1
    @I.G.Pascual take a look at the automap docs for ways to ignore properties - https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/auto-map.html#ignoring-properties – Russ Cam May 10 '16 at 03:58
  • 1
    @RussCam when did they just update those docs?? That-is-AWESOME! – I.G. Pascual May 10 '16 at 08:04
3

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.

Ray
  • 187,153
  • 97
  • 222
  • 204