WARNING: Version 5.x has passed its EOL date.
This documentation is no longer being maintained and may be removed. If you are running this version, we strongly advise you to upgrade. For the latest information, see the current release documentation.
Ignoring properties
editIgnoring properties
editProperties on a POCO can be ignored for mapping purposes in a few ways:
-
Using the
Ignore
property on a derivedElasticsearchPropertyAttribute
type applied to the property that should be ignored on the POCO -
Using the
.InferMappingFor<TDocument>(Func<ClrTypeMappingDescriptor<TDocument>, IClrTypeMapping<TDocument>> selector)
onConnectionSettings
-
Using an ignore attribute applied to the POCO property that is understood by
the
IElasticsearchSerializer
used, and inspected inside of theCreatePropertyMapping()
on the serializer. In the case of the defaultJsonNetSerializer
, this is the Json.NETJsonIgnoreAttribute
This example demonstrates all ways, using the Ignore
property on the attribute to ignore the property
PropertyToIgnore
, the infer mapping to ignore the property AnotherPropertyToIgnore
and the
json serializer specific attribute to ignore the property JsonIgnoredProperty
[ElasticsearchType(Name = "company")] public class CompanyWithAttributesAndPropertiesToIgnore { public string Name { get; set; } [String(Ignore = true)] public string PropertyToIgnore { get; set; } public string AnotherPropertyToIgnore { get; set; } [JsonIgnore] public string JsonIgnoredProperty { get; set; } }
All of the properties except Name
have been ignored in the mapping
var descriptor = new CreateIndexDescriptor("myindex") .Mappings(ms => ms .Map<CompanyWithAttributesAndPropertiesToIgnore>(m => m .AutoMap() ) ); var settings = WithConnectionSettings(s => s .InferMappingFor<CompanyWithAttributesAndPropertiesToIgnore>(i => i .Ignore(p => p.AnotherPropertyToIgnore) ) );
The JSON output for the mapping does not contain the ignored properties
{ "mappings": { "company": { "properties": { "name": { "type": "string" } } } } }
Ignoring inherited properties
editBy using the infer mapping configuration for a POCO on the ConnectionSettings
, it is possible to
ignore inherited properties too.
public class Parent { public int Id { get; set; } public string Description { get; set; } public string IgnoreMe { get; set; } } public class Child : Parent { } var descriptor = new CreateIndexDescriptor("myindex") .Mappings(ms => ms .Map<Child>(m => m .AutoMap() ) ); var settings = WithConnectionSettings(s => s .InferMappingFor<Child>(m => m .Rename(p => p.Description, "desc") .Ignore(p => p.IgnoreMe) ) );
The property IgnoreMe
has been ignored for the child mapping
{ "mappings": { "child": { "properties": { "id": { "type": "integer" }, "desc": { "type": "string" } } } } }