Attribute mappingedit

In 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 an Employee type and use attributes to define the mappings.

[ElasticsearchType(Name = "employee")]
public class Employee
    [Text(Name = "first_name", Norms = false, Similarity = "LMDirichlet")]
    public string FirstName { get; set; }

    [Text(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; }

    public List<Employee> Employees { get; set; }

     [Text(Name = "office_hours")]
     public TimeSpan? OfficeHours { get; set; }

     public List<Skill> Skills { get; set; }

public class Skill
    public string Name { get; set; }

    [Number(NumberType.Byte, Name = "level")]
    public int Proficiency { get; set; }

Then we map the types by calling .AutoMap()

var createIndexResponse = client.CreateIndex("myindex", c => c
    .Mappings(ms => ms
        .Map<Employee>(m => m.AutoMap())
  "mappings": {
    "employee": {
      "properties": {
        "birthday": {
          "format": "MMddyyyy",
          "type": "date"
        "empl": {
          "properties": {},
          "type": "nested"
        "first_name": {
          "type": "text",
          "norms": false,
          "similarity": "LMDirichlet"
        "isManager": {
          "null_value": false,
          "store": true,
          "type": "boolean"
        "last_name": {
          "type": "text"
        "office_hours": {
          "type": "text"
        "salary": {
          "coerce": true,
          "doc_values": false,
          "ignore_malformed": true,
          "type": "float"
        "skills": {
          "properties": {
            "level": {
              "type": "byte"
            "name": {
              "type": "text"
          "type": "object"

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.