- .NET Clients: other versions:
- Introduction
- Installation
- 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
- Long Range Query Usage
- Numeric Range Query Usage
- Term Range Query Usage
- Regexp Query Usage
- Term Query Usage
- Terms Set Query Usage
- Terms List Query Usage
- Terms Lookup Query Usage
- Terms Query Usage
- Wildcard Query Usage
- Compound queries
- Joining queries
- Geo queries
- Specialized queries
- Span queries
- NEST specific queries
- Aggregations
- Metric Aggregations
- Average Aggregation Usage
- Boxplot Aggregation Usage
- Cardinality Aggregation Usage
- Extended Stats Aggregation Usage
- Geo Bounds Aggregation Usage
- Geo Centroid Aggregation Usage
- Geo Line Aggregation Usage
- Max Aggregation Usage
- Median Absolute Deviation Aggregation Usage
- Min Aggregation Usage
- Percentile Ranks Aggregation Usage
- Percentiles Aggregation Usage
- Rate Aggregation Usage
- Scripted Metric Aggregation Usage
- Stats Aggregation Usage
- String Stats Aggregation Usage
- Sum Aggregation Usage
- T Test Aggregation Usage
- Top Hits Aggregation Usage
- Top Metrics Aggregation Usage
- Value Count Aggregation Usage
- Weighted Average Aggregation Usage
- Bucket Aggregations
- Adjacency Matrix Usage
- Auto Date Histogram Aggregation Usage
- Children Aggregation Usage
- Composite Aggregation Usage
- Date Histogram Aggregation Usage
- Date Range Aggregation Usage
- Diversified Sampler Aggregation Usage
- Filter Aggregation Usage
- Filters Aggregation Usage
- Geo Distance Aggregation Usage
- Geo Hash Grid Aggregation Usage
- Geo Tile Grid Aggregation Usage
- Global Aggregation Usage
- Histogram Aggregation Usage
- Ip Range Aggregation Usage
- Missing Aggregation Usage
- Multi Terms Aggregation Usage
- Nested Aggregation Usage
- Parent Aggregation Usage
- Range Aggregation Usage
- Rare Terms Aggregation Usage
- Reverse Nested Aggregation Usage
- Sampler Aggregation Usage
- Significant Terms Aggregation Usage
- Significant Text Aggregation Usage
- Terms Aggregation Usage
- Variable Width Histogram Usage
- Pipeline Aggregations
- Average Bucket Aggregation Usage
- Bucket Script Aggregation Usage
- Bucket Selector Aggregation Usage
- Bucket Sort Aggregation Usage
- Cumulative Cardinality 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
- Moving Function Aggregation Usage
- Moving Percentiles Aggregation Usage
- Normalize Aggregation Usage
- Percentiles Bucket Aggregation Usage
- Serial Differencing Aggregation Usage
- Stats Bucket Aggregation Usage
- Sum Bucket Aggregation Usage
- Matrix Aggregations
- Metric Aggregations
Auto mapping
editAuto mapping
editWhen creating a mapping either when creating an index or through the Put Mapping API, NEST offers a feature called auto mapping that can automagically infer the correct Elasticsearch field datatypes from the CLR POCO property types you are mapping.
We’ll look at the features of auto mapping with a number of examples. For this,
we’ll define two POCOs, Company
, which has a name
and a collection of Employees, and Employee
which has various properties of
different types, and itself has a collection of Employee
types.
public abstract class Document { public JoinField Join { get; set; } } public class Company : Document { public string Name { get; set; } public List<Employee> Employees { get; set; } } public class Employee : Document { public string LastName { get; set; } public int Salary { get; set; } public DateTime Birthday { get; set; } public bool IsManager { get; set; } public List<Employee> Employees { get; set; } public TimeSpan Hours { get; set; } }
Auto mapping can take the pain out of having to define a manual mapping for all properties on the POCO. In this case we want to index two subclasses into a single index. We call Map for the base class and then call AutoMap foreach of the types we want it to implement
var createIndexResponse = _client.Indices.Create("myindex", c => c .Map<Document>(m => m .AutoMap<Company>() .AutoMap(typeof(Employee)) ) );
This produces the following JSON request
{ "mappings": { "properties": { "birthday": { "type": "date" }, "employees": { "properties": { "birthday": { "type": "date" }, "employees": { "properties": {}, "type": "object" }, "hours": { "type": "long" }, "isManager": { "type": "boolean" }, "join": { "properties": {}, "type": "object" }, "lastName": { "fields": { "keyword": { "ignore_above": 256, "type": "keyword" } }, "type": "text" }, "salary": { "type": "integer" } }, "type": "object" }, "hours": { "type": "long" }, "isManager": { "type": "boolean" }, "join": { "properties": {}, "type": "object" }, "lastName": { "fields": { "keyword": { "ignore_above": 256, "type": "keyword" } }, "type": "text" }, "name": { "fields": { "keyword": { "ignore_above": 256, "type": "keyword" } }, "type": "text" }, "salary": { "type": "integer" } } } }
Observe that NEST has inferred the Elasticsearch types based on the CLR type of our POCO properties. In this example,
-
Birthday is mapped as a
date
, -
Hours is mapped as a
long
(TimeSpan
ticks) -
IsManager is mapped as a
boolean
, -
Salary is mapped as an
integer
-
Employees is mapped as an
object
and the remaining string properties as multi field text
datatypes, each with a keyword
datatype
sub field.
Inferred .NET type mapping
editNEST has inferred mapping support for the following .NET types
|
maps to |
|
maps to |
|
maps to |
|
maps to |
|
maps to |
|
maps to |
|
maps to |
|
maps to |
|
maps to |
|
maps to |
|
maps to |
|
maps to |
|
maps to |
|
maps to |
|
maps to |
|
maps to |
|
maps to |
and supports a number of special types defined in NEST
|
maps to |
|
maps to |
|
maps to |
|
maps to |
|
maps to |
|
maps to |
|
maps to |
|
maps to |
|
maps to |
|
maps to |
All other types map to "object"
by default.
Some .NET types do not have direct equivalent Elasticsearch types. For example, System.Decimal
is a type
commonly used to express currencies and other financial calculations that require large numbers of significant
integral and fractional digits and no round-off errors. There is no equivalent type in Elasticsearch, and the
nearest type is double, a double-precision 64-bit IEEE 754 floating point.
When a POCO has a System.Decimal
property, it is automapped to the Elasticsearch double
type. With the caveat
of a potential loss of precision, this is generally acceptable for a lot of use cases, but it can however cause
problems in some edge cases.
As the C# Specification states,
For a conversion from decimal to float or double , the decimal value is rounded to the nearest double or float value.
While this conversion may lose precision, it never causes an exception to be thrown.
|
||
-- C# Specification section 6.2.1 |
This conversion does cause an exception to be thrown at deserialization time for Decimal.MinValue
and Decimal.MaxValue
because, at
serialization time, the nearest double
value that is converted to is outside of the bounds of Decimal.MinValue
or Decimal.MaxValue
,
respectively. In these cases, it is advisable to use double
as the POCO property type.
Mapping Recursion
editIf you notice in our previous Company
and Employee
example, the Employee
type is recursive
in that the Employee
class itself contains a collection of type Employee
. By default, .AutoMap()
will only
traverse a single depth when it encounters recursive instances like this; the collection of type Employee
on the Employee
class did not get any of its properties mapped.
This is done as a safe-guard to prevent stack overflows and all the fun that comes with
infinite recursion. Additionally, in most cases, when it comes to Elasticsearch mappings, it is
often an edge case to have deeply nested mappings like this. However, you may still have
the need to do this, so you can control the recursion depth of .AutoMap()
.
Let’s introduce a very simple class, A
, which itself has a property
Child of type A
.
public class A { public A Child { get; set; } }
By default, .AutoMap()
only goes as far as depth 1
var createIndexResponse = _client.Indices.Create("myindex", c => c .Map<A>(m => m.AutoMap()) );
Thus we do not map properties on the second occurrence of our Child property
{ "mappings": { "properties": { "child": { "properties": {}, "type": "object" } } } }
Now let’s specify a maxRecursion of 3
createIndexResponse = _client.Indices.Create("myindex", c => c .Map<A>(m => m.AutoMap(3)) );
.AutoMap()
has now mapped three levels of our Child property
{ "mappings": { "properties": { "child": { "type": "object", "properties": { "child": { "type": "object", "properties": { "child": { "type": "object", "properties": { "child": { "type": "object", "properties": {} } } } } } } } } } }
On this page