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()); } }