diff --git a/src/Nest/Enums/FieldType.cs b/src/Nest/Enums/FieldType.cs index 9c5bb8c835a..b5ff37886b1 100644 --- a/src/Nest/Enums/FieldType.cs +++ b/src/Nest/Enums/FieldType.cs @@ -59,6 +59,16 @@ public enum FieldType [EnumMember(Value = "long")] Long, /// + /// Short type. + /// + [EnumMember(Value = "short")] + Short, + /// + /// Byte type. + /// + [EnumMember(Value = "byte")] + Byte, + /// /// Float type. /// [EnumMember(Value = "float")] diff --git a/src/Nest/Resolvers/Writers/TypeMappingWriter.cs b/src/Nest/Resolvers/Writers/TypeMappingWriter.cs index 06cec1dae65..98631534e3d 100644 --- a/src/Nest/Resolvers/Writers/TypeMappingWriter.cs +++ b/src/Nest/Resolvers/Writers/TypeMappingWriter.cs @@ -85,6 +85,7 @@ internal RootObjectMapping RootObjectMappingFromAttributes() using (var ms = new MemoryStream(nestedJson.Utf8Bytes())) return this._elasticSerializer.Deserialize(ms); } + internal ObjectMapping ObjectMappingFromAttributes() { var json = JObject.Parse(this.MapFromAttributes()); @@ -93,6 +94,7 @@ internal ObjectMapping ObjectMappingFromAttributes() using (var ms = new MemoryStream(nestedJson.Utf8Bytes())) return this._elasticSerializer.Deserialize(ms); } + internal NestedObjectMapping NestedObjectMappingFromAttributes() { var json = JObject.Parse(this.MapFromAttributes()); @@ -101,6 +103,7 @@ internal NestedObjectMapping NestedObjectMappingFromAttributes() using (var ms = new MemoryStream(nestedJson.Utf8Bytes())) return this._elasticSerializer.Deserialize(ms); } + public string MapFromAttributes() { var sb = new StringBuilder(); @@ -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; @@ -182,38 +185,37 @@ private void WritePropertiesFromAttribute(JsonWriter jsonWriter, IElasticPropert } /// - /// Get the Elastic Search Field Type Related. + /// Gets the Elasticsearch type name for a given ElasticPropertyAttribute. /// - /// ElasticPropertyAttribute - /// Property Field - /// String with the type name or null if can not be inferres - private string GetElasticSearchType(IElasticPropertyAttribute att, PropertyInfo p) + /// ElasticPropertyAttribute + /// Property field + /// String containing the type name, or null if can not be inferred. + 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); } /// - /// Get the Elastic Search Field from a FieldType. + /// Gets the Elasticsearch type name for a given FieldType. /// /// FieldType - /// String with the type name or null if can not be inferres - private string GetElasticSearchTypeFromFieldType(FieldType? fieldType) + /// String containing the type name, or null if can not be inferred. + private string GetElasticsearchType(FieldType? fieldType) { switch (fieldType) { @@ -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: @@ -255,11 +261,11 @@ private string GetElasticSearchTypeFromFieldType(FieldType? fieldType) } /// - /// Inferes the FieldType from the type of the property. + /// Gets the FieldType for a given CLR type. /// - /// Type of the property - /// FieldType or null if can not be inferred - private FieldType? GetFieldTypeFromType(Type propertyType) + /// CLR type of the property + /// The FieldType, or null if can not be inferred. + private FieldType? GetFieldType(Type propertyType) { propertyType = GetUnderlyingType(propertyType); @@ -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": + return FieldType.Byte; case "Int64": + case "UInt64": return FieldType.Long; case "Single": return FieldType.Float; diff --git a/src/Tests/Nest.Tests.Unit/Core/AttributeBasedMap/AllTypesTest.cs b/src/Tests/Nest.Tests.Unit/Core/AttributeBasedMap/AllTypesTest.cs new file mode 100644 index 00000000000..20d7830e7ee --- /dev/null +++ b/src/Tests/Nest.Tests.Unit/Core/AttributeBasedMap/AllTypesTest.cs @@ -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(); + this.JsonEquals(json, System.Reflection.MethodInfo.GetCurrentMethod()); + } + } +} diff --git a/src/Tests/Nest.Tests.Unit/Core/AttributeBasedMap/TestAllTypes.json b/src/Tests/Nest.Tests.Unit/Core/AttributeBasedMap/TestAllTypes.json new file mode 100644 index 00000000000..eacb07dc99d --- /dev/null +++ b/src/Tests/Nest.Tests.Unit/Core/AttributeBasedMap/TestAllTypes.json @@ -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" + } + } + } +} \ No newline at end of file diff --git a/src/Tests/Nest.Tests.Unit/Nest.Tests.Unit.csproj b/src/Tests/Nest.Tests.Unit/Nest.Tests.Unit.csproj index 3a61368b706..e2073bc986f 100644 --- a/src/Tests/Nest.Tests.Unit/Nest.Tests.Unit.csproj +++ b/src/Tests/Nest.Tests.Unit/Nest.Tests.Unit.csproj @@ -100,6 +100,7 @@ + @@ -120,6 +121,9 @@ Always + + Always + Always