/src/Nest/ElasticClient-MappingType.cs

https://github.com/wryczko/NEST · C# · 154 lines · 91 code · 12 blank · 51 comment · 3 complexity · 08d6a768eff498e69ffb15dfee64682e MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Newtonsoft.Json;
  5. using Nest.Resolvers.Writers;
  6. namespace Nest
  7. {
  8. public partial class ElasticClient
  9. {
  10. /// <summary>
  11. /// <para>Automatically map an object based on its attributes, this will also explicitly map strings to strings, datetimes to dates etc even
  12. /// if they are not marked with any attributes.</para>
  13. /// <para>
  14. /// Type name is the inferred type name for T under the default index
  15. /// </para>
  16. /// </summary>
  17. public IIndicesResponse MapFromAttributes<T>(int maxRecursion = 0) where T : class
  18. {
  19. string type = this.TypeNameResolver.GetTypeNameFor<T>();
  20. return this.MapFromAttributes<T>(this.IndexNameResolver.GetIndexForType<T>(), type, maxRecursion);
  21. }
  22. /// <summary>
  23. /// <para>Automatically map an object based on its attributes, this will also explicitly map strings to strings, datetimes to dates etc even
  24. /// if they are not marked with any attributes.</para>
  25. /// <para>
  26. /// Type name is the inferred type name for T under the specified index
  27. /// </para>
  28. /// </summary>
  29. public IIndicesResponse MapFromAttributes<T>(string index, int maxRecursion = 0) where T : class
  30. {
  31. string type = this.TypeNameResolver.GetTypeNameFor<T>();
  32. return this.MapFromAttributes<T>(index, type, maxRecursion);
  33. }
  34. /// <summary>
  35. /// <para>Automatically map an object based on its attributes, this will also explicitly map strings to strings, datetimes to dates etc even
  36. /// if they are not marked with any attributes.</para>
  37. /// <para>
  38. /// Type name is the specified type name under the specified index
  39. /// </para>
  40. /// </summary>
  41. public IIndicesResponse MapFromAttributes<T>(string index, string type, int maxRecursion = 0) where T : class
  42. {
  43. string path = this.PathResolver.CreateIndexTypePath(index, type, "_mapping");
  44. var typeMapping = this.CreateMapFor<T>(type, maxRecursion);
  45. typeMapping.Name = type;
  46. return this.Map(typeMapping, index, type, ignoreConflicts: false);
  47. }
  48. /// <summary>
  49. /// <para>Automatically map an object based on its attributes, this will also explicitly map strings to strings, datetimes to dates etc even
  50. /// if they are not marked with any attributes.</para>
  51. /// <para>
  52. /// Type name is the inferred type name for T under the default index
  53. /// </para>
  54. /// </summary>
  55. public IIndicesResponse MapFromAttributes(Type t, int maxRecursion = 0)
  56. {
  57. string type = this.TypeNameResolver.GetTypeNameForType(t);
  58. return this.MapFromAttributes(t, this.IndexNameResolver.GetIndexForType(t), type, maxRecursion);
  59. }
  60. /// <summary>
  61. /// <para>Automatically map an object based on its attributes, this will also explicitly map strings to strings, datetimes to dates etc even
  62. /// if they are not marked with any attributes.</para>
  63. /// <para>
  64. /// Type name is the inferred type name for T under the specified index
  65. /// </para>
  66. /// </summary>
  67. public IIndicesResponse MapFromAttributes(Type t, string index, int maxRecursion = 0)
  68. {
  69. string type = this.TypeNameResolver.GetTypeNameForType(t);
  70. return this.MapFromAttributes(t, index, type, maxRecursion);
  71. }
  72. /// <summary>
  73. /// <para>Automatically map an object based on its attributes, this will also explicitly map strings to strings, datetimes to dates etc even
  74. /// if they are not marked with any attributes.</para>
  75. /// <para>
  76. /// Type name is the specified type name under the specified index
  77. /// </para>
  78. /// </summary>
  79. public IIndicesResponse MapFromAttributes(Type t, string index, string type, int maxRecursion = 0)
  80. {
  81. string path = this.PathResolver.CreateIndexTypePath(index, type, "_mapping");
  82. var typeMapping = this.CreateMapFor(t, type, maxRecursion);
  83. typeMapping.Name = type;
  84. return this.Map(typeMapping, index, type, ignoreConflicts: false);
  85. }
  86. public IIndicesResponse MapFluent(Func<RootObjectMappingDescriptor<dynamic>, RootObjectMappingDescriptor<dynamic>> typeMappingDescriptor)
  87. {
  88. return this.MapFluent<dynamic>(typeMappingDescriptor);
  89. }
  90. public IIndicesResponse MapFluent<T>(Func<RootObjectMappingDescriptor<T>, RootObjectMappingDescriptor<T>> typeMappingDescriptor) where T : class
  91. {
  92. typeMappingDescriptor.ThrowIfNull("typeMappingDescriptor");
  93. var d = typeMappingDescriptor(new RootObjectMappingDescriptor<T>());
  94. var typeMapping = d._Mapping;
  95. var indexName = d._IndexName;
  96. if (indexName.IsNullOrEmpty())
  97. indexName = this.IndexNameResolver.GetIndexForType<T>();
  98. return this.Map(typeMapping, indexName, d._TypeName, d._IgnoreConflicts);
  99. }
  100. /// <summary>
  101. /// Verbosely and explicitly map an object using a TypeMapping object, this gives you exact control over the mapping. Index is the inferred default index
  102. /// </summary>
  103. public IIndicesResponse Map(RootObjectMapping typeMapping)
  104. {
  105. return this.Map(typeMapping, this.Settings.DefaultIndex);
  106. }
  107. /// <summary>
  108. /// Verbosely and explicitly map an object using a TypeMapping object, this gives you exact control over the mapping.
  109. /// </summary>
  110. public IIndicesResponse Map(RootObjectMapping typeMapping, string index, string typeName = null, bool ignoreConflicts = false)
  111. {
  112. if (typeName.IsNullOrEmpty())
  113. typeName = typeMapping.Name;
  114. var mapping = new Dictionary<string, RootObjectMapping>();
  115. mapping.Add(typeMapping.Name, typeMapping);
  116. string map = JsonConvert.SerializeObject(mapping, Formatting.None, SerializationSettings);
  117. return MapRaw(typeName, map, index, ignoreConflicts);
  118. }
  119. /// <summary>
  120. /// Explicitly map an object using direct json input, the json should be of the form { "type" = {mapping} }.
  121. /// </summary>
  122. public IIndicesResponse MapRaw(string typeName, string map, string index, bool ignoreConflicts = false)
  123. {
  124. string path = this.PathResolver.CreateIndexTypePath(index, typeName, "_mapping");
  125. if (ignoreConflicts)
  126. path += "?ignore_conflicts=true";
  127. ConnectionStatus status = this.Connection.PutSync(path, map);
  128. var r = this.ToParsedResponse<IndicesResponse>(status);
  129. return r;
  130. }
  131. private RootObjectMapping CreateMapFor<T>(string type, int maxRecursion = 0) where T : class
  132. {
  133. return this.CreateMapFor(typeof(T), type, maxRecursion);
  134. }
  135. private RootObjectMapping CreateMapFor(Type t, string type, int maxRecursion = 0)
  136. {
  137. var writer = new TypeMappingWriter(t, type, maxRecursion);
  138. var typeMapping = writer.RootObjectMappingFromAttributes();
  139. return typeMapping;
  140. }
  141. }
  142. }