private IAbstractDistinctValuesCollector <AbstractDistinctValuesCollector.IGroupCount <T> > CreateDistinctCountCollector <T>(IAbstractFirstPassGroupingCollector <T> firstPassGroupingCollector, string groupField, string countField, DocValuesType dvType) { Random random = Random; IEnumerable <ISearchGroup <T> > searchGroups = firstPassGroupingCollector.GetTopGroups(0, false); if (typeof(FunctionFirstPassGroupingCollector).IsAssignableFrom(firstPassGroupingCollector.GetType())) { return((IAbstractDistinctValuesCollector <AbstractDistinctValuesCollector.IGroupCount <T> >) new FunctionDistinctValuesCollector(new Hashtable(), new BytesRefFieldSource(groupField), new BytesRefFieldSource(countField), searchGroups as IEnumerable <ISearchGroup <MutableValue> >)); } else { return((IAbstractDistinctValuesCollector <AbstractDistinctValuesCollector.IGroupCount <T> >) new TermDistinctValuesCollector(groupField, countField, searchGroups as IEnumerable <ISearchGroup <BytesRef> >)); } }
public virtual void TestRandom() { Random random = Random; int numberOfRuns = TestUtil.NextInt32(random, 3, 6); for (int indexIter = 0; indexIter < numberOfRuns; indexIter++) { IndexContext context = CreateIndexContext(); for (int searchIter = 0; searchIter < 100; searchIter++) { IndexSearcher searcher = NewSearcher(context.indexReader); bool useDv = context.dvType != DocValuesType.NONE && random.nextBoolean(); DocValuesType dvType = useDv ? context.dvType : DocValuesType.NONE; string term = context.contentStrings[random.nextInt(context.contentStrings.Length)]; Sort groupSort = new Sort(new SortField("id", SortFieldType.STRING)); int topN = 1 + random.nextInt(10); List <AbstractDistinctValuesCollector.IGroupCount <IComparable> > expectedResult = CreateExpectedResult(context, term, groupSort, topN); IAbstractFirstPassGroupingCollector <IComparable> firstCollector = CreateRandomFirstPassCollector(dvType, groupSort, groupField, topN); searcher.Search(new TermQuery(new Term("content", term)), firstCollector); IAbstractDistinctValuesCollector <AbstractDistinctValuesCollector.IGroupCount <IComparable> > distinctValuesCollector = CreateDistinctCountCollector(firstCollector, groupField, countField, dvType); searcher.Search(new TermQuery(new Term("content", term)), distinctValuesCollector); // LUCENENET TODO: Try to work out how to do this without an O(n) operation List <AbstractDistinctValuesCollector.IGroupCount <IComparable> > actualResult = new List <AbstractDistinctValuesCollector.IGroupCount <IComparable> >(distinctValuesCollector.Groups); if (VERBOSE) { Console.WriteLine("Index iter=" + indexIter); Console.WriteLine("Search iter=" + searchIter); Console.WriteLine("1st pass collector class name=" + firstCollector.GetType().Name); Console.WriteLine("2nd pass collector class name=" + distinctValuesCollector.GetType().Name); Console.WriteLine("Search term=" + term); Console.WriteLine("DVType=" + dvType); Console.WriteLine("1st pass groups=" + firstCollector.GetTopGroups(0, false).toString()); Console.WriteLine("Expected:"); PrintGroups(expectedResult); Console.WriteLine("Actual:"); PrintGroups(actualResult); Console.Out.Flush(); } assertEquals(expectedResult.Count, actualResult.Count); for (int i = 0; i < expectedResult.size(); i++) { AbstractDistinctValuesCollector.IGroupCount <IComparable> expected = expectedResult[i]; AbstractDistinctValuesCollector.IGroupCount <IComparable> actual = actualResult[i]; AssertValues(expected.GroupValue, actual.GroupValue); assertEquals(expected.UniqueValues.Count(), actual.UniqueValues.Count()); List <IComparable> expectedUniqueValues = new List <IComparable>(expected.UniqueValues); expectedUniqueValues.Sort(nullComparer); List <IComparable> actualUniqueValues = new List <IComparable>(actual.UniqueValues); actualUniqueValues.Sort(nullComparer); for (int j = 0; j < expectedUniqueValues.size(); j++) { AssertValues(expectedUniqueValues[j], actualUniqueValues[j]); } } } context.indexReader.Dispose(); context.directory.Dispose(); } }