|
| 1 | +using System; |
| 2 | +using Newtonsoft.Json; |
| 3 | + |
| 4 | +namespace Nest |
| 5 | +{ |
| 6 | + /// <summary> |
| 7 | + /// A generic property to map properties that may be of different types. |
| 8 | + /// Not all methods are valid for all types. |
| 9 | + /// </summary> |
| 10 | + [JsonObject(MemberSerialization.OptIn)] |
| 11 | + public interface IGenericProperty : IProperty |
| 12 | + { |
| 13 | + [JsonProperty("index")] |
| 14 | + FieldIndexOption? Index { get; set; } |
| 15 | + |
| 16 | + [JsonProperty("term_vector")] |
| 17 | + TermVectorOption? TermVector { get; set; } |
| 18 | + |
| 19 | + [JsonProperty("boost")] |
| 20 | + double? Boost { get; set; } |
| 21 | + |
| 22 | + [JsonProperty("null_value")] |
| 23 | + string NullValue { get; set; } |
| 24 | + |
| 25 | + [JsonProperty("norms")] |
| 26 | + INorms Norms { get; set; } |
| 27 | + |
| 28 | + [JsonProperty("index_options")] |
| 29 | + IndexOptions? IndexOptions { get; set; } |
| 30 | + |
| 31 | + [JsonProperty("analyzer")] |
| 32 | + string Analyzer { get; set; } |
| 33 | + |
| 34 | + [JsonProperty("search_analyzer")] |
| 35 | + string SearchAnalyzer { get; set; } |
| 36 | + |
| 37 | + [JsonProperty("include_in_all")] |
| 38 | + bool? IncludeInAll { get; set; } |
| 39 | + |
| 40 | + [JsonProperty("ignore_above")] |
| 41 | + int? IgnoreAbove { get; set; } |
| 42 | + |
| 43 | + [JsonProperty("position_increment_gap")] |
| 44 | + int? PositionIncrementGap { get; set; } |
| 45 | + |
| 46 | + [JsonProperty("fielddata")] |
| 47 | + IStringFielddata Fielddata { get; set; } |
| 48 | + } |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// A generic property to map properties that may be of different types. |
| 52 | + /// Not all methods are valid for all types. |
| 53 | + /// </summary> |
| 54 | + public class GenericProperty : PropertyBase, IGenericProperty |
| 55 | + { |
| 56 | + public GenericProperty() : base(null) { } |
| 57 | + |
| 58 | + public TermVectorOption? TermVector { get; set; } |
| 59 | + public double? Boost { get; set; } |
| 60 | + public string SearchAnalyzer { get; set; } |
| 61 | + public bool? IncludeInAll { get; set; } |
| 62 | + public int? IgnoreAbove { get; set; } |
| 63 | + public int? PositionIncrementGap { get; set; } |
| 64 | + public IStringFielddata Fielddata { get; set; } |
| 65 | + public FieldIndexOption? Index { get; set; } |
| 66 | + public string NullValue { get; set; } |
| 67 | + public INorms Norms { get; set; } |
| 68 | + public IndexOptions? IndexOptions { get; set; } |
| 69 | + public string Analyzer { get; set; } |
| 70 | + } |
| 71 | + |
| 72 | + /// <summary> |
| 73 | + /// A generic property to map properties that may be of different types. |
| 74 | + /// Not all methods are valid for all types. |
| 75 | + /// </summary> |
| 76 | + /// <typeparam name="T">the type on which the property is declared</typeparam> |
| 77 | + public class GenericPropertyDescriptor<T> |
| 78 | + : PropertyDescriptorBase<GenericPropertyDescriptor<T>, IGenericProperty, T>, IGenericProperty |
| 79 | + where T : class |
| 80 | + { |
| 81 | + FieldIndexOption? IGenericProperty.Index { get; set; } |
| 82 | + TermVectorOption? IGenericProperty.TermVector { get; set; } |
| 83 | + double? IGenericProperty.Boost { get; set; } |
| 84 | + string IGenericProperty.NullValue { get; set; } |
| 85 | + INorms IGenericProperty.Norms { get; set; } |
| 86 | + IndexOptions? IGenericProperty.IndexOptions { get; set; } |
| 87 | + string IGenericProperty.Analyzer { get; set; } |
| 88 | + string IGenericProperty.SearchAnalyzer { get; set; } |
| 89 | + bool? IGenericProperty.IncludeInAll { get; set; } |
| 90 | + int? IGenericProperty.IgnoreAbove { get; set; } |
| 91 | + int? IGenericProperty.PositionIncrementGap { get; set; } |
| 92 | + IStringFielddata IGenericProperty.Fielddata { get; set; } |
| 93 | + |
| 94 | + public GenericPropertyDescriptor() : base(null) { } |
| 95 | + |
| 96 | + public GenericPropertyDescriptor<T> Index(FieldIndexOption? index = FieldIndexOption.NotAnalyzed) => Assign(a => a.Index = index); |
| 97 | + |
| 98 | + public GenericPropertyDescriptor<T> Boost(double boost) => Assign(a => a.Boost = boost); |
| 99 | + |
| 100 | + public GenericPropertyDescriptor<T> NullValue(string nullValue) => Assign(a => a.NullValue = nullValue); |
| 101 | + |
| 102 | + public GenericPropertyDescriptor<T> IncludeInAll(bool includeInAll = true) => Assign(a => a.IncludeInAll = includeInAll); |
| 103 | + |
| 104 | + public GenericPropertyDescriptor<T> NotAnalyzed() => Index(FieldIndexOption.NotAnalyzed); |
| 105 | + |
| 106 | + public GenericPropertyDescriptor<T> Index(FieldIndexOption index) => Assign(a => a.Index = index); |
| 107 | + |
| 108 | + public GenericPropertyDescriptor<T> TermVector(TermVectorOption termVector) => Assign(a => a.TermVector = termVector); |
| 109 | + |
| 110 | + public GenericPropertyDescriptor<T> IndexOptions(IndexOptions indexOptions) => Assign(a => a.IndexOptions = indexOptions); |
| 111 | + |
| 112 | + public GenericPropertyDescriptor<T> Analyzer(string analyzer) => Assign(a => a.Analyzer = analyzer); |
| 113 | + |
| 114 | + public GenericPropertyDescriptor<T> SearchAnalyzer(string searchAnalyzer) => Assign(a => a.SearchAnalyzer = searchAnalyzer); |
| 115 | + |
| 116 | + public GenericPropertyDescriptor<T> Norms(Func<NormsDescriptor, INorms> selector) => Assign(a => a.Norms = selector?.Invoke(new NormsDescriptor())); |
| 117 | + |
| 118 | + public GenericPropertyDescriptor<T> IgnoreAbove(int ignoreAbove) => Assign(a => a.IgnoreAbove = ignoreAbove); |
| 119 | + |
| 120 | + public GenericPropertyDescriptor<T> PositionIncrementGap(int? positionIncrementGap) => Assign(a => a.PositionIncrementGap = positionIncrementGap); |
| 121 | + |
| 122 | + public GenericPropertyDescriptor<T> Fielddata(Func<StringFielddataDescriptor, IStringFielddata> selector) => |
| 123 | + Assign(a => a.Fielddata = selector?.Invoke(new StringFielddataDescriptor())); |
| 124 | + } |
| 125 | +} |
0 commit comments