LuceneIterable implements Iterable<Vector> { @Override public Iterator<Vector> iterator() { try { return new LuceneIterator(indexReader, idField, field, mapper, normPower, maxPercentErrorDocs); } catch (IOException e) { throw new IllegalStateException(e); } } LuceneIterable(IndexReader reader, String idField, String field, VectorMapper mapper);  LuceneIterable(IndexReader indexReader, String idField, String field, VectorMapper mapper, double normPower);  LuceneIterable(IndexReader indexReader,
                        String idField,
                        String field,
                        VectorMapper mapper,
                        double normPower,
                        double maxPercentErrorDocs); @Override Iterator<Vector> iterator(); static final double NO_NORMALIZING; }
@Test(expected = IllegalStateException.class) public void testIterable_noTermVectors() throws IOException { RAMDirectory directory = createTestIndex(Field.TermVector.NO); IndexReader reader = IndexReader.open(directory, true); Weight weight = new TFIDF(); TermInfo termInfo = new CachedTermInfo(reader, "content", 1, 100); VectorMapper mapper = new TFDFMapper(reader, weight, termInfo); LuceneIterable iterable = new LuceneIterable(reader, "id", "content", mapper); Iterator<Vector> iterator = iterable.iterator(); iterator.hasNext(); iterator.next(); }
@Test public void testIterable_someNoiseTermVectors() throws IOException { RAMDirectory directory = createTestIndex(Field.TermVector.YES, new RAMDirectory(), true, 0); createTestIndex(Field.TermVector.NO, directory, false, 5); IndexReader reader = IndexReader.open(directory, true); Weight weight = new TFIDF(); TermInfo termInfo = new CachedTermInfo(reader, "content", 1, 100); VectorMapper mapper = new TFDFMapper(reader, weight, termInfo); boolean exceptionThrown; LuceneIterable iterable = new LuceneIterable(reader, "id", "content", mapper); try { Iterator<Vector> iterator = iterable.iterator(); while (iterator.hasNext()) { iterator.next(); } exceptionThrown = false; } catch(IllegalStateException ise) { exceptionThrown = true; } assertTrue(exceptionThrown); iterable = new LuceneIterable(reader, "id", "content", mapper, -1, 1.0); try { Iterator<Vector> iterator = iterable.iterator(); while (iterator.hasNext()) { iterator.next(); } exceptionThrown = false; } catch(IllegalStateException ise) { exceptionThrown = true; } assertFalse(exceptionThrown); iterable = new LuceneIterable(reader, "id", "content", mapper, -1, 0.5); Iterator<Vector> iterator = iterable.iterator(); iterator.next(); iterator.next(); iterator.next(); iterator.next(); iterator.next(); try { while (iterator.hasNext()) { iterator.next(); } exceptionThrown = false; } catch(IllegalStateException ise) { exceptionThrown = true; } assertTrue(exceptionThrown); }
@Test public void testIterable_someNoiseTermVectors() throws IOException { RAMDirectory directory = createTestIndex(Field.TermVector.YES, new RAMDirectory(), true, 0); createTestIndex(Field.TermVector.NO, directory, false, 5); IndexReader reader = IndexReader.open(directory, true); Weight weight = new TFIDF(); TermInfo termInfo = new CachedTermInfo(reader, "content", 1, 100); VectorMapper mapper = new TFDFMapper(reader, weight, termInfo); boolean exceptionThrown; LuceneIterable iterable = new LuceneIterable(reader, "id", "content", mapper); try { for (Object a : iterable) { } exceptionThrown = false; } catch(IllegalStateException ise) { exceptionThrown = true; } assertTrue(exceptionThrown); iterable = new LuceneIterable(reader, "id", "content", mapper, -1, 1.0); try { for (Object a : iterable) { } exceptionThrown = false; } catch(IllegalStateException ise) { exceptionThrown = true; } assertFalse(exceptionThrown); iterable = new LuceneIterable(reader, "id", "content", mapper, -1, 0.5); Iterator<Vector> iterator = iterable.iterator(); iterator.next(); iterator.next(); iterator.next(); iterator.next(); iterator.next(); try { while (iterator.hasNext()) { iterator.next(); } exceptionThrown = false; } catch(IllegalStateException ise) { exceptionThrown = true; } assertTrue(exceptionThrown); }