Skip to content

Non generic automap #3076

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public class AllowAllCertificatesCluster : SslAndKpiXPackCluster
If your client application has access to the public CA certificate locally, Elasticsearch.NET and NEST ship with some handy helpers
that can assert that a certificate the server presents is one that came from the local CA.

If you use X-Pack's {ref_current}/certutil.html[+certutil+ tool] to generate SSL certificates, the generated node certificate
If you use X-Pack's {ref_current}/certutil.html`certutil` tool] to generate SSL certificates, the generated node certificate
does not include the CA in the certificate chain, in order to cut down on SSL handshake size. In those case you can use`CertificateValidations.AuthorityIsRoot` and pass it your local copy of the CA public key to assert that
the certificate the server presented was generated using it

Expand Down Expand Up @@ -115,7 +115,7 @@ the local CA certificate is part of the chain that was used to generate the serv
==== Client Certificates

X-Pack also allows you to configure a {xpack_current}/pki-realm.html[PKI realm] to enable user authentication
through client certificates. The {ref_current}/certutil.html[+certutil+ tool] included with X-Pack allows you to
through client certificates. The {ref_current}/certutil.html`certutil` tool] included with X-Pack allows you to
generate client certificates as well and assign the distinguished name (DN) of the
certificate to a user with a certain role.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public class Employee
public bool IsManager { get; set; }

[Nested]
[PropertyName("empl"), JsonProperty("empl")]
[PropertyName("empl")]
public List<Employee> Employees { get; set; }
}
----
Expand All @@ -74,11 +74,12 @@ Then we map the types by calling `.AutoMap()`

[source,csharp]
----
var descriptor = new CreateIndexDescriptor("myindex")
var createIndexResponse = client.CreateIndex("myindex", c => c
.Mappings(ms => ms
.Map<Company>(m => m.AutoMap())
.Map<Employee>(m => m.AutoMap())
);
)
);
----

[source,javascript]
Expand Down
189 changes: 121 additions & 68 deletions docs/client-concepts/high-level/mapping/auto-map.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,20 @@ for the base class and then call AutoMap foreach of the types we want it it the

[source,csharp]
----
var descriptor = new CreateIndexDescriptor("myindex")
var createIndexResponse = client.CreateIndex("myindex", c => c
.Mappings(ms => ms
.Map<Document>(m => m
.AutoMap<Company>() <1>
.AutoMap<Employee>() <2>
.AutoMap(typeof(Employee)) <2>
)
);
)
);
----
<1> Auto map `Company`
<1> Auto map `Company` using the generic method

<2> Auto map `Employee` using the non-generic method

<2> Auto map `Employee`
This produces the following JSON request

[source,javascript]
----
Expand Down Expand Up @@ -153,47 +156,47 @@ public class ParentWithStringId : IgnoringProperties.Parent
public new string Id { get; set; }
}

var descriptor = new CreateIndexDescriptor("myindex")
.Mappings(ms => ms
.Map<ParentWithStringId>(m => m
.AutoMap()
)
);

var expected = new
{
mappings = new
{
parent = new
{
properties = new
{
id = new
{
type = "text",
fields = new
{
keyword = new
{
ignore_above = 256,
type = "keyword"
}
}
}
}
}
}
};

var settings = WithConnectionSettings(s => s
var connectionSettings = new ConnectionSettings(new InMemoryConnection()) <1>
.DisableDirectStreaming() <2>
.DefaultMappingFor<ParentWithStringId>(m => m
.TypeName("parent")
.Ignore(p => p.Description)
.Ignore(p => p.IgnoreMe)
);

var client = new ElasticClient(connectionSettings);

var createIndexResponse = client.CreateIndex("myindex", c => c
.Mappings(ms => ms
.Map<ParentWithStringId>(m => m
.AutoMap()
)
)
);
----
<1> we're using an _in memory_ connection for this example. In your production application though, you'll want to use an `IConnection` that actually sends a request.

<2> we disable direct streaming here to capture the request and response bytes. In your production application however, you'll likely not want to do this, since it causes the request and response bytes to be buffered in memory.

settings.Expect(expected).WhenSerializing((ICreateIndexRequest) descriptor);
[source,javascript]
----
{
"mappings": {
"parent": {
"properties": {
"id": {
"type": "text",
"fields": {
"keyword": {
"ignore_above": 256,
"type": "keyword"
}
}
}
}
}
}
}
----

Observe that NEST has inferred the Elasticsearch types based on the CLR type of our POCO properties.
Expand All @@ -214,57 +217,105 @@ sub field.

NEST has inferred mapping support for the following .NET types

* `String` maps to `"text"` with a `"keyword"` sub field. See <<multi-fields, Multi Fields>>.
[horizontal]
`String`::

maps to `"text"` with a `"keyword"` sub field. See <<multi-fields, Multi Fields>>.

`Int32`::

maps to `"integer"`

`UInt16`::

maps to `"integer"`

`Int16`::

maps to `"short"`

`Byte`::

maps to `"short"`

`Int64`::

* `Int32` maps to `"integer"`
maps to `"long"`

* `UInt16` maps to `"integer"`
`UInt32`::

* `Int16` maps to `"short"`
maps to `"long"`

* `Byte` maps to `"short"`
`TimeSpan`::

* `Int64` maps to `"long"`
maps to `"long"`

* `UInt32` maps to `"long"`
`Single`::

* `TimeSpan` maps to `"long"`
maps to `"float"`

* `Single` maps to `"float"`
`Double`::

* `Double` maps to `"double"`
maps to `"double"`

* `Decimal` maps to `"double"`
`Decimal`::

* `UInt64` maps to `"double"`
maps to `"double"`

* `DateTime` maps to `"date"`
`UInt64`::

* `DateTimeOffset` maps to `"date"`
maps to `"double"`

* `Boolean` maps to `"boolean"`
`DateTime`::

* `Char` maps to `"keyword"`
maps to `"date"`

* `Guid` maps to `"keyword"`
`DateTimeOffset`::

maps to `"date"`

`Boolean`::

maps to `"boolean"`

`Char`::

maps to `"keyword"`

`Guid`::

maps to `"keyword"`

and supports a number of special types defined in NEST

* `Nest.GeoLocation` maps to `"geo_point"`
[horizontal]
`Nest.GeoLocation`::

maps to `"geo_point"`

* `Nest.CompletionField` maps to `"completion"`
`Nest.CompletionField`::

* `Nest.Attachment` maps to `"attachment"`
maps to `"completion"`

* `Nest.DateRange` maps to `"date_range"`
`Nest.DateRange`::

* `Nest.DoubleRange` maps to `"double_range"`
maps to `"date_range"`

* `Nest.FloatRange` maps to `"float_range"`
`Nest.DoubleRange`::

* `Nest.IntegerRange` maps to `"integer_range"`
maps to `"double_range"`

* `Nest.LongRange` maps to `"long_range"`
`Nest.FloatRange`::

maps to `"float_range"`

`Nest.IntegerRange`::

maps to `"integer_range"`

`Nest.LongRange`::

maps to `"long_range"`

All other types map to `"object"` by default.

Expand Down Expand Up @@ -319,10 +370,11 @@ By default, `.AutoMap()` only goes as far as depth 1

[source,csharp]
----
var descriptor = new CreateIndexDescriptor("myindex")
var createIndexResponse = client.CreateIndex("myindex", c => c
.Mappings(ms => ms
.Map<A>(m => m.AutoMap())
);
)
);
----

Thus we do not map properties on the second occurrence of our Child property
Expand All @@ -347,10 +399,11 @@ Now let's specify a maxRecursion of `3`

[source,csharp]
----
var withMaxRecursionDescriptor = new CreateIndexDescriptor("myindex")
createIndexResponse = client.CreateIndex("myindex", c => c
.Mappings(ms => ms
.Map<A>(m => m.AutoMap(3))
);
)
);
----

`.AutoMap()` has now mapped three levels of our Child property
Expand Down
Loading