|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using Nest.Tests.MockData; |
| 5 | +using Nest.Tests.MockData.Domain; |
| 6 | +using NUnit.Framework; |
| 7 | +using System.Diagnostics; |
| 8 | +using FluentAssertions; |
| 9 | + |
| 10 | +namespace Nest.Tests.Integration.Reproduce |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// tests to reproduce reported errors |
| 14 | + /// </summary> |
| 15 | + [TestFixture] |
| 16 | + public class Reproduce325Tests : IntegrationTests |
| 17 | + { |
| 18 | + |
| 19 | + /// <summary> |
| 20 | + /// https://github.com/Mpdreamz/NEST/issues/325 |
| 21 | + /// </summary> |
| 22 | + [Test] |
| 23 | + public void FluentMappingReturnsResults() |
| 24 | + { |
| 25 | + var indexName = ElasticsearchConfiguration.NewUniqueIndexName(); |
| 26 | + this._client.CreateIndex(indexName, settings => settings |
| 27 | + .Settings(s => s.Add("search.slowlog.threshold.fetch.warn", "1s")) |
| 28 | + .Analysis(x => |
| 29 | + { |
| 30 | + var descriptor = x.Analyzers(i => i.Add("autocomplete", new CustomAnalyzer |
| 31 | + { |
| 32 | + Tokenizer = new WhitespaceTokenizer().Type, |
| 33 | + Filter = new[] { new LowercaseTokenFilter().Type, "engram" } |
| 34 | + })); |
| 35 | + |
| 36 | + descriptor.TokenFilters(i => i.Add("engram", new EdgeNGramTokenFilter |
| 37 | + { |
| 38 | + MinGram = 3, |
| 39 | + MaxGram = 10 |
| 40 | + } |
| 41 | + )); |
| 42 | + |
| 43 | + return descriptor; |
| 44 | + }) |
| 45 | + .AddMapping<TechnicalProduct>(m => MapTechnicalProduct(m, indexName))); |
| 46 | + |
| 47 | + } |
| 48 | + |
| 49 | + |
| 50 | + private static RootObjectMappingDescriptor<TechnicalProduct> MapTechnicalProduct(RootObjectMappingDescriptor<TechnicalProduct> m, string indexName) |
| 51 | + { |
| 52 | + return m |
| 53 | + .TypeName("technicalProducts") |
| 54 | + .DateDetection() |
| 55 | + .NumericDetection() |
| 56 | + .DynamicDateFormats(new[] { "dateOptionalTime", "yyyy/MM/dd HH:mm:ss Z||yyyy/MM/dd Z" }) |
| 57 | + .IndexName(indexName) |
| 58 | + .Dynamic(false) |
| 59 | + .IdField(i => i |
| 60 | + .SetIndex("not_analyzed") |
| 61 | + .SetStored() |
| 62 | + ) |
| 63 | + .Properties(o => o |
| 64 | + .String(i => i |
| 65 | + .Name(x => x.Name) |
| 66 | + .Index(FieldIndexOption.analyzed) |
| 67 | + .IndexAnalyzer("autocomplete") |
| 68 | + .SearchAnalyzer("standard") |
| 69 | + ) |
| 70 | + .String(i => i |
| 71 | + .Name(x => x.Brand) |
| 72 | + .Index(FieldIndexOption.analyzed) |
| 73 | + .IndexAnalyzer("autocomplete") |
| 74 | + .SearchAnalyzer("standard") |
| 75 | + ) |
| 76 | + ); |
| 77 | + } |
| 78 | + |
| 79 | + public class TechnicalProduct : Product |
| 80 | + { |
| 81 | + public virtual int NumberProcessorCores { get; protected set; } |
| 82 | + public virtual decimal ProcessorSpeed { get; protected set; } |
| 83 | + public virtual decimal BatteryLife { get; protected set; } |
| 84 | + public virtual int HardDriveSize { get; protected set; } |
| 85 | + public virtual decimal ScreenSize { get; protected set; } |
| 86 | + public virtual decimal Memory { get; protected set; } |
| 87 | + public virtual int HardDriveSpeed { get; protected set; } |
| 88 | + |
| 89 | + |
| 90 | + protected TechnicalProduct() { } |
| 91 | + |
| 92 | + public TechnicalProduct(string brand, string name, string imageUrl, decimal price, int numberCores, decimal processorSpeed, decimal batteryLife, int hardDriveSize, decimal screenSize, |
| 93 | + decimal memory, int hardDriveSpeed) |
| 94 | + : base(brand, name, price, imageUrl) |
| 95 | + { |
| 96 | + NumberProcessorCores = numberCores; |
| 97 | + ProcessorSpeed = processorSpeed; |
| 98 | + BatteryLife = batteryLife; |
| 99 | + Price = price; |
| 100 | + HardDriveSize = hardDriveSize; |
| 101 | + ScreenSize = screenSize; |
| 102 | + Memory = memory; |
| 103 | + HardDriveSpeed = hardDriveSpeed; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + public abstract class Product : Entity |
| 108 | + { |
| 109 | + private readonly Dictionary<string, string> _additionalAttributes; |
| 110 | + |
| 111 | + public virtual string Brand { get; protected set; } |
| 112 | + public virtual decimal Price { get; protected set; } |
| 113 | + public virtual string ImageUrl { get; protected set; } |
| 114 | + public virtual string Name { get; protected set; } |
| 115 | + |
| 116 | + public virtual IDictionary<string, string> AdditionalAttributes |
| 117 | + { |
| 118 | + get { return _additionalAttributes; } |
| 119 | + } |
| 120 | + |
| 121 | + protected Product() |
| 122 | + { |
| 123 | + _additionalAttributes = new Dictionary<string, string>(); |
| 124 | + } |
| 125 | + |
| 126 | + protected Product(string brand, string name, decimal price, string imageUrl) |
| 127 | + : this() |
| 128 | + { |
| 129 | + Brand = brand; |
| 130 | + Name = name; |
| 131 | + Price = price; |
| 132 | + ImageUrl = imageUrl; |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + public abstract class Entity |
| 137 | + { |
| 138 | + private int? _oldHashCode; |
| 139 | + |
| 140 | + public virtual int Id { get; protected set; } |
| 141 | + |
| 142 | + public virtual bool Equals(Entity other) |
| 143 | + { |
| 144 | + return this == other; |
| 145 | + } |
| 146 | + |
| 147 | + public static bool operator ==(Entity left, Entity right) |
| 148 | + { |
| 149 | + return Equals(left, right); |
| 150 | + } |
| 151 | + |
| 152 | + public static bool operator !=(Entity left, Entity right) |
| 153 | + { |
| 154 | + return !Equals(left, right); |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments