- .NET Clients: other versions:
- Introduction
- Breaking changes
- API Conventions
- Elasticsearch.Net - Low level client
- NEST - High level client
- Troubleshooting
- Search
- Query DSL
- Full text queries
- Term level queries
- Exists Query Usage
- Fuzzy Date Query Usage
- Fuzzy Numeric Query Usage
- Fuzzy Query Usage
- Ids Query Usage
- Prefix Query Usage
- Date Range Query Usage
- Numeric Range Query Usage
- Term Range Query Usage
- Regexp Query Usage
- Term Query Usage
- Terms List Query Usage
- Terms Lookup Query Usage
- Terms Query Usage
- Type Query Usage
- Wildcard Query Usage
- Compound queries
- Joining queries
- Geo queries
- Geo Bounding Box Query Usage
- Geo Distance Query Usage
- Geo Distance Range Query Usage
- Geo Hash Cell Query Usage
- Geo Polygon Query Usage
- Geo Shape Circle Query Usage
- Geo Shape Envelope Query Usage
- Geo Shape Geometry Collection Query Usage
- Geo Shape Indexed Shape Query Usage
- Geo Shape Line String Query Usage
- Geo Shape Multi Line String Query Usage
- Geo Shape Multi Point Query Usage
- Geo Shape Multi Polygon Query Usage
- Geo Shape Point Query Usage
- Geo Shape Polygon Query Usage
- Specialized queries
- Span queries
- NEST specific queries
- Aggregations
- Metric Aggregations
- Average Aggregation Usage
- Cardinality Aggregation Usage
- Extended Stats Aggregation Usage
- Geo Bounds Aggregation Usage
- Geo Centroid Aggregation Usage
- Max Aggregation Usage
- Min Aggregation Usage
- Percentile Ranks Aggregation Usage
- Percentiles Aggregation Usage
- Scripted Metric Aggregation Usage
- Stats Aggregation Usage
- Sum Aggregation Usage
- Top Hits Aggregation Usage
- Value Count Aggregation Usage
- Bucket Aggregations
- Adjacency Matrix Usage
- Children Aggregation Usage
- Date Histogram Aggregation Usage
- Date Range Aggregation Usage
- Filter Aggregation Usage
- Filters Aggregation Usage
- Geo Distance Aggregation Usage
- Geo Hash Grid Aggregation Usage
- Global Aggregation Usage
- Histogram Aggregation Usage
- Ip Range Aggregation Usage
- Missing Aggregation Usage
- Nested Aggregation Usage
- Range Aggregation Usage
- Reverse Nested Aggregation Usage
- Sampler Aggregation Usage
- Significant Terms Aggregation Usage
- Terms Aggregation Usage
- Pipeline Aggregations
- Average Bucket Aggregation Usage
- Bucket Script Aggregation Usage
- Bucket Selector Aggregation Usage
- Cumulative Sum Aggregation Usage
- Derivative Aggregation Usage
- Extended Stats Bucket Aggregation Usage
- Max Bucket Aggregation Usage
- Min Bucket Aggregation Usage
- Moving Average Ewma Aggregation Usage
- Moving Average Holt Linear Aggregation Usage
- Moving Average Holt Winters Aggregation Usage
- Moving Average Linear Aggregation Usage
- Moving Average Simple Aggregation Usage
- Percentiles Bucket Aggregation Usage
- Serial Differencing Aggregation Usage
- Stats Bucket Aggregation Usage
- Sum Bucket Aggregation Usage
- Matrix Aggregations
- Metric Aggregations
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 { [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.