Skip to content
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
10 changes: 10 additions & 0 deletions src/Nest/Enums/FieldType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ public enum FieldType
[EnumMember(Value = "long")]
Long,
/// <summary>
/// Short type.
/// </summary>
[EnumMember(Value = "short")]
Short,
/// <summary>
/// Byte type.
/// </summary>
[EnumMember(Value = "byte")]
Byte,
/// <summary>
/// Float type.
/// </summary>
[EnumMember(Value = "float")]
Expand Down
56 changes: 35 additions & 21 deletions src/Nest/Resolvers/Writers/TypeMappingWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ internal RootObjectMapping RootObjectMappingFromAttributes()
using (var ms = new MemoryStream(nestedJson.Utf8Bytes()))
return this._elasticSerializer.Deserialize<RootObjectMapping>(ms);
}

internal ObjectMapping ObjectMappingFromAttributes()
{
var json = JObject.Parse(this.MapFromAttributes());
Expand All @@ -93,6 +94,7 @@ internal ObjectMapping ObjectMappingFromAttributes()
using (var ms = new MemoryStream(nestedJson.Utf8Bytes()))
return this._elasticSerializer.Deserialize<ObjectMapping>(ms);
}

internal NestedObjectMapping NestedObjectMappingFromAttributes()
{
var json = JObject.Parse(this.MapFromAttributes());
Expand All @@ -101,6 +103,7 @@ internal NestedObjectMapping NestedObjectMappingFromAttributes()
using (var ms = new MemoryStream(nestedJson.Utf8Bytes()))
return this._elasticSerializer.Deserialize<NestedObjectMapping>(ms);
}

public string MapFromAttributes()
{
var sb = new StringBuilder();
Expand Down Expand Up @@ -139,7 +142,7 @@ internal void WriteProperties(JsonWriter jsonWriter)
continue;

var propertyName = this.Infer.PropertyName(p);
var type = GetElasticSearchType(att, p);
var type = GetElasticsearchTypeName(att, p);

if (type == null) //could not get type from attribute or infer from CLR type.
continue;
Expand Down Expand Up @@ -182,38 +185,37 @@ private void WritePropertiesFromAttribute(JsonWriter jsonWriter, IElasticPropert
}

/// <summary>
/// Get the Elastic Search Field Type Related.
/// Gets the Elasticsearch type name for a given ElasticPropertyAttribute.
/// </summary>
/// <param name="att">ElasticPropertyAttribute</param>
/// <param name="p">Property Field</param>
/// <returns>String with the type name or null if can not be inferres</returns>
private string GetElasticSearchType(IElasticPropertyAttribute att, PropertyInfo p)
/// <param name="attribute">ElasticPropertyAttribute</param>
/// <param name="propertyInfo">Property field</param>
/// <returns>String containing the type name, or null if can not be inferred.</returns>
private string GetElasticsearchTypeName(IElasticPropertyAttribute attribute, PropertyInfo propertyInfo)
{
FieldType? fieldType = null;
if (att != null)
{
fieldType = att.Type;
}

if (attribute != null)
fieldType = attribute.Type;

if (fieldType == null || fieldType == FieldType.None)
{
fieldType = this.GetFieldTypeFromType(p.PropertyType);
if (fieldType == null && att != null)
fieldType = this.GetFieldType(propertyInfo.PropertyType);
if (fieldType == null && attribute != null)
{
var message = _noFieldTypeMessage.F(p.Name, this._type.Name);
var message = _noFieldTypeMessage.F(propertyInfo.Name, this._type.Name);
throw new DslException(message);
}
}

return this.GetElasticSearchTypeFromFieldType(fieldType);
return this.GetElasticsearchType(fieldType);
}

/// <summary>
/// Get the Elastic Search Field from a FieldType.
/// Gets the Elasticsearch type name for a given FieldType.
/// </summary>
/// <param name="fieldType">FieldType</param>
/// <returns>String with the type name or null if can not be inferres</returns>
private string GetElasticSearchTypeFromFieldType(FieldType? fieldType)
/// <returns>String containing the type name, or null if can not be inferred.</returns>
private string GetElasticsearchType(FieldType? fieldType)
{
switch (fieldType)
{
Expand All @@ -231,6 +233,10 @@ private string GetElasticSearchTypeFromFieldType(FieldType? fieldType)
return "string";
case FieldType.Integer:
return "integer";
case FieldType.Short:
return "short";
case FieldType.Byte:
return "byte";
case FieldType.Long:
return "long";
case FieldType.Float:
Expand All @@ -255,11 +261,11 @@ private string GetElasticSearchTypeFromFieldType(FieldType? fieldType)
}

/// <summary>
/// Inferes the FieldType from the type of the property.
/// Gets the FieldType for a given CLR type.
/// </summary>
/// <param name="propertyType">Type of the property</param>
/// <returns>FieldType or null if can not be inferred</returns>
private FieldType? GetFieldTypeFromType(Type propertyType)
/// <param name="propertyType">CLR type of the property</param>
/// <returns>The FieldType, or null if can not be inferred.</returns>
private FieldType? GetFieldType(Type propertyType)
{
propertyType = GetUnderlyingType(propertyType);

Expand All @@ -274,8 +280,16 @@ private string GetElasticSearchTypeFromFieldType(FieldType? fieldType)
switch (propertyType.Name)
{
case "Int32":
case "UInt32":
return FieldType.Integer;
case "Int16":
case "UInt16":
return FieldType.Short;
case "Byte":
case "SByte":
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can do better then map to integer e.g CLR byte to Elasticsearch byte

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Just pushed another commit that adds support for byte and short. Also did some minor cleanup ofTypeMappingWriter`.

return FieldType.Byte;
case "Int64":
case "UInt64":
return FieldType.Long;
case "Single":
return FieldType.Float;
Expand Down
32 changes: 32 additions & 0 deletions src/Tests/Nest.Tests.Unit/Core/AttributeBasedMap/AllTypesTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;

namespace Nest.Tests.Unit.Core.AttributeBasedMap
{
[TestFixture]
public class AllTypesTest : BaseAttributeMappingTests
{
public class AllTypes
{
public int IntegerField { get; set; }
public short ShortField { get; set; }
public byte ByteField { get; set; }
public long LongField { get; set; }
public float FloatField { get; set; }
public double DoubleField { get; set; }
public DateTime DateField { get; set; }
public bool BoolField { get; set; }
}

[Test]
public void TestAllTypes()
{
var json = this.CreateMapFor<AllTypes>();
this.JsonEquals(json, System.Reflection.MethodInfo.GetCurrentMethod());
}
}
}
30 changes: 30 additions & 0 deletions src/Tests/Nest.Tests.Unit/Core/AttributeBasedMap/TestAllTypes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"alltypes": {
"properties": {
"integerField": {
"type": "integer"
},
"shortField": {
"type": "short"
},
"byteField": {
"type": "byte"
},
"longField": {
"type": "long"
},
"floatField": {
"type": "float"
},
"doubleField": {
"type": "double"
},
"dateField": {
"type": "date"
},
"boolField": {
"type": "boolean"
}
}
}
}
4 changes: 4 additions & 0 deletions src/Tests/Nest.Tests.Unit/Nest.Tests.Unit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
<Compile Include="Cluster\NodeTests.cs" />
<Compile Include="Cluster\Reroute\RerouteTests.cs" />
<Compile Include="Cluster\State\StateTests.cs" />
<Compile Include="Core\AttributeBasedMap\AllTypesTest.cs" />
<Compile Include="Core\AttributeBasedMap\IncludeInParentTests.cs" />
<Compile Include="Core\Bulk\BulkTests.cs" />
<Compile Include="Core\Bulk\BulkUrlTests.cs" />
Expand All @@ -120,6 +121,9 @@
<None Include="Cluster\Reroute\ClusterReroute.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Core\AttributeBasedMap\TestAllTypes.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Core\AttributeBasedMap\TestIncludeInParent.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down