|
| 1 | +using System; |
| 2 | +using System.Collections; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Linq; |
| 5 | +using System.Linq.Expressions; |
| 6 | +using System.Text; |
| 7 | +using Nest.Resolvers; |
| 8 | +using Newtonsoft.Json; |
| 9 | +using Newtonsoft.Json.Converters; |
| 10 | + |
| 11 | +namespace Nest.DSL.Query |
| 12 | +{ |
| 13 | + [JsonObject(MemberSerialization = MemberSerialization.OptIn)] |
| 14 | + public class FunctionScoreQueryDescriptor<T> : IQuery where T : class |
| 15 | + { |
| 16 | + [JsonProperty(PropertyName = "functions")] |
| 17 | + internal IEnumerable<FunctionScoreFunction<T>> _Functions { get; set; } |
| 18 | + |
| 19 | + [JsonProperty(PropertyName = "query")] |
| 20 | + internal BaseQuery _Query { get; set; } |
| 21 | + |
| 22 | + [JsonProperty(PropertyName = "score_mode")] |
| 23 | + [JsonConverter(typeof(StringEnumConverter))] |
| 24 | + FunctionScoreMode _ScoreMode { get; set; } |
| 25 | + |
| 26 | + |
| 27 | + internal bool IsConditionless |
| 28 | + { |
| 29 | + get |
| 30 | + { |
| 31 | + return this._Query == null || this._Query.IsConditionless; |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + public FunctionScoreQueryDescriptor<T> Query(Func<QueryDescriptor<T>, BaseQuery> querySelector) |
| 36 | + { |
| 37 | + querySelector.ThrowIfNull("querySelector"); |
| 38 | + var query = new QueryDescriptor<T>(); |
| 39 | + var q = querySelector(query); |
| 40 | + |
| 41 | + this._Query = q; |
| 42 | + return this; |
| 43 | + } |
| 44 | + |
| 45 | + public FunctionScoreQueryDescriptor<T> Functions(params Func<FunctionScoreFunctionsDescriptor<T>, FunctionScoreFunction<T>>[] functions) |
| 46 | + { |
| 47 | + var descriptor = new FunctionScoreFunctionsDescriptor<T>(); |
| 48 | + |
| 49 | + foreach (var f in functions) |
| 50 | + { |
| 51 | + f(descriptor); |
| 52 | + } |
| 53 | + |
| 54 | + _Functions = descriptor; |
| 55 | + |
| 56 | + return this; |
| 57 | + } |
| 58 | + |
| 59 | + public FunctionScoreQueryDescriptor<T> ScoreMode(FunctionScoreMode mode) |
| 60 | + { |
| 61 | + this._ScoreMode = mode; |
| 62 | + return this; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + public enum FunctionScoreMode |
| 67 | + { |
| 68 | + multiply, |
| 69 | + sum, |
| 70 | + avg, |
| 71 | + first, |
| 72 | + max, |
| 73 | + min |
| 74 | + } |
| 75 | + |
| 76 | + public class FunctionScoreFunctionsDescriptor<T> : IEnumerable<FunctionScoreFunction<T>> |
| 77 | + { |
| 78 | + internal List<FunctionScoreFunction<T>> _Functions { get; set; } |
| 79 | + |
| 80 | + public FunctionScoreFunctionsDescriptor() |
| 81 | + { |
| 82 | + this._Functions = new List<FunctionScoreFunction<T>>(); |
| 83 | + } |
| 84 | + |
| 85 | + public FunctionScoreFunction<T> Gauss(Expression<Func<T, object>> objectPath, Action<FunctionScoreDecayFieldDescriptor> db) |
| 86 | + { |
| 87 | + var fn = new GaussFunction<T>(objectPath, db); |
| 88 | + this._Functions.Add(fn); |
| 89 | + return fn; |
| 90 | + } |
| 91 | + |
| 92 | + public FunctionScoreFunction<T> Linear(Expression<Func<T, object>> objectPath, Action<FunctionScoreDecayFieldDescriptor> db) |
| 93 | + { |
| 94 | + var fn = new LinearFunction<T>(objectPath, db); |
| 95 | + this._Functions.Add(fn); |
| 96 | + return fn; |
| 97 | + } |
| 98 | + |
| 99 | + public FunctionScoreFunction<T> Exp(Expression<Func<T, object>> objectPath, Action<FunctionScoreDecayFieldDescriptor> db) |
| 100 | + { |
| 101 | + var fn = new ExpFunction<T>(objectPath, db); |
| 102 | + this._Functions.Add(fn); |
| 103 | + return fn; |
| 104 | + } |
| 105 | + |
| 106 | + public BoostFactorFunction<T> BoostFactor(double value) |
| 107 | + { |
| 108 | + var fn = new BoostFactorFunction<T>(value); |
| 109 | + this._Functions.Add(fn); |
| 110 | + return fn; |
| 111 | + } |
| 112 | + |
| 113 | + |
| 114 | + public IEnumerator<FunctionScoreFunction<T>> GetEnumerator() |
| 115 | + { |
| 116 | + return _Functions.GetEnumerator(); |
| 117 | + } |
| 118 | + |
| 119 | + IEnumerator IEnumerable.GetEnumerator() |
| 120 | + { |
| 121 | + return _Functions.GetEnumerator(); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + [JsonObject(MemberSerialization = MemberSerialization.OptIn)] |
| 126 | + public class FunctionScoreFunction<T> |
| 127 | + { |
| 128 | + } |
| 129 | + |
| 130 | + public class FunctionScoreDecayFieldDescriptor |
| 131 | + { |
| 132 | + [JsonProperty(PropertyName = "origin")] |
| 133 | + internal string _Origin { get; set; } |
| 134 | + |
| 135 | + [JsonProperty(PropertyName = "scale")] |
| 136 | + internal string _Scale { get; set; } |
| 137 | + |
| 138 | + [JsonProperty(PropertyName = "offset")] |
| 139 | + internal string _Offset { get; set; } |
| 140 | + |
| 141 | + [JsonProperty(PropertyName = "decay")] |
| 142 | + internal double? _Decay { get; set; } |
| 143 | + |
| 144 | + public FunctionScoreDecayFieldDescriptor Origin(string origin) |
| 145 | + { |
| 146 | + this._Origin = origin; |
| 147 | + return this; |
| 148 | + } |
| 149 | + |
| 150 | + public FunctionScoreDecayFieldDescriptor Scale(string scale) |
| 151 | + { |
| 152 | + this._Scale = scale; |
| 153 | + return this; |
| 154 | + } |
| 155 | + |
| 156 | + public FunctionScoreDecayFieldDescriptor Offset(string offset) |
| 157 | + { |
| 158 | + this._Offset = offset; |
| 159 | + return this; |
| 160 | + } |
| 161 | + |
| 162 | + public FunctionScoreDecayFieldDescriptor Decay(double? decay) |
| 163 | + { |
| 164 | + this._Decay = decay; |
| 165 | + return this; |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + [JsonObject(MemberSerialization = MemberSerialization.OptIn)] |
| 170 | + public class FunctionScoreDecayFunction<T> : FunctionScoreFunction<T> |
| 171 | + { |
| 172 | + } |
| 173 | + |
| 174 | + [JsonObject(MemberSerialization = MemberSerialization.OptIn)] |
| 175 | + public class GaussFunction<T> : FunctionScoreDecayFunction<T> |
| 176 | + { |
| 177 | + [JsonProperty(PropertyName = "gauss")] |
| 178 | + [JsonConverter(typeof(DictionaryKeysAreNotPropertyNamesJsonConverter))] |
| 179 | + internal IDictionary<string, FunctionScoreDecayFieldDescriptor> _GaussDescriptor { get; set; } |
| 180 | + |
| 181 | + public GaussFunction(Expression<Func<T, object>> objectPath, Action<FunctionScoreDecayFieldDescriptor> descriptorBuilder) |
| 182 | + { |
| 183 | + _GaussDescriptor = new Dictionary<string, FunctionScoreDecayFieldDescriptor>(); |
| 184 | + |
| 185 | + var resolver = new PropertyNameResolver(); |
| 186 | + var fieldName = resolver.Resolve(objectPath); |
| 187 | + var descriptor = new FunctionScoreDecayFieldDescriptor(); |
| 188 | + descriptorBuilder(descriptor); |
| 189 | + _GaussDescriptor[fieldName] = descriptor; |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + [JsonObject(MemberSerialization = MemberSerialization.OptIn)] |
| 194 | + public class LinearFunction<T> : FunctionScoreDecayFunction<T> |
| 195 | + { |
| 196 | + [JsonProperty(PropertyName = "linear")] |
| 197 | + [JsonConverter(typeof(DictionaryKeysAreNotPropertyNamesJsonConverter))] |
| 198 | + internal IDictionary<string, FunctionScoreDecayFieldDescriptor> _LinearDescriptor { get; set; } |
| 199 | + |
| 200 | + public LinearFunction(Expression<Func<T, object>> objectPath, Action<FunctionScoreDecayFieldDescriptor> descriptorBuilder) |
| 201 | + { |
| 202 | + _LinearDescriptor = new Dictionary<string, FunctionScoreDecayFieldDescriptor>(); |
| 203 | + |
| 204 | + var resolver = new PropertyNameResolver(); |
| 205 | + var fieldName = resolver.Resolve(objectPath); |
| 206 | + var descriptor = new FunctionScoreDecayFieldDescriptor(); |
| 207 | + descriptorBuilder(descriptor); |
| 208 | + _LinearDescriptor[fieldName] = descriptor; |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | + [JsonObject(MemberSerialization = MemberSerialization.OptIn)] |
| 213 | + public class ExpFunction<T> : FunctionScoreDecayFunction<T> |
| 214 | + { |
| 215 | + [JsonProperty(PropertyName = "exp")] |
| 216 | + [JsonConverter(typeof(DictionaryKeysAreNotPropertyNamesJsonConverter))] |
| 217 | + internal IDictionary<string, FunctionScoreDecayFieldDescriptor> _ExpDescriptor { get; set; } |
| 218 | + |
| 219 | + public ExpFunction(Expression<Func<T, object>> objectPath, Action<FunctionScoreDecayFieldDescriptor> descriptorBuilder) |
| 220 | + { |
| 221 | + _ExpDescriptor = new Dictionary<string, FunctionScoreDecayFieldDescriptor>(); |
| 222 | + |
| 223 | + var resolver = new PropertyNameResolver(); |
| 224 | + var fieldName = resolver.Resolve(objectPath); |
| 225 | + var descriptor = new FunctionScoreDecayFieldDescriptor(); |
| 226 | + descriptorBuilder(descriptor); |
| 227 | + _ExpDescriptor[fieldName] = descriptor; |
| 228 | + } |
| 229 | + } |
| 230 | + |
| 231 | + [JsonObject(MemberSerialization = MemberSerialization.OptIn)] |
| 232 | + public class BoostFactorFunction<T> : FunctionScoreFunction<T> |
| 233 | + { |
| 234 | + [JsonProperty(PropertyName = "boost_factor")] |
| 235 | + internal double _BoostFactor { get; set; } |
| 236 | + |
| 237 | + public BoostFactorFunction(double boostFactor) |
| 238 | + { |
| 239 | + _BoostFactor = boostFactor; |
| 240 | + } |
| 241 | + } |
| 242 | +} |
0 commit comments