public void AddToNonSelected(FacetMatch match)
        {
            if (NonSelectedMatches.Count >= _facetFieldInfoToCalculateFor.MaxToFetchExcludingSelections)
            {
                if (match.Count < MinCountForNonSelected)
                    return;
                if (match.Count > MinCountForNonSelected)
                {
                    //Remove tail if possible
                    while (true)
                    {
                        var allWithMinCount = NonSelectedMatches.Where(x => x.Count == MinCountForNonSelected).ToList();
                        if (allWithMinCount.Count == 0)
                            break;
                        var countWhenAddingThisAndRemovingMin = NonSelectedMatches.Count - allWithMinCount.Count + 1;
                        if (countWhenAddingThisAndRemovingMin >= _facetFieldInfoToCalculateFor.MaxToFetchExcludingSelections)
                        {
                            allWithMinCount.ForEach(x => NonSelectedMatches.Remove(x));
                            MinCountForNonSelected = NonSelectedMatches.Min(x => x.Count);
                        }
                        else
                        {
                            break;
                        }
                    }

                }
            }

            MinCountForNonSelected = MinCountForNonSelected == 0 ? match.Count : Math.Min(MinCountForNonSelected, match.Count);

            NonSelectedMatches.Add(match);
        }
        private IEnumerable <FacetMatch> FindMatchesInQuery(Query baseQueryWithoutFacetDrilldown, IList <FacetFieldInfo> allFacetFieldInfos, FacetFieldInfo facetFieldInfoToCalculateFor)
        {
            var calculations = 0;

            var queryFilter = new CachingWrapperFilter(new QueryWrapperFilter(CreateFacetedQuery(baseQueryWithoutFacetDrilldown, allFacetFieldInfos, facetFieldInfoToCalculateFor.FieldName)));
            var bitsQueryWithoutFacetDrilldown     = new OpenBitSetDISI(queryFilter.GetDocIdSet(IndexReader).Iterator(), IndexReader.MaxDoc);
            var baseQueryWithoutFacetDrilldownCopy = new OpenBitSetDISI(bitsQueryWithoutFacetDrilldown.Bits.Length)
            {
                Bits = new long[bitsQueryWithoutFacetDrilldown.Bits.Length]
            };

            var calculatedFacetCounts = new ResultCollection(facetFieldInfoToCalculateFor);

            foreach (var facetValueBitSet in GetOrCreateFacetBitSet(facetFieldInfoToCalculateFor.FieldName).FacetValueBitSetList)
            {
                var isSelected = calculatedFacetCounts.IsSelected(facetValueBitSet.Value);

                if (!isSelected && facetValueBitSet.Count < calculatedFacetCounts.MinCountForNonSelected) //Impossible to get a better result
                {
                    if (calculatedFacetCounts.HaveEnoughResults)
                    {
                        break;
                    }
                }

                bitsQueryWithoutFacetDrilldown.Bits.CopyTo(baseQueryWithoutFacetDrilldownCopy.Bits, 0);
                baseQueryWithoutFacetDrilldownCopy.NumWords = bitsQueryWithoutFacetDrilldown.NumWords;

                var bitset = facetValueBitSet.Bitset ?? CalculateOpenBitSetDisi(facetFieldInfoToCalculateFor.FieldName, facetValueBitSet.Value);
                baseQueryWithoutFacetDrilldownCopy.And(bitset);
                var count = baseQueryWithoutFacetDrilldownCopy.Cardinality();
                if (count == 0)
                {
                    continue;
                }
                var match = new FacetMatch
                {
                    Count          = count,
                    Value          = facetValueBitSet.Value,
                    FacetFieldName = facetFieldInfoToCalculateFor.FieldName
                };

                calculations++;
                if (isSelected)
                {
                    calculatedFacetCounts.AddToSelected(match);
                }
                else
                {
                    calculatedFacetCounts.AddToNonSelected(match);
                }
            }

            return(calculatedFacetCounts.GetList());
        }
        public void AddToNonSelected(FacetMatch match)
        {
            if (NonSelectedMatches.Count >= _facetFieldInfoToCalculateFor.MaxToFetchExcludingSelections)
            {
                if (match.Count < MinCountForNonSelected)
                {
                    return;
                }
                if (match.Count > MinCountForNonSelected)
                {
                    //Remove tail if possible
                    while (true)
                    {
                        var allWithMinCount = NonSelectedMatches.Where(x => x.Count == MinCountForNonSelected).ToList();
                        if (allWithMinCount.Count == 0)
                        {
                            break;
                        }
                        var countWhenAddingThisAndRemovingMin = NonSelectedMatches.Count - allWithMinCount.Count + 1;
                        if (countWhenAddingThisAndRemovingMin >= _facetFieldInfoToCalculateFor.MaxToFetchExcludingSelections)
                        {
                            allWithMinCount.ForEach(x => NonSelectedMatches.Remove(x));
                            MinCountForNonSelected = NonSelectedMatches.Min(x => x.Count);
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }

            MinCountForNonSelected = MinCountForNonSelected == 0 ? match.Count : Math.Min(MinCountForNonSelected, match.Count);

            NonSelectedMatches.Add(match);
        }
 public void AddToSelected(FacetMatch match)
 {
     SelectedMatches.Add(match);
     _uncalculatedSelectedCount--;
 }
        private IEnumerable<FacetMatch> FindMatchesInQuery(Query baseQueryWithoutFacetDrilldown, IList<FacetFieldInfo> allFacetFieldInfos, FacetFieldInfo facetFieldInfoToCalculateFor)
        {
            var calculations = 0;

            var queryFilter = new CachingWrapperFilter(new QueryWrapperFilter(CreateFacetedQuery(baseQueryWithoutFacetDrilldown, allFacetFieldInfos, facetFieldInfoToCalculateFor.FieldName)));
            var bitsQueryWithoutFacetDrilldown = new OpenBitSetDISI(queryFilter.GetDocIdSet(IndexReader).Iterator(), IndexReader.MaxDoc);
            var baseQueryWithoutFacetDrilldownCopy = new OpenBitSetDISI(bitsQueryWithoutFacetDrilldown.Bits.Length)
            {
                Bits = new long[bitsQueryWithoutFacetDrilldown.Bits.Length]
            };

            var calculatedFacetCounts = new ResultCollection(facetFieldInfoToCalculateFor);
            foreach (var facetValueBitSet in GetOrCreateFacetBitSet(facetFieldInfoToCalculateFor.FieldName).FacetValueBitSetList)
            {
                var isSelected = calculatedFacetCounts.IsSelected(facetValueBitSet.Value);

                if (!isSelected && facetValueBitSet.Count < calculatedFacetCounts.MinCountForNonSelected) //Impossible to get a better result
                {
                    if (calculatedFacetCounts.HaveEnoughResults)
                        break;
                }

                bitsQueryWithoutFacetDrilldown.Bits.CopyTo(baseQueryWithoutFacetDrilldownCopy.Bits, 0);
                baseQueryWithoutFacetDrilldownCopy.NumWords = bitsQueryWithoutFacetDrilldown.NumWords;

                var bitset = facetValueBitSet.Bitset ?? CalculateOpenBitSetDisi(facetFieldInfoToCalculateFor.FieldName, facetValueBitSet.Value);
                baseQueryWithoutFacetDrilldownCopy.And(bitset);
                var count = baseQueryWithoutFacetDrilldownCopy.Cardinality();
                if (count == 0)
                    continue;
                var match = new FacetMatch
                {
                    Count = count,
                    Value = facetValueBitSet.Value,
                    FacetFieldName = facetFieldInfoToCalculateFor.FieldName
                };

                calculations++;
                if (isSelected)
                    calculatedFacetCounts.AddToSelected(match);
                else
                    calculatedFacetCounts.AddToNonSelected(match);
            }

            return calculatedFacetCounts.GetList();
        }
 public void AddToSelected(FacetMatch match)
 {
     SelectedMatches.Add(match);
     _uncalculatedSelectedCount--;
 }