private bool TryGetFieldValueFromDocument(Document document, FieldsToFetch.FieldToFetch field, out object value) { if (field.IsDocumentId) { value = document.Id; } else if (field.IsCompositeField == false) { if (BlittableJsonTraverserHelper.TryRead(_blittableTraverser, document, field.Name, out value) == false) { return(false); } } else { var component = new DynamicJsonValue(); foreach (var componentField in field.Components) { if (BlittableJsonTraverserHelper.TryRead(_blittableTraverser, document, componentField, out var componentValue)) { component[componentField] = componentValue; } } value = component; } return(true); }
protected override int GetFields <T>(T instance, LazyStringValue key, object doc, JsonOperationContext indexContext) { int newFields = 0; var document = (Document)doc; if (key != null) { Debug.Assert(document.LowerId == null || (key == document.LowerId)); instance.Add(GetOrCreateKeyField(key)); newFields++; } if (_reduceOutput) { instance.Add(GetReduceResultValueField(document.Data)); newFields++; } foreach (var indexField in _fields.Values) { if (BlittableJsonTraverserHelper.TryRead(_blittableTraverser, document, indexField.OriginalName ?? indexField.Name, out object value) == false) { continue; } newFields += GetRegularFields(instance, indexField, value, indexContext); } return(newFields); }
public override int HandleMap(LazyStringValue lowerId, IEnumerable mapResults, IndexWriteOperation writer, TransactionOperationContext indexContext, IndexingStatsScope stats) { EnsureValidStats(stats); var mappedResult = new DynamicJsonValue(); using (_stats.BlittableJsonAggregation.Start()) { var document = ((Document[])mapResults)[0]; Debug.Assert(lowerId == document.LowerId); foreach (var field in Definition.MapFields.Values) { var autoIndexField = field.As <AutoIndexField>(); switch (autoIndexField.Aggregation) { case AggregationOperation.Count: mappedResult[autoIndexField.Name] = 1; break; case AggregationOperation.Sum: BlittableJsonTraverserHelper.TryRead(BlittableJsonTraverser.Default, document, autoIndexField.Name, out object fieldValue); var arrayResult = fieldValue as IEnumerable <object>; if (arrayResult == null) { // explicitly adding this even if the value isn't there, as a null mappedResult[autoIndexField.Name] = fieldValue; continue; } decimal total = 0; foreach (var item in arrayResult) { if (item == null) { continue; } switch (BlittableNumber.Parse(item, out double doubleValue, out long longValue)) { case NumberParseResult.Double: total += (decimal)doubleValue; break; case NumberParseResult.Long: total += longValue; break; } } mappedResult[autoIndexField.Name] = total; break;
private static void MaybeExtractValueFromDocument(FieldsToFetch.FieldToFetch fieldToFetch, Document document, DynamicJsonValue toFill) { object value; if (BlittableJsonTraverserHelper.TryRead(BlittableJsonTraverser.Default, document, fieldToFetch.Name, out value) == false) { return; } toFill[fieldToFetch.Name.Value] = value; }
public override int HandleMap(IndexItem indexItem, IEnumerable mapResults, IndexWriteOperation writer, TransactionOperationContext indexContext, IndexingStatsScope stats) { EnsureValidStats(stats); var document = ((Document[])mapResults)[0]; Debug.Assert(indexItem.LowerId == document.LowerId); using (_stats.BlittableJsonAggregation.Start()) { DynamicJsonValue singleResult = null; var groupByFieldsCount = Definition.OrderedGroupByFields.Length; for (var i = 0; i < groupByFieldsCount; i++) { var groupByField = Definition.OrderedGroupByFields[i]; BlittableJsonTraverserHelper.TryRead(BlittableJsonTraverser.Default, document, groupByField.Name, out object result); if (_isFanout == false) { if (singleResult == null) { singleResult = new DynamicJsonValue(); } singleResult[groupByField.Name] = result; _reduceKeyProcessor.Process(indexContext.Allocator, result); } else { if (result is IEnumerable array && groupByField.GroupByArrayBehavior == GroupByArrayBehavior.ByIndividualValues) { // fanout foreach (var item in array) { _output.AddGroupByValue(groupByField.Name, item); } }
protected override IEnumerable<AbstractField> GetFields(LazyStringValue key, object doc, JsonOperationContext indexContext) { var document = (Document)doc; if (key != null) { Debug.Assert(document.LoweredKey == null || (key == document.LoweredKey)); yield return GetOrCreateKeyField(key); } if (_reduceOutput) { yield return GetReduceResultValueField(document.Data); } foreach (var indexField in _fields.Values) { object value; BlittableJsonTraverserHelper.TryRead(_blittableTraverser, document, indexField.Name, out value); foreach (var luceneField in GetRegularFields(indexField, value, indexContext)) yield return luceneField; } }
protected override int GetFields <T>(T instance, LazyStringValue key, object doc, JsonOperationContext indexContext, IWriteOperationBuffer writeBuffer) { int newFields = 0; var document = (Document)doc; if (key != null) { Debug.Assert(document.LowerId == null || (key == document.LowerId)); instance.Add(GetOrCreateKeyField(key)); newFields++; } if (_reduceOutput) { instance.Add(GetReduceResultValueField(document.Data, writeBuffer)); newFields++; } foreach (var indexField in _fields.Values) { object value; if (indexField.Spatial is AutoSpatialOptions spatialOptions) { var spatialField = CurrentIndexingScope.Current.GetOrCreateSpatialField(indexField.Name); switch (spatialOptions.MethodType) { case AutoSpatialOptions.AutoSpatialMethodType.Wkt: if (BlittableJsonTraverserHelper.TryRead(_blittableTraverser, document, spatialOptions.MethodArguments[0], out var wktValue) == false) { continue; } value = StaticIndexBase.CreateSpatialField(spatialField, wktValue); break; case AutoSpatialOptions.AutoSpatialMethodType.Point: if (BlittableJsonTraverserHelper.TryRead(_blittableTraverser, document, spatialOptions.MethodArguments[0], out var latValue) == false) { continue; } if (BlittableJsonTraverserHelper.TryRead(_blittableTraverser, document, spatialOptions.MethodArguments[1], out var lngValue) == false) { continue; } value = StaticIndexBase.CreateSpatialField(spatialField, latValue, lngValue); break; default: throw new ArgumentOutOfRangeException(); } } else { if (BlittableJsonTraverserHelper.TryRead(_blittableTraverser, document, indexField.OriginalName ?? indexField.Name, out value) == false) { continue; } } newFields += GetRegularFields(instance, indexField, value, indexContext, out _); } return(newFields); }