Wrap() public static method

Return a sorted view of reader according to the order defined by sort. If the reader is already sorted, this method might return the reader as-is.
public static Wrap ( AtomicReader reader, Lucene.Net.Search.Sort sort ) : AtomicReader
reader AtomicReader
sort Lucene.Net.Search.Sort
return AtomicReader
コード例 #1
0
        public override void BeforeClass() // LUCENENET specific - renamed from BeforeClassSorterUtilTest() to ensure calling order vs base class
        {
            base.BeforeClass();

            // only read the values of the undeleted documents, since after addIndexes,
            // the deleted ones will be dropped from the index.
            IBits liveDocs = reader.LiveDocs;

            JCG.List <int> values = new JCG.List <int>();
            for (int i = 0; i < reader.MaxDoc; i++)
            {
                if (liveDocs is null || liveDocs.Get(i))
                {
                    values.Add(int.Parse(reader.Document(i).Get(ID_FIELD), CultureInfo.InvariantCulture));
                }
            }
            int  idx    = Random.nextInt(SORT.Length);
            Sort sorter = SORT[idx];

            if (idx == 1)
            { // reverse doc sort
                values.Reverse();
            }
            else
            {
                values.Sort();
                if (Random.nextBoolean())
                {
                    sorter = new Sort(new SortField(NUMERIC_DV_FIELD, SortFieldType.INT64, true)); // descending
                    values.Reverse();
                }
            }
            sortedValues = values.ToArray();
            if (Verbose)
            {
                Console.WriteLine("sortedValues: " + sortedValues);
                Console.WriteLine("Sorter: " + sorter);
            }

            Directory target = NewDirectory();

            using (IndexWriter writer = new IndexWriter(target, NewIndexWriterConfig(TEST_VERSION_CURRENT, null)))
            {
                using (reader = SortingAtomicReader.Wrap(reader, sorter))
                {
                    writer.AddIndexes(reader);
                }
            }
            dir.Dispose();

            // CheckIndex the target directory
            dir = target;
            TestUtil.CheckIndex(dir);

            // set reader for tests
            reader = SlowCompositeReaderWrapper.Wrap(DirectoryReader.Open(dir));
            assertFalse("index should not have deletions", reader.HasDeletions);
        }
コード例 #2
0
        public void TestSortingMP()
        {
            AtomicReader sortedReader1 = SortingAtomicReader.Wrap(SlowCompositeReaderWrapper.Wrap(reader), sort);
            AtomicReader sortedReader2 = SlowCompositeReaderWrapper.Wrap(sortedReader);

            AssertSorted(sortedReader1);
            AssertSorted(sortedReader2);

            AssertReaderEquals("", sortedReader1, sortedReader2);
        }
コード例 #3
0
 public void TestBadSort()
 {
     try
     {
         SortingAtomicReader.Wrap(reader, Sort.RELEVANCE);
         fail("Didn't get expected exception");
     }
     catch (ArgumentException e)
     {
         assertEquals("Cannot sort an index with a Sort that refers to the relevance score", e.Message);
     }
 }
コード例 #4
0
        public override void BeforeClass() // LUCENENET specific - renamed from BeforeClassSortingAtomicReaderTest() to ensure calling order vs base class
        {
            base.BeforeClass();

            // sort the index by id (as integer, in NUMERIC_DV_FIELD)
            Sort sort = new Sort(new SortField(NUMERIC_DV_FIELD, SortFieldType.INT32));

            Sorter.DocMap docMap = new Sorter(sort).Sort(reader);

            // Sorter.compute also sorts the values
            NumericDocValues dv = reader.GetNumericDocValues(NUMERIC_DV_FIELD);

            sortedValues = new int[reader.MaxDoc];
            for (int i = 0; i < reader.MaxDoc; ++i)
            {
                sortedValues[docMap.OldToNew(i)] = (int)dv.Get(i);
            }
            if (VERBOSE)
            {
                Console.WriteLine("docMap: " + docMap);
                Console.WriteLine("sortedValues: " + Arrays.ToString(sortedValues));
            }

            // sort the index by id (as integer, in NUMERIC_DV_FIELD)
            reader = SortingAtomicReader.Wrap(reader, sort);

            if (VERBOSE)
            {
                Console.WriteLine("mapped-deleted-docs: ");
                IBits mappedLiveDocs = reader.LiveDocs;
                for (int i = 0; i < mappedLiveDocs.Length; i++)
                {
                    if (!mappedLiveDocs.Get(i))
                    {
                        Console.WriteLine(i + " ");
                    }
                }
                Console.WriteLine();
            }

            TestUtil.CheckReader(reader);
        }
コード例 #5
0
 public override IList <AtomicReader> GetMergeReaders()
 {
     if (unsortedReaders == null)
     {
         unsortedReaders = base.GetMergeReaders();
         AtomicReader atomicView;
         if (unsortedReaders.Count == 1)
         {
             atomicView = unsortedReaders[0];
         }
         else
         {
             IndexReader multiReader = new MultiReader(unsortedReaders.ToArray());
             atomicView = SlowCompositeReaderWrapper.Wrap(multiReader);
         }
         docMap     = outerInstance.sorter.Sort(atomicView);
         sortedView = SortingAtomicReader.Wrap(atomicView, docMap);
     }
     // a null doc map means that the readers are already sorted
     return(docMap == null ? unsortedReaders : new List <AtomicReader>(new AtomicReader[] { sortedView }));
 }
コード例 #6
0
        public static void BeforeClassSortingAtomicReaderTest()
        {
            // sort the index by id (as integer, in NUMERIC_DV_FIELD)
            Sort sort = new Sort(new SortField(NUMERIC_DV_FIELD, SortField.Type_e.INT));

            Sorter.DocMap docMap = new Sorter(sort).Sort(reader);

            // Sorter.compute also sorts the values
            NumericDocValues dv = reader.GetNumericDocValues(NUMERIC_DV_FIELD);

            sortedValues = new int[reader.MaxDoc];
            for (int i = 0; i < reader.MaxDoc; ++i)
            {
                sortedValues[docMap.OldToNew(i)] = (int)dv.Get(i);
            }
            if (VERBOSE)
            {
                Console.WriteLine("docMap: " + docMap);
                Console.WriteLine("sortedValues: " + Arrays.ToString(sortedValues));
            }

            // sort the index by id (as integer, in NUMERIC_DV_FIELD)
            reader = SortingAtomicReader.Wrap(reader, sort);

            if (VERBOSE)
            {
                Console.WriteLine("mapped-deleted-docs: ");
                Bits mappedLiveDocs = reader.LiveDocs;
                for (int i = 0; i < mappedLiveDocs.Length(); i++)
                {
                    if (!mappedLiveDocs.Get(i))
                    {
                        Console.WriteLine(i + " ");
                    }
                }
                Console.WriteLine();
            }

            TestUtil.CheckReader(reader);
        }