private void HandleFacets( List <ReaderFacetInfo> returnedReaders, KeyValuePair <string, FacetedQueryParser.FacetResult> result, Dictionary <string, Dictionary <string, FacetValues> > facetsByName, bool legacy, CancellationToken token) { var needToApplyAggregation = result.Value.Aggregations.Count > 0; foreach (var readerFacetInfo in returnedReaders) { var termsForField = IndexedTerms.GetTermsAndDocumentsFor(readerFacetInfo.Reader, readerFacetInfo.DocBase, result.Value.AggregateBy, _indexName, _state); if (facetsByName.TryGetValue(result.Key, out var facetValues) == false) { facetsByName[result.Key] = facetValues = new Dictionary <string, FacetValues>(); } foreach (var kvp in termsForField) { token.ThrowIfCancellationRequested(); var intersectedDocuments = GetIntersectedDocuments(new ArraySegment <int>(kvp.Value), readerFacetInfo.Results, needToApplyAggregation); var intersectCount = intersectedDocuments.Count; if (intersectCount == 0) { continue; } if (facetValues.TryGetValue(kvp.Key, out var collectionOfFacetValues) == false) { var range = FacetedQueryHelper.GetRangeName(result.Value.AggregateBy, kvp.Key); collectionOfFacetValues = new FacetValues(legacy); if (needToApplyAggregation == false) { collectionOfFacetValues.AddDefault(range); } else { foreach (var aggregation in result.Value.Aggregations) { collectionOfFacetValues.Add(aggregation.Key, range); } } facetValues.Add(kvp.Key, collectionOfFacetValues); } collectionOfFacetValues.IncrementCount(intersectCount); if (needToApplyAggregation) { var docsInQuery = new ArraySegment <int>(intersectedDocuments.Documents, 0, intersectedDocuments.Count); ApplyAggregation(result.Value.Aggregations, collectionOfFacetValues, docsInQuery, readerFacetInfo.Reader, readerFacetInfo.DocBase, _state); } } } }
private void HandleRangeFacets( List <ReaderFacetInfo> returnedReaders, KeyValuePair <string, FacetedQueryParser.FacetResult> result, bool legacy, CancellationToken token) { var needToApplyAggregation = result.Value.Aggregations.Count > 0; var facetValues = new Dictionary <string, FacetValues>(); var ranges = result.Value.Ranges; foreach (var range in ranges) { var key = range.RangeText; if (facetValues.TryGetValue(key, out var collectionOfFacetValues)) { continue; } collectionOfFacetValues = new FacetValues(legacy); if (needToApplyAggregation == false) { collectionOfFacetValues.AddDefault(key); } else { foreach (var aggregation in result.Value.Aggregations) { collectionOfFacetValues.Add(aggregation.Key, key); } } facetValues.Add(key, collectionOfFacetValues); } foreach (var readerFacetInfo in returnedReaders) { var name = FieldUtil.ApplyRangeSuffixIfNecessary(result.Value.AggregateBy, result.Value.RangeType); var termsForField = IndexedTerms.GetTermsAndDocumentsFor(readerFacetInfo.Reader, readerFacetInfo.DocBase, name, _indexName, _state); foreach (var kvp in termsForField) { foreach (var range in ranges) { token.ThrowIfCancellationRequested(); if (range.IsMatch(kvp.Key) == false) { continue; } var intersectedDocuments = GetIntersectedDocuments(new ArraySegment <int>(kvp.Value), readerFacetInfo.Results, needToApplyAggregation); var intersectCount = intersectedDocuments.Count; if (intersectCount == 0) { continue; } var collectionOfFacetValues = facetValues[range.RangeText]; collectionOfFacetValues.IncrementCount(intersectCount); if (needToApplyAggregation) { var docsInQuery = new ArraySegment <int>(intersectedDocuments.Documents, 0, intersectedDocuments.Count); ApplyAggregation(result.Value.Aggregations, collectionOfFacetValues, docsInQuery, readerFacetInfo.Reader, readerFacetInfo.DocBase, _state); IntArraysPool.Instance.FreeArray(intersectedDocuments.Documents); intersectedDocuments.Documents = null; } } } } foreach (var kvp in facetValues) { if (kvp.Value.Any == false) { continue; } result.Value.Result.Values.AddRange(kvp.Value.GetAll()); } }
private static void ApplyAggregation(Dictionary <FacetAggregationField, FacetedQueryParser.FacetResult.Aggregation> aggregations, FacetValues values, ArraySegment <int> docsInQuery, IndexReader indexReader, int docBase, IState state) { foreach (var kvp in aggregations) { if (string.IsNullOrEmpty(kvp.Key.Name)) // Count { continue; } var value = values.Get(kvp.Key); var name = FieldUtil.ApplyRangeSuffixIfNecessary(kvp.Key.Name, RangeType.Double); var doubles = FieldCache_Fields.DEFAULT.GetDoubles(indexReader, name, state); var val = kvp.Value; double min = value.Min ?? double.MaxValue, max = value.Max ?? double.MinValue, sum = value.Sum ?? 0, avg = value.Average ?? 0; int[] array = docsInQuery.Array; for (var index = 0; index < docsInQuery.Count; index++) { var doc = array[index]; var currentVal = doubles[doc - docBase]; sum += currentVal; avg += currentVal; min = Math.Min(min, currentVal); max = Math.Max(max, currentVal); } if (val.Min) { value.Min = min; } if (val.Average) { value.Average = avg; } if (val.Max) { value.Max = max; } if (val.Sum) { value.Sum = sum; } } }