org.apache.lucene.index.LeafReader Java Examples
The following examples show how to use
org.apache.lucene.index.LeafReader.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LukeRequestHandler.java From lucene-solr with Apache License 2.0 | 6 votes |
private static Document getFirstLiveDoc(Terms terms, LeafReader reader) throws IOException { PostingsEnum postingsEnum = null; TermsEnum termsEnum = terms.iterator(); BytesRef text; // Deal with the chance that the first bunch of terms are in deleted documents. Is there a better way? for (int idx = 0; idx < 1000 && postingsEnum == null; ++idx) { text = termsEnum.next(); if (text == null) { // Ran off the end of the terms enum without finding any live docs with that field in them. return null; } postingsEnum = termsEnum.postings(postingsEnum, PostingsEnum.NONE); final Bits liveDocs = reader.getLiveDocs(); if (postingsEnum.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) { if (liveDocs != null && liveDocs.get(postingsEnum.docID())) { continue; } return reader.document(postingsEnum.docID()); } } return null; }
Example #2
Source File: KNearestNeighborClassifierTest.java From lucene-solr with Apache License 2.0 | 6 votes |
@Test public void testBasicUsage() throws Exception { LeafReader leafReader = null; try { MockAnalyzer analyzer = new MockAnalyzer(random()); leafReader = getSampleIndex(analyzer); checkCorrectClassification(new KNearestNeighborClassifier(leafReader, null, analyzer, null, 1, 0, 0, categoryFieldName, textFieldName), TECHNOLOGY_INPUT, TECHNOLOGY_RESULT); checkCorrectClassification(new KNearestNeighborClassifier(leafReader, new LMDirichletSimilarity(), analyzer, null, 1, 0, 0, categoryFieldName, textFieldName), TECHNOLOGY_INPUT, TECHNOLOGY_RESULT); ClassificationResult<BytesRef> resultDS = checkCorrectClassification(new KNearestNeighborClassifier(leafReader, new BM25Similarity(), analyzer, null, 3, 2, 1, categoryFieldName, textFieldName), TECHNOLOGY_INPUT, TECHNOLOGY_RESULT); ClassificationResult<BytesRef> resultLMS = checkCorrectClassification(new KNearestNeighborClassifier(leafReader, new LMDirichletSimilarity(), analyzer, null, 3, 2, 1, categoryFieldName, textFieldName), TECHNOLOGY_INPUT, TECHNOLOGY_RESULT); assertTrue(resultDS.getScore() != resultLMS.getScore()); } finally { if (leafReader != null) { leafReader.close(); } } }
Example #3
Source File: TestGeo3DPoint.java From lucene-solr with Apache License 2.0 | 6 votes |
public static String explain(String fieldName, GeoShape shape, GeoPoint targetDocPoint, GeoPoint scaledDocPoint, IndexReader reader, int docID) throws Exception { final XYZBounds bounds = new XYZBounds(); shape.getBounds(bounds); // First find the leaf reader that owns this doc: int subIndex = ReaderUtil.subIndex(docID, reader.leaves()); LeafReader leafReader = reader.leaves().get(subIndex).reader(); StringBuilder b = new StringBuilder(); b.append("target is in leaf " + leafReader + " of full reader " + reader + "\n"); DocIdSetBuilder hits = new DocIdSetBuilder(leafReader.maxDoc()); ExplainingVisitor visitor = new ExplainingVisitor(shape, targetDocPoint, scaledDocPoint, new PointInShapeIntersectVisitor(hits, shape, bounds), docID - reader.leaves().get(subIndex).docBase, 3, Integer.BYTES, b); // Do first phase, where we just figure out the "path" that leads to the target docID: leafReader.getPointValues(fieldName).intersect(visitor); // Do second phase, where we we see how the wrapped visitor responded along that path: visitor.startSecondPhase(); leafReader.getPointValues(fieldName).intersect(visitor); return b.toString(); }
Example #4
Source File: PerThreadIDAndVersionLookup.java From Elasticsearch with Apache License 2.0 | 6 votes |
/** * Initialize lookup for the provided segment */ public PerThreadIDAndVersionLookup(LeafReader reader) throws IOException { TermsEnum termsEnum = null; NumericDocValues versions = null; boolean hasPayloads = false; Fields fields = reader.fields(); if (fields != null) { Terms terms = fields.terms(UidFieldMapper.NAME); if (terms != null) { hasPayloads = terms.hasPayloads(); termsEnum = terms.iterator(); assert termsEnum != null; versions = reader.getNumericDocValues(VersionFieldMapper.NAME); } } this.versions = versions; this.termsEnum = termsEnum; this.hasPayloads = hasPayloads; }
Example #5
Source File: ConfusionMatrixGeneratorTest.java From lucene-solr with Apache License 2.0 | 6 votes |
@Test public void testGetConfusionMatrixWithSNB() throws Exception { LeafReader reader = null; try { MockAnalyzer analyzer = new MockAnalyzer(random()); reader = getSampleIndex(analyzer); Classifier<BytesRef> classifier = new SimpleNaiveBayesClassifier(reader, analyzer, null, categoryFieldName, textFieldName); ConfusionMatrixGenerator.ConfusionMatrix confusionMatrix = ConfusionMatrixGenerator.getConfusionMatrix(reader, classifier, categoryFieldName, textFieldName, -1); checkCM(confusionMatrix); } finally { if (reader != null) { reader.close(); } } }
Example #6
Source File: LuceneBatchIterator.java From crate with Apache License 2.0 | 6 votes |
private boolean innerMoveNext() throws IOException { while (tryAdvanceDocIdSetIterator()) { LeafReader reader = currentLeaf.reader(); Bits liveDocs = reader.getLiveDocs(); int doc; while ((doc = currentDocIdSetIt.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) { if (docDeleted(liveDocs, doc) || belowMinScore(currentScorer)) { continue; } onDoc(doc); return true; } currentDocIdSetIt = null; } clearState(); return false; }
Example #7
Source File: TestCompressingTermVectorsFormat.java From lucene-solr with Apache License 2.0 | 6 votes |
public void testNoOrds() throws Exception { Directory dir = newDirectory(); RandomIndexWriter iw = new RandomIndexWriter(random(), dir); Document doc = new Document(); FieldType ft = new FieldType(TextField.TYPE_NOT_STORED); ft.setStoreTermVectors(true); doc.add(new Field("foo", "this is a test", ft)); iw.addDocument(doc); LeafReader ir = getOnlyLeafReader(iw.getReader()); Terms terms = ir.getTermVector(0, "foo"); assertNotNull(terms); TermsEnum termsEnum = terms.iterator(); assertEquals(SeekStatus.FOUND, termsEnum.seekCeil(new BytesRef("this"))); expectThrows(UnsupportedOperationException.class, termsEnum::ord); expectThrows(UnsupportedOperationException.class, () -> termsEnum.seekExact(0)); ir.close(); iw.close(); dir.close(); }
Example #8
Source File: GeoPointArrayIndexFieldData.java From Elasticsearch with Apache License 2.0 | 6 votes |
@Override public AtomicGeoPointFieldData loadDirect(LeafReaderContext context) throws Exception { LeafReader reader = context.reader(); Terms terms = reader.terms(getFieldNames().indexName()); AtomicGeoPointFieldData data = null; // TODO: Use an actual estimator to estimate before loading. NonEstimatingEstimator estimator = new NonEstimatingEstimator(breakerService.getBreaker(CircuitBreaker.FIELDDATA)); if (terms == null) { data = AbstractAtomicGeoPointFieldData.empty(reader.maxDoc()); estimator.afterLoad(null, data.ramBytesUsed()); return data; } return (Version.indexCreated(indexSettings).before(Version.V_2_2_0)) ? loadLegacyFieldData(reader, estimator, terms, data) : loadFieldData22(reader, estimator, terms, data); }
Example #9
Source File: CodecCollector.java From mtas with Apache License 2.0 | 6 votes |
/** * Collect collection. * * @param reader * the reader * @param docSet * the doc set * @param collectionInfo * the collection info * @throws IOException * Signals that an I/O exception has occurred. */ public static void collectCollection(IndexReader reader, List<Integer> docSet, ComponentCollection collectionInfo) throws IOException { if (collectionInfo.action().equals(ComponentCollection.ACTION_CHECK)) { // can't do anything in lucene for check } else if (collectionInfo.action() .equals(ComponentCollection.ACTION_LIST)) { // can't do anything in lucene for list } else if (collectionInfo.action() .equals(ComponentCollection.ACTION_CREATE)) { BytesRef term = null; PostingsEnum postingsEnum = null; Integer docId; Integer termDocId = -1; Terms terms; LeafReaderContext lrc; LeafReader r; ListIterator<LeafReaderContext> iterator = reader.leaves().listIterator(); while (iterator.hasNext()) { lrc = iterator.next(); r = lrc.reader(); for (String field : collectionInfo.fields()) { if ((terms = r.terms(field)) != null) { TermsEnum termsEnum = terms.iterator(); while ((term = termsEnum.next()) != null) { Iterator<Integer> docIterator = docSet.iterator(); postingsEnum = termsEnum.postings(postingsEnum, PostingsEnum.NONE); termDocId = -1; while (docIterator.hasNext()) { docId = docIterator.next() - lrc.docBase; if ((docId >= termDocId) && ((docId.equals(termDocId)) || ((termDocId = postingsEnum.advance(docId)) .equals(docId)))) { collectionInfo.addValue(term.utf8ToString()); break; } if (termDocId.equals(PostingsEnum.NO_MORE_DOCS)) { break; } } } } } } } }
Example #10
Source File: ConfusionMatrixGeneratorTest.java From lucene-solr with Apache License 2.0 | 6 votes |
@Test public void testGetConfusionMatrixWithBM25NB() throws Exception { LeafReader reader = null; try { MockAnalyzer analyzer = new MockAnalyzer(random()); reader = getSampleIndex(analyzer); Classifier<BytesRef> classifier = new BM25NBClassifier(reader, analyzer, null, categoryFieldName, textFieldName); ConfusionMatrixGenerator.ConfusionMatrix confusionMatrix = ConfusionMatrixGenerator.getConfusionMatrix(reader, classifier, categoryFieldName, textFieldName, -1); checkCM(confusionMatrix); } finally { if (reader != null) { reader.close(); } } }
Example #11
Source File: TopLevelJoinQuery.java From lucene-solr with Apache License 2.0 | 6 votes |
private SortedSetDocValues validateAndFetchDocValues(SolrIndexSearcher solrSearcher, String fieldName, String querySide) throws IOException { final IndexSchema schema = solrSearcher.getSchema(); final SchemaField field = schema.getFieldOrNull(fieldName); if (field == null) { throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, querySide + " field '" + fieldName + "' does not exist"); } if (!field.hasDocValues()) { throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "'top-level' join queries require both 'from' and 'to' fields to have docValues, but " + querySide + " field [" + fieldName + "] does not."); } final LeafReader leafReader = solrSearcher.getSlowAtomicReader(); if (field.multiValued()) { return DocValues.getSortedSet(leafReader, fieldName); } return DocValues.singleton(DocValues.getSorted(leafReader, fieldName)); }
Example #12
Source File: TestMemoryIndex.java From lucene-solr with Apache License 2.0 | 6 votes |
public void testIndexingPointsAndDocValues() throws Exception { FieldType type = new FieldType(); type.setDimensions(1, 4); type.setDocValuesType(DocValuesType.BINARY); type.freeze(); Document doc = new Document(); byte[] packedPoint = "term".getBytes(StandardCharsets.UTF_8); doc.add(new BinaryPoint("field", packedPoint, type)); MemoryIndex mi = MemoryIndex.fromDocument(doc, analyzer); LeafReader leafReader = mi.createSearcher().getIndexReader().leaves().get(0).reader(); assertEquals(1, leafReader.getPointValues("field").size()); assertArrayEquals(packedPoint, leafReader.getPointValues("field").getMinPackedValue()); assertArrayEquals(packedPoint, leafReader.getPointValues("field").getMaxPackedValue()); BinaryDocValues dvs = leafReader.getBinaryDocValues("field"); assertEquals(0, dvs.nextDoc()); assertEquals("term", dvs.binaryValue().utf8ToString()); }
Example #13
Source File: KNearestNeighborClassifierTest.java From lucene-solr with Apache License 2.0 | 6 votes |
/** * This test is for the scenario where in the first topK results from the MLT query, we have less results * for the expected class than the results for the bad class. * But the results for the expected class have a better score in comparison with the results of the second class. * So we would expect a greater score for the best ranked class. * * @throws Exception if any error happens */ @Test public void testUnbalancedClasses() throws Exception { LeafReader leafReader = null; try { Analyzer analyzer = new EnglishAnalyzer(); leafReader = getSampleIndex(analyzer); KNearestNeighborClassifier knnClassifier = new KNearestNeighborClassifier(leafReader, null,analyzer, null, 3, 1, 1, categoryFieldName, textFieldName); List<ClassificationResult<BytesRef>> classes = knnClassifier.getClasses(SUPER_STRONG_TECHNOLOGY_INPUT); assertTrue(classes.get(0).getScore() > classes.get(1).getScore()); checkCorrectClassification(knnClassifier, SUPER_STRONG_TECHNOLOGY_INPUT, TECHNOLOGY_RESULT); } finally { if (leafReader != null) { leafReader.close(); } } }
Example #14
Source File: UnifiedHighlighter.java From lucene-solr with Apache License 2.0 | 6 votes |
static IndexReader wrap(IndexReader reader) throws IOException { LeafReader[] leafReaders = reader.leaves().stream() .map(LeafReaderContext::reader) .map(TermVectorReusingLeafReader::new) .toArray(LeafReader[]::new); return new BaseCompositeReader<IndexReader>(leafReaders) { @Override protected void doClose() throws IOException { reader.close(); } @Override public CacheHelper getReaderCacheHelper() { return null; } }; }
Example #15
Source File: TestQueryBitSetProducer.java From lucene-solr with Apache License 2.0 | 6 votes |
public DummyDirectoryReader(DirectoryReader in) throws IOException { super(in, new SubReaderWrapper() { @Override public LeafReader wrap(LeafReader reader) { return new FilterLeafReader(reader) { @Override public CacheHelper getCoreCacheHelper() { return null; } @Override public CacheHelper getReaderCacheHelper() { return null; }}; } }); }
Example #16
Source File: StatisHelper.java From HongsCORE with MIT License | 6 votes |
@Override public LeafCollector getLeafCollector(LeafReaderContext lrc) throws IOException { LeafReader reader = lrc.reader( ); for (int i = 0; i < fields.length; i ++) { if (groups[i][0] >= 1) { if (groups[i][1] == 1) { values[i] = reader.getSortedNumericDocValues("%"+fields[i]); } else { values[i] = reader. getNumericDocValues("#"+fields[i]); } } else { if (groups[i][1] == 1) { values[i] = reader.getSortedSetDocValues("%"+fields[i]); } else { values[i] = reader. getSortedDocValues("#"+fields[i]); } } } return this; }
Example #17
Source File: TestLegacyFieldCache.java From lucene-solr with Apache License 2.0 | 5 votes |
public void testNonIndexedFields() throws Exception { Directory dir = newDirectory(); RandomIndexWriter iw = new RandomIndexWriter(random(), dir); Document doc = new Document(); doc.add(new StoredField("bogusbytes", "bogus")); doc.add(new StoredField("bogusshorts", "bogus")); doc.add(new StoredField("bogusints", "bogus")); doc.add(new StoredField("boguslongs", "bogus")); doc.add(new StoredField("bogusfloats", "bogus")); doc.add(new StoredField("bogusdoubles", "bogus")); doc.add(new StoredField("bogusbits", "bogus")); iw.addDocument(doc); DirectoryReader ir = iw.getReader(); iw.close(); LeafReader ar = getOnlyLeafReader(ir); final FieldCache cache = FieldCache.DEFAULT; cache.purgeAllCaches(); assertEquals(0, cache.getCacheEntries().length); NumericDocValues ints = cache.getNumerics(ar, "bogusints", FieldCache.LEGACY_INT_PARSER); assertEquals(NO_MORE_DOCS, ints.nextDoc()); NumericDocValues longs = cache.getNumerics(ar, "boguslongs", FieldCache.LEGACY_LONG_PARSER); assertEquals(NO_MORE_DOCS, longs.nextDoc()); NumericDocValues floats = cache.getNumerics(ar, "bogusfloats", FieldCache.LEGACY_FLOAT_PARSER); assertEquals(NO_MORE_DOCS, floats.nextDoc()); NumericDocValues doubles = cache.getNumerics(ar, "bogusdoubles", FieldCache.LEGACY_DOUBLE_PARSER); assertEquals(NO_MORE_DOCS, doubles.nextDoc()); // check that we cached nothing assertEquals(0, cache.getCacheEntries().length); ir.close(); dir.close(); }
Example #18
Source File: CachingNaiveBayesClassifierTest.java From lucene-solr with Apache License 2.0 | 5 votes |
@Test public void testBasicUsageWithQuery() throws Exception { LeafReader leafReader = null; try { MockAnalyzer analyzer = new MockAnalyzer(random()); leafReader = getSampleIndex(analyzer); TermQuery query = new TermQuery(new Term(textFieldName, "it")); checkCorrectClassification(new CachingNaiveBayesClassifier(leafReader, analyzer, query, categoryFieldName, textFieldName), TECHNOLOGY_INPUT, TECHNOLOGY_RESULT); } finally { if (leafReader != null) { leafReader.close(); } } }
Example #19
Source File: TestTermScorer.java From lucene-solr with Apache License 2.0 | 5 votes |
public void testDoesNotLoadNorms() throws IOException { Term allTerm = new Term(FIELD, "all"); TermQuery termQuery = new TermQuery(allTerm); LeafReader forbiddenNorms = new FilterLeafReader(indexReader) { @Override public NumericDocValues getNormValues(String field) throws IOException { fail("Norms should not be loaded"); // unreachable return null; } @Override public CacheHelper getCoreCacheHelper() { return in.getCoreCacheHelper(); } @Override public CacheHelper getReaderCacheHelper() { return in.getReaderCacheHelper(); } }; // We don't use newSearcher because it sometimes runs checkIndex which loads norms IndexSearcher indexSearcher = new IndexSearcher(forbiddenNorms); Weight weight = indexSearcher.createWeight(termQuery, ScoreMode.COMPLETE, 1); expectThrows(AssertionError.class, () -> { weight.scorer(forbiddenNorms.getContext()).iterator().nextDoc(); }); Weight weight2 = indexSearcher.createWeight(termQuery, ScoreMode.COMPLETE_NO_SCORES, 1); // should not fail this time since norms are not necessary weight2.scorer(forbiddenNorms.getContext()).iterator().nextDoc(); }
Example #20
Source File: DistanceValueSource.java From lucene-solr with Apache License 2.0 | 5 votes |
/** * Returns the FunctionValues used by the function query. */ @Override public DoubleValues getValues(LeafReaderContext readerContext, DoubleValues scores) throws IOException { LeafReader reader = readerContext.reader(); final NumericDocValues ptX = DocValues.getNumeric(reader, strategy.getFieldNameX()); final NumericDocValues ptY = DocValues.getNumeric(reader, strategy.getFieldNameY()); return DoubleValues.withDefault(new DoubleValues() { private final Point from = DistanceValueSource.this.from; private final DistanceCalculator calculator = strategy.getSpatialContext().getDistCalc(); @Override public double doubleValue() throws IOException { double x = Double.longBitsToDouble(ptX.longValue()); double y = Double.longBitsToDouble(ptY.longValue()); return calculator.distance(from, x, y) * multiplier; } @Override public boolean advanceExact(int doc) throws IOException { return ptX.advanceExact(doc) && ptY.advanceExact(doc); } }, nullValue); }
Example #21
Source File: AnalyzingInfixSuggester.java From lucene-solr with Apache License 2.0 | 5 votes |
@Override public long ramBytesUsed() { long mem = RamUsageEstimator.shallowSizeOf(this); try { if (searcherMgr != null) { SearcherManager mgr; IndexSearcher searcher; synchronized (searcherMgrLock) { mgr = searcherMgr; // acquire & release on same SearcherManager, via local reference searcher = mgr.acquire(); } try { for (LeafReaderContext context : searcher.getIndexReader().leaves()) { LeafReader reader = FilterLeafReader.unwrap(context.reader()); if (reader instanceof SegmentReader) { mem += ((SegmentReader) context.reader()).ramBytesUsed(); } } } finally { mgr.release(searcher); } } return mem; } catch (IOException ioe) { throw new RuntimeException(ioe); } }
Example #22
Source File: BooleanPerceptronClassifierTest.java From lucene-solr with Apache License 2.0 | 5 votes |
@Test public void testExplicitThreshold() throws Exception { LeafReader leafReader = null; try { MockAnalyzer analyzer = new MockAnalyzer(random()); leafReader = getSampleIndex(analyzer); BooleanPerceptronClassifier classifier = new BooleanPerceptronClassifier(leafReader, analyzer, null, 1, 50d, booleanFieldName, textFieldName); checkCorrectClassification(classifier, TECHNOLOGY_INPUT, false); checkCorrectClassification(classifier, POLITICS_INPUT, true); } finally { if (leafReader != null) { leafReader.close(); } } }
Example #23
Source File: Insanity.java From lucene-solr with Apache License 2.0 | 5 votes |
InsaneReader(LeafReader in, String insaneField) { super(in); this.insaneField = insaneField; ArrayList<FieldInfo> filteredInfos = new ArrayList<>(); for (FieldInfo fi : in.getFieldInfos()) { if (fi.name.equals(insaneField)) { filteredInfos.add(new FieldInfo(fi.name, fi.number, fi.hasVectors(), fi.omitsNorms(), fi.hasPayloads(), fi.getIndexOptions(), DocValuesType.NONE, -1, Collections.emptyMap(), fi.getPointDimensionCount(), fi.getPointIndexDimensionCount(), fi.getPointNumBytes(), fi.isSoftDeletesField())); } else { filteredInfos.add(fi); } } fieldInfos = new FieldInfos(filteredInfos.toArray(new FieldInfo[filteredInfos.size()])); }
Example #24
Source File: VersionsAndSeqNoResolver.java From crate with Apache License 2.0 | 5 votes |
public DocIdAndVersion(int docId, long version, long seqNo, long primaryTerm, LeafReader reader, int docBase) { this.docId = docId; this.version = version; this.seqNo = seqNo; this.primaryTerm = primaryTerm; this.reader = reader; this.docBase = docBase; }
Example #25
Source File: ArrayLengthQuery.java From crate with Apache License 2.0 | 5 votes |
NumTermsPerDocTwoPhaseIterator(LeafReader reader, IntUnaryOperator numTermsOfDoc, IntPredicate matches) { super(DocIdSetIterator.all(reader.maxDoc())); this.numTermsOfDoc = numTermsOfDoc; this.matches = matches; }
Example #26
Source File: IndexSearcherWrapper.java From crate with Apache License 2.0 | 5 votes |
private NonClosingReaderWrapper(DirectoryReader in) throws IOException { super(in, new SubReaderWrapper() { @Override public LeafReader wrap(LeafReader reader) { return reader; } }); }
Example #27
Source File: CodecCollector.java From mtas with Apache License 2.0 | 5 votes |
/** * Compute termvector number basic. * * @param termsEnum * the terms enum * @param r * the r * @return the termvector number basic * @throws IOException * Signals that an I/O exception has occurred. */ private static TermvectorNumberBasic computeTermvectorNumberBasic( TermsEnum termsEnum, LeafReader r) throws IOException { TermvectorNumberBasic result = new TermvectorNumberBasic(); boolean hasDeletedDocuments = (r.getLiveDocs() != null); if (!hasDeletedDocuments) { result.valueSum[0] = termsEnum.totalTermFreq(); result.docNumber = termsEnum.docFreq(); if (result.valueSum[0] > -1) { return result; } } throw new IOException("should not call this"); }
Example #28
Source File: TestSlowCompositeReaderWrapper.java From lucene-solr with Apache License 2.0 | 5 votes |
public void testCoreListenerOnSlowCompositeReaderWrapper() throws IOException { RandomIndexWriter w = new RandomIndexWriter(random(), newDirectory()); final int numDocs = TestUtil.nextInt(random(), 1, 5); for (int i = 0; i < numDocs; ++i) { w.addDocument(new Document()); if (random().nextBoolean()) { w.commit(); } } w.commit(); w.close(); final IndexReader reader = DirectoryReader.open(w.w.getDirectory()); final LeafReader leafReader = SlowCompositeReaderWrapper.wrap(reader); final int numListeners = TestUtil.nextInt(random(), 1, 10); final List<IndexReader.ClosedListener> listeners = new ArrayList<>(); AtomicInteger counter = new AtomicInteger(numListeners); for (int i = 0; i < numListeners; ++i) { CountCoreListener listener = new CountCoreListener(counter, leafReader.getCoreCacheHelper().getKey()); listeners.add(listener); leafReader.getCoreCacheHelper().addClosedListener(listener); } for (int i = 0; i < 100; ++i) { leafReader.getCoreCacheHelper().addClosedListener(listeners.get(random().nextInt(listeners.size()))); } assertEquals(numListeners, counter.get()); // make sure listeners are registered on the wrapped reader and that closing any of them has the same effect if (random().nextBoolean()) { reader.close(); } else { leafReader.close(); } assertEquals(0, counter.get()); w.w.getDirectory().close(); }
Example #29
Source File: BooleanPerceptronClassifierTest.java From lucene-solr with Apache License 2.0 | 5 votes |
@Test public void testBasicUsage() throws Exception { LeafReader leafReader = null; try { MockAnalyzer analyzer = new MockAnalyzer(random()); leafReader = getSampleIndex(analyzer); BooleanPerceptronClassifier classifier = new BooleanPerceptronClassifier(leafReader, analyzer, null, 1, null, booleanFieldName, textFieldName); checkCorrectClassification(classifier, TECHNOLOGY_INPUT, false); checkCorrectClassification(classifier, POLITICS_INPUT, true); } finally { if (leafReader != null) { leafReader.close(); } } }
Example #30
Source File: TermFilteredPresearcher.java From lucene-solr with Apache License 2.0 | 5 votes |
private Query buildFilterFields(LeafReader reader) throws IOException { BooleanQuery.Builder builder = new BooleanQuery.Builder(); for (String field : filterFields) { Query q = buildFilterClause(reader, field); if (q != null) { builder.add(q, BooleanClause.Occur.MUST); } } BooleanQuery bq = builder.build(); if (bq.clauses().size() == 0) { return null; } return bq; }