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.
Attribute mapping
editAttribute mapping
editIn Auto mapping, you saw that the type mapping for a POCO can be inferred from the
properties of the POCO, using .AutoMap()
. But what do you do when you want to map differently
to the inferred mapping? This is where attribute mapping can help.
It is possible to define your mappings using attributes on your POCO type and properties. With
attributes on properties and calling .AutoMap()
, NEST will infer the mappings from the POCO property
types and take into account the mapping attributes.
When you use attributes, you must also call .AutoMap()
for the attributes to be applied.
Here we define the same two types as before, but this time using attributes to define the mappings.
[ElasticsearchType(Name = "company")] public class Company { [String(NullValue = "null", Similarity = SimilarityOption.BM25, Index = FieldIndexOption.NotAnalyzed)] public string Name { get; set; } [String(Name = "office_hours")] public TimeSpan? HeadOfficeHours { get; set; } [Object(Store = false)] public List<Employee> Employees { get; set; } } [ElasticsearchType(Name = "employee")] public class Employee { [String(Name = "first_name")] public string FirstName { get; set; } [String(Name = "last_name")] public string LastName { get; set; } [Number(DocValues = false, IgnoreMalformed = true, Coerce = true)] public int Salary { get; set; } [Date(Format = "MMddyyyy")] public DateTime Birthday { get; set; } [Boolean(NullValue = false, Store = true)] public bool IsManager { get; set; } [Nested] [JsonProperty("empl")] public List<Employee> Employees { get; set; } }
Then we map the types by calling .AutoMap()
var descriptor = new CreateIndexDescriptor("myindex") .Mappings(ms => ms .Map<Company>(m => m.AutoMap()) .Map<Employee>(m => m.AutoMap()) );
{ "mappings": { "company": { "properties": { "employees": { "properties": { "birthday": { "format": "MMddyyyy", "type": "date" }, "empl": { "properties": {}, "type": "nested" }, "first_name": { "type": "string" }, "isManager": { "null_value": false, "store": true, "type": "boolean" }, "last_name": { "type": "string" }, "salary": { "coerce": true, "doc_values": false, "ignore_malformed": true, "type": "double" } }, "type": "object", "store": false }, "name": { "null_value": "null", "similarity": "BM25", "type": "string", "index": "not_analyzed" }, "office_hours": { "type": "string" } } }, "employee": { "properties": { "birthday": { "format": "MMddyyyy", "type": "date" }, "empl": { "properties": {}, "type": "nested" }, "first_name": { "type": "string" }, "isManager": { "null_value": false, "store": true, "type": "boolean" }, "last_name": { "type": "string" }, "salary": { "coerce": true, "doc_values": false, "ignore_malformed": true, "type": "double" } } } } }
Attribute mapping can be a convenient way to control how POCOs are mapped with minimal code, however there are some mapping features that cannot be expressed with attributes, for example, Multi fields. In order to have the full power of mapping in NEST at your disposal, take a look at Fluent Mapping next.