|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using FluentAssertions; |
| 5 | +using Nest.Tests.MockData.Domain; |
| 6 | +using NUnit.Framework; |
| 7 | + |
| 8 | +namespace Nest.Tests.Integration.Mapping |
| 9 | +{ |
| 10 | + [TestFixture] |
| 11 | + public class GetMultipleMappingTests : IntegrationTests |
| 12 | + { |
| 13 | + private void TestElasticsearchProjectMapping(RootObjectMapping typeMapping) |
| 14 | + { |
| 15 | + Assert.NotNull(typeMapping); |
| 16 | + Assert.AreEqual("string", typeMapping.Properties["content"].Type.Name); |
| 17 | + Assert.AreEqual("string", typeMapping.Properties["country"].Type.Name); |
| 18 | + Assert.AreEqual("double", typeMapping.Properties["doubleValue"].Type.Name); |
| 19 | + Assert.AreEqual("long", typeMapping.Properties["longValue"].Type.Name); |
| 20 | + Assert.AreEqual("boolean", typeMapping.Properties["boolValue"].Type.Name); |
| 21 | + Assert.AreEqual("integer", typeMapping.Properties["intValues"].Type.Name); |
| 22 | + Assert.AreEqual("float", typeMapping.Properties["floatValues"].Type.Name); |
| 23 | + Assert.AreEqual("string", typeMapping.Properties["name"].Type.Name); |
| 24 | + Assert.AreEqual("date", typeMapping.Properties["startedOn"].Type.Name); |
| 25 | + Assert.AreEqual("long", typeMapping.Properties["stupidIntIWantAsLong"].Type.Name); |
| 26 | + Assert.AreEqual("float", typeMapping.Properties["floatValue"].Type.Name); |
| 27 | + Assert.AreEqual("integer", typeMapping.Properties["id"].Type.Name); |
| 28 | + Assert.AreEqual("integer", typeMapping.Properties["loc"].Type.Name); |
| 29 | + Assert.AreEqual("geo_point", typeMapping.Properties["origin"].Type.Name); |
| 30 | + Assert.AreEqual("object", typeMapping.Properties["product"].Type.Name); |
| 31 | + |
| 32 | + var productMapping = typeMapping.Properties["product"] as ObjectMapping; |
| 33 | + Assert.NotNull(productMapping); |
| 34 | + Assert.AreEqual("string", productMapping.Properties["name"].Type.Name); |
| 35 | + Assert.AreEqual("string", productMapping.Properties["id"].Type.Name); |
| 36 | + |
| 37 | + var countryMapping = typeMapping.Properties["country"] as StringMapping; |
| 38 | + Assert.NotNull(countryMapping); |
| 39 | + Assert.AreEqual(FieldIndexOption.NotAnalyzed, countryMapping.Index); |
| 40 | + } |
| 41 | + |
| 42 | + private void TestPersonMapping(RootObjectMapping typeMapping) |
| 43 | + { |
| 44 | + Assert.NotNull(typeMapping); |
| 45 | + Assert.AreEqual("string", typeMapping.Properties["email"].Type.Name); |
| 46 | + var firstNameMapping = typeMapping.Properties["email"] as StringMapping; |
| 47 | + firstNameMapping.Should().NotBeNull(); |
| 48 | + firstNameMapping.Index.Should().Be(FieldIndexOption.NotAnalyzed); |
| 49 | + } |
| 50 | + |
| 51 | + [Test] |
| 52 | + public void GetSameMappingFromTwoDifferentIndices() |
| 53 | + { |
| 54 | + var indices = new[] |
| 55 | + { |
| 56 | + ElasticsearchConfiguration.NewUniqueIndexName(), |
| 57 | + ElasticsearchConfiguration.NewUniqueIndexName() |
| 58 | + }; |
| 59 | + |
| 60 | + var x = this.Client.CreateIndex(indices.First(), s => s |
| 61 | + .AddMapping<ElasticsearchProject>(m => m.MapFromAttributes()) |
| 62 | + ); |
| 63 | + Assert.IsTrue(x.Acknowledged, x.ConnectionStatus.ToString()); |
| 64 | + |
| 65 | + x = this.Client.CreateIndex(indices.Last(), s => s |
| 66 | + .AddMapping<ElasticsearchProject>(m => m.MapFromAttributes()) |
| 67 | + ); |
| 68 | + Assert.IsTrue(x.Acknowledged, x.ConnectionStatus.ToString()); |
| 69 | + |
| 70 | + var response = this.Client.GetMapping<ElasticsearchProject>(i => i |
| 71 | + .Index(string.Join(",", indices)) |
| 72 | + .Type("elasticsearchprojects") |
| 73 | + ); |
| 74 | + response.Should().NotBeNull(); |
| 75 | + response.Mappings.Should().NotBeEmpty() |
| 76 | + .And.HaveCount(2); |
| 77 | + foreach (var indexMapping in response.Mappings) |
| 78 | + { |
| 79 | + var indexName = indexMapping.Key; |
| 80 | + indices.Should().Contain(indexName); |
| 81 | + var mappings = indexMapping.Value; |
| 82 | + mappings.Should().NotBeEmpty().And.HaveCount(1); |
| 83 | + foreach (var mapping in mappings) |
| 84 | + { |
| 85 | + mapping.TypeName.Should().Be("elasticsearchprojects"); |
| 86 | + TestElasticsearchProjectMapping(mapping.Mapping); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + |
| 91 | + } |
| 92 | + |
| 93 | + [Test] |
| 94 | + public void GetDifferentMappingsFromMultipleIndices() |
| 95 | + { |
| 96 | + var indices = new[] |
| 97 | + { |
| 98 | + ElasticsearchConfiguration.NewUniqueIndexName(), |
| 99 | + ElasticsearchConfiguration.NewUniqueIndexName() |
| 100 | + }; |
| 101 | + |
| 102 | + var x = this.Client.CreateIndex(indices.First(), s => s |
| 103 | + .AddMapping<ElasticsearchProject>(m => m.MapFromAttributes()) |
| 104 | + .AddMapping<Person>(m => m.MapFromAttributes()) |
| 105 | + ); |
| 106 | + Assert.IsTrue(x.Acknowledged, x.ConnectionStatus.ToString()); |
| 107 | + |
| 108 | + x = this.Client.CreateIndex(indices.Last(), s => s |
| 109 | + .AddMapping<ElasticsearchProject>(m => m.MapFromAttributes()) |
| 110 | + .AddMapping<GeoLocation>(m => m.MapFromAttributes()) |
| 111 | + ); |
| 112 | + Assert.IsTrue(x.Acknowledged, x.ConnectionStatus.ToString()); |
| 113 | + |
| 114 | + var response = this.Client.GetMapping(new GetMappingRequest(string.Join(",", indices), "*")); |
| 115 | + response.Should().NotBeNull(); |
| 116 | + response.Mappings.Should().NotBeEmpty() |
| 117 | + .And.HaveCount(2); |
| 118 | + foreach (var indexMapping in response.Mappings) |
| 119 | + { |
| 120 | + var indexName = indexMapping.Key; |
| 121 | + indices.Should().Contain(indexName); |
| 122 | + var mappings = indexMapping.Value; |
| 123 | + mappings.Should().NotBeEmpty().And.HaveCount(2); |
| 124 | + mappings.Should().Contain(m => m.TypeName == "elasticsearchprojects") |
| 125 | + .And.Contain(m=>m.TypeName == (indexName == indices.First() ? "person" : "geolocation")); |
| 126 | + foreach (var mapping in mappings) |
| 127 | + { |
| 128 | + switch (mapping.TypeName) |
| 129 | + { |
| 130 | + case "elasticsearchprojects": |
| 131 | + TestElasticsearchProjectMapping(mapping.Mapping); |
| 132 | + break; |
| 133 | + case "person": |
| 134 | + TestPersonMapping(mapping.Mapping); |
| 135 | + break; |
| 136 | + case "geolocation": |
| 137 | + break; |
| 138 | + default: |
| 139 | + Assert.Fail("Unexpected mapping found {0}", mapping.TypeName); |
| 140 | + break; |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + |
| 146 | + } |
| 147 | + |
| 148 | + } |
| 149 | +} |
0 commit comments