Attribute mapping

edit

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 the same two types as before, but this time using attributes to define the mappings.

[ElasticsearchType(Name = "company")]
public class Company
{
    [Keyword(NullValue = "null", Similarity = "BM25")]
    public string Name { get; set; }

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

    [Object(Store = false)]
    public List<Employee> Employees { get; set; }
}

[ElasticsearchType(Name = "employee")]
public class Employee
{
    [Text(Name = "first_name")]
    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; }

    [Nested]
    [JsonProperty("empl")]
    public List<Employee> Employees { get; set; }
}

Then we map the types by calling .AutoMap()

var createIndexResponse = _client.CreateIndex("myindex", c => c
    .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": "text"
            },
            "isManager": {
              "null_value": false,
              "store": true,
              "type": "boolean"
            },
            "last_name": {
              "type": "text"
            },
            "salary": {
              "coerce": true,
              "doc_values": false,
              "ignore_malformed": true,
              "type": "float"
            }
          },
          "type": "object",
          "store": false
        },
        "name": {
          "null_value": "null",
          "similarity": "BM25",
          "type": "keyword"
        },
        "office_hours": {
          "type": "text"
        }
      }
    },
    "employee": {
      "properties": {
        "birthday": {
          "format": "MMddyyyy",
          "type": "date"
        },
        "empl": {
          "properties": {},
          "type": "nested"
        },
        "first_name": {
          "type": "text"
        },
        "isManager": {
          "null_value": false,
          "store": true,
          "type": "boolean"
        },
        "last_name": {
          "type": "text"
        },
        "salary": {
          "coerce": true,
          "doc_values": false,
          "ignore_malformed": true,
          "type": "float"
        }
      }
    }
  }
}

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.