public IScoreFunction Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver) { QueryContainer filter = null; double? weight = null; IScoreFunction function = null; var count = 0; while (reader.ReadIsInObject(ref count)) { var propertyName = reader.ReadPropertyNameSegmentRaw(); if (AutomataDictionary.TryGetValue(propertyName, out var value)) { switch (value) { case 0: var formatter = formatterResolver.GetFormatter <QueryContainer>(); filter = formatter.Deserialize(ref reader, formatterResolver); break; case 1: weight = reader.ReadDouble(); break; case 2: var innerCount = 0; MultiValueMode?multiValueMode = null; IDecayFunction decayFunction = null; while (reader.ReadIsInObject(ref innerCount)) { var functionPropertyName = reader.ReadPropertyName(); if (functionPropertyName == "multi_value_mode") { multiValueMode = formatterResolver.GetFormatter <MultiValueMode>() .Deserialize(ref reader, formatterResolver); } else { var name = propertyName.Utf8String(); decayFunction = ReadDecayFunction(ref reader, name, formatterResolver); decayFunction.Field = functionPropertyName; } } if (decayFunction != null) { decayFunction.MultiValueMode = multiValueMode; function = decayFunction; } break; case 3: var randomScoreFormatter = formatterResolver.GetFormatter <RandomScoreFunction>(); function = randomScoreFormatter.Deserialize(ref reader, formatterResolver); break; case 4: var fieldValueFormatter = formatterResolver.GetFormatter <FieldValueFactorFunction>(); function = fieldValueFormatter.Deserialize(ref reader, formatterResolver); break; case 5: var scriptFormatter = formatterResolver.GetFormatter <ScriptScoreFunction>(); function = scriptFormatter.Deserialize(ref reader, formatterResolver); break; } } } if (function == null) { if (weight.HasValue) { function = new WeightFunction(); } else { return(null); } } function.Weight = weight; function.Filter = filter; return(function); }
internal static QueryContainer CombineAsShould(this QueryContainer leftContainer, QueryContainer rightContainer) { QueryContainer c = null; var leftBool = leftContainer.Self()?.Bool; var rightBool = rightContainer.Self()?.Bool; if (TryFlattenShould(leftContainer, rightContainer, leftBool, rightBool, out c)) { return(c); } var lBoolQuery = leftContainer.Self().Bool; var rBoolQuery = rightContainer.Self().Bool; var lHasShouldQueries = lBoolQuery != null && lBoolQuery.Should.HasAny(); var rHasShouldQueries = rBoolQuery != null && rBoolQuery.Should.HasAny(); var lq = lHasShouldQueries ? lBoolQuery.Should : new[] { leftContainer }; var rq = rHasShouldQueries ? rBoolQuery.Should : new[] { rightContainer }; var shouldClauses = lq.EagerConcat(rq); return(CreateShouldContainer(shouldClauses)); }
private static bool TryFlattenShould( QueryContainer leftContainer, QueryContainer rightContainer, IBoolQuery leftBool, IBoolQuery rightBool, out QueryContainer c ) { c = null; var leftCanMerge = leftContainer.CanMergeShould(); var rightCanMerge = rightContainer.CanMergeShould(); if (!leftCanMerge && !rightCanMerge) { c = CreateShouldContainer(new Containers { leftContainer, rightContainer }); } //left can merge but right's bool can not instead of wrapping into a new bool we inject the whole bool into left else if (leftCanMerge && !rightCanMerge && rightBool != null) { leftBool.Should = leftBool.Should.AddIfNotNull(rightContainer); c = leftContainer; } else if (rightCanMerge && !leftCanMerge && leftBool != null) { rightBool.Should = rightBool.Should.AddIfNotNull(leftContainer); c = rightContainer; } return(c != null); }
internal static bool NotWritable(this QueryContainer q) => q == null || !q.IsWritable;