Beispiel #1
0
        private static void ApplyAggregation(Facet facet, FacetValue value, ArraySegment <int> docsInQuery, IndexReader indexReader, int docBase, IState state)
        {
            var name      = facet.AggregationField;
            var rangeType = FieldUtil.GetRangeTypeFromFieldName(name);

            if (rangeType == RangeType.None)
            {
                name      = FieldUtil.ApplyRangeSuffixIfNecessary(facet.AggregationField, RangeType.Double);
                rangeType = RangeType.Double;
            }

            long[]   longs   = null;
            double[] doubles = null;
            switch (rangeType)
            {
            case RangeType.Long:
                longs = FieldCache_Fields.DEFAULT.GetLongs(indexReader, name, state);
                break;

            case RangeType.Double:
                doubles = FieldCache_Fields.DEFAULT.GetDoubles(indexReader, name, state);
                break;

            default:
                throw new InvalidOperationException("Invalid range type for " + facet.Name + ", don't know how to handle " + rangeType);
            }

            for (int index = 0; index < docsInQuery.Count; index++)
            {
                var doc = docsInQuery.Array[index];

                var currentVal = rangeType == RangeType.Long ? longs[doc - docBase] : doubles[doc - docBase];
                if ((facet.Aggregation & FacetAggregation.Max) == FacetAggregation.Max)
                {
                    value.Max = Math.Max(value.Max ?? double.MinValue, currentVal);
                }

                if ((facet.Aggregation & FacetAggregation.Min) == FacetAggregation.Min)
                {
                    value.Min = Math.Min(value.Min ?? double.MaxValue, currentVal);
                }

                if ((facet.Aggregation & FacetAggregation.Sum) == FacetAggregation.Sum)
                {
                    value.Sum = currentVal + (value.Sum ?? 0d);
                }

                if ((facet.Aggregation & FacetAggregation.Average) == FacetAggregation.Average)
                {
                    value.Average = currentVal + (value.Average ?? 0d);
                }
            }
        }
        public static string GetRangeName(string field, string text)
        {
            var rangeType = FieldUtil.GetRangeTypeFromFieldName(field);

            switch (rangeType)
            {
            case RangeType.Long:
                return(NumericUtils.PrefixCodedToLong(text).ToInvariantString());

            case RangeType.Double:
                return(NumericUtils.PrefixCodedToDouble(text).ToInvariantString());

            default:
                return(text);
            }
        }
Beispiel #3
0
        private static string ConvertFieldValue(string field, string value)
        {
            if (NumberUtil.IsNull(value))
            {
                return(null);
            }

            var rangeType = FieldUtil.GetRangeTypeFromFieldName(field);

            switch (rangeType)
            {
            case RangeType.Long:
                var longValue = NumberUtil.StringToLong(value);
                return(NumericUtils.LongToPrefixCoded(longValue.Value));

            case RangeType.Double:
                var doubleValue = NumberUtil.StringToDouble(value);
                return(NumericUtils.DoubleToPrefixCoded(doubleValue.Value));

            default:
                return(value);
            }
        }
        protected Document GetProjection(Lucene.Net.Documents.Document input, Lucene.Net.Search.ScoreDoc scoreDoc, string lowerId, IState state)
        {
            using (_projectionScope = _projectionScope?.Start() ?? RetrieverScope?.For(nameof(QueryTimingsScope.Names.Projection)))
            {
                Document doc = null;
                if (FieldsToFetch.AnyExtractableFromIndex == false)
                {
                    using (_projectionStorageScope = _projectionStorageScope?.Start() ?? _projectionScope?.For(nameof(QueryTimingsScope.Names.Storage)))
                        doc = DirectGet(input, lowerId, DocumentFields.All, state);

                    if (doc == null)
                    {
                        return(null);
                    }
                    return(GetProjectionFromDocumentInternal(doc, input, scoreDoc, FieldsToFetch, _context, state));
                }

                var documentLoaded = false;

                var result = new DynamicJsonValue();

                Dictionary <string, FieldsToFetch.FieldToFetch> fields = null;
                if (FieldsToFetch.ExtractAllFromIndex)
                {
                    fields = input.GetFields()
                             .Where(x => x.Name != Constants.Documents.Indexing.Fields.DocumentIdFieldName &&
                                    x.Name != Constants.Documents.Indexing.Fields.SourceDocumentIdFieldName &&
                                    x.Name != Constants.Documents.Indexing.Fields.ReduceKeyHashFieldName &&
                                    x.Name != Constants.Documents.Indexing.Fields.ReduceKeyValueFieldName &&
                                    x.Name != Constants.Documents.Indexing.Fields.ValueFieldName &&
                                    FieldUtil.GetRangeTypeFromFieldName(x.Name) == RangeType.None)
                             .Distinct(UniqueFieldNames.Instance)
                             .ToDictionary(x => x.Name, x => new FieldsToFetch.FieldToFetch(x.Name, null, null, x.IsStored, isDocumentId: false, isTimeSeries: false));
                }

                if (fields == null)
                {
                    fields = FieldsToFetch.Fields;
                }
                else if (FieldsToFetch.Fields != null && FieldsToFetch.Fields.Count > 0)
                {
                    foreach (var kvp in FieldsToFetch.Fields)
                    {
                        if (fields.ContainsKey(kvp.Key))
                        {
                            continue;
                        }

                        fields[kvp.Key] = kvp.Value;
                    }
                }

                foreach (var fieldToFetch in fields.Values)
                {
                    if (TryExtractValueFromIndex(fieldToFetch, input, result, state))
                    {
                        continue;
                    }

                    if (documentLoaded == false)
                    {
                        using (_projectionStorageScope = _projectionStorageScope?.Start() ?? _projectionScope?.For(nameof(QueryTimingsScope.Names.Storage)))
                            doc = DirectGet(input, lowerId, DocumentFields.All, state);

                        documentLoaded = true;
                    }

                    if (doc == null)
                    {
                        continue;
                    }

                    if (TryGetValue(fieldToFetch, doc, input, state, FieldsToFetch.IndexFields, FieldsToFetch.AnyDynamicIndexFields, out var key, out var fieldVal))
                    {
                        if (FieldsToFetch.SingleBodyOrMethodWithNoAlias)
                        {
                            if (fieldVal is BlittableJsonReaderObject nested)
                            {
                                doc.Data = nested;
                            }
                            else if (fieldVal is Document d)
                            {
                                doc = d;
                            }
                            else
                            {
                                ThrowInvalidQueryBodyResponse(fieldVal);
                            }
                            FinishDocumentSetup(doc, scoreDoc);
                            return(doc);
                        }

                        if (fieldVal is List <object> list)
                        {
                            fieldVal = new DynamicJsonArray(list);
                        }

                        if (fieldVal is Document d2)
                        {
                            fieldVal = d2.Data;
                        }

                        result[key] = fieldVal;
                    }
                }

                if (doc == null)
                {
                    doc = new Document
                    {
                        Id = _context.GetLazyString(lowerId)
                    };
                }

                return(ReturnProjection(result, doc, scoreDoc, _context));
            }
        }
Beispiel #5
0
        protected Document GetProjection(Lucene.Net.Documents.Document input, float score, string lowerId, IState state)
        {
            Document doc = null;

            if (FieldsToFetch.AnyExtractableFromIndex == false)
            {
                doc = DirectGet(input, lowerId, state);

                if (doc == null)
                {
                    return(null);
                }

                return(GetProjectionFromDocument(doc, input, score, FieldsToFetch, _context, state));
            }

            var documentLoaded = false;

            var result = new DynamicJsonValue();

            Dictionary <string, FieldsToFetch.FieldToFetch> fields = null;

            if (FieldsToFetch.ExtractAllFromIndex)
            {
                fields = input.GetFields()
                         .Where(x => x.Name != Constants.Documents.Indexing.Fields.DocumentIdFieldName &&
                                x.Name != Constants.Documents.Indexing.Fields.ReduceKeyHashFieldName &&
                                x.Name != Constants.Documents.Indexing.Fields.ReduceKeyValueFieldName &&
                                FieldUtil.GetRangeTypeFromFieldName(x.Name) == RangeType.None)
                         .Distinct(UniqueFieldNames.Instance)
                         .ToDictionary(x => x.Name, x => new FieldsToFetch.FieldToFetch(x.Name, null, null, x.IsStored, isDocumentId: false));
            }

            if (fields == null)
            {
                fields = FieldsToFetch.Fields;
            }
            else if (FieldsToFetch.Fields != null && FieldsToFetch.Fields.Count > 0)
            {
                foreach (var kvp in FieldsToFetch.Fields)
                {
                    if (fields.ContainsKey(kvp.Key))
                    {
                        continue;
                    }

                    fields[kvp.Key] = kvp.Value;
                }
            }

            foreach (var fieldToFetch in fields.Values)
            {
                if (TryExtractValueFromIndex(fieldToFetch, input, result, state))
                {
                    continue;
                }

                if (documentLoaded == false)
                {
                    doc = DirectGet(input, lowerId, state);

                    documentLoaded = true;
                }

                if (doc == null)
                {
                    continue;
                }

                if (TryGetValue(fieldToFetch, doc, input, state, out var fieldVal))
                {
                    if (FieldsToFetch.SingleBodyOrMethodWithNoAlias)
                    {
                        if (fieldVal is BlittableJsonReaderObject nested)
                        {
                            doc.Data = nested;
                        }
                        else if (fieldVal is Document d)
                        {
                            doc = d;
                        }
                        else
                        {
                            ThrowInvalidQueryBodyResponse(fieldVal);
                        }
                        doc.IndexScore = score;
                        return(doc);
                    }
                    if (fieldVal is List <object> list)
                    {
                        fieldVal = new DynamicJsonArray(list);
                    }
                    result[fieldToFetch.ProjectedName ?? fieldToFetch.Name.Value] = fieldVal;
                }
            }

            if (doc == null)
            {
                doc = new Document
                {
                    Id = _context.GetLazyString(lowerId)
                };
            }

            return(ReturnProjection(result, doc, score, _context));
        }