List of usage examples for org.apache.lucene.index IndexReader maxDoc
public abstract int maxDoc();
From source file:org.modeshape.search.lucene.query.NotQueryTest.java
License:Open Source License
@Test public void scorerShouldSkipAdjacentDocsIfScoredByOperandScorer() throws IOException { IndexReader reader = mock(IndexReader.class); when(reader.isDeleted(anyInt())).thenReturn(false); when(reader.maxDoc()).thenReturn(10); Scorer operandScorer = new MockScorer(0, 1, 2, 3, 4); Scorer notScorer = new NotQuery.NotScorer(operandScorer, reader, null); assertScores(notScorer, 5, 6, 7, 8, 9); }
From source file:org.modeshape.search.lucene.query.NotQueryTest.java
License:Open Source License
@Test public void scorerShouldSkipDocsAtEndIfScoredByOperandScorer() throws IOException { IndexReader reader = mock(IndexReader.class); when(reader.isDeleted(anyInt())).thenReturn(false); when(reader.maxDoc()).thenReturn(10); Scorer operandScorer = new MockScorer(8, 9); Scorer notScorer = new NotQuery.NotScorer(operandScorer, reader, null); assertScores(notScorer, 0, 1, 2, 3, 4, 5, 6, 7); }
From source file:org.modeshape.search.lucene.query.NotQueryTest.java
License:Open Source License
@Test public void scorerShouldScoreFirstDocsIfNotScoredByOperandScorer() throws IOException { IndexReader reader = mock(IndexReader.class); when(reader.isDeleted(anyInt())).thenReturn(false); when(reader.maxDoc()).thenReturn(10); Scorer operandScorer = new MockScorer(2, 3, 4); Scorer notScorer = new NotQuery.NotScorer(operandScorer, reader, null); assertScores(notScorer, 0, 1, 5, 6, 7, 8, 9); }
From source file:org.modeshape.search.lucene.query.NotQueryTest.java
License:Open Source License
@Test public void scorerShouldScoreNonAdjacentDocsNotScoredByOperandScorer() throws IOException { IndexReader reader = mock(IndexReader.class); when(reader.isDeleted(anyInt())).thenReturn(false); when(reader.maxDoc()).thenReturn(10); Scorer operandScorer = new MockScorer(2, 4, 8); Scorer notScorer = new NotQuery.NotScorer(operandScorer, reader, null); assertScores(notScorer, 0, 1, 3, 5, 6, 7, 9); }
From source file:org.netbeans.modules.jackpot30.backend.impl.spi.StatisticsGenerator.java
License:Open Source License
public static Map<String, Long> generateStatistics(IndexReader r) throws IOException { statistics = new HashMap<String, Long>(); Collection<? extends StatisticsGenerator> generators = Lookup.getDefault() .lookupAll(StatisticsGenerator.class); int maxDocs = r.maxDoc(); for (int d = 0; d < maxDocs; d++) { Document doc = r.document(d, new FieldSelector() { @Override/*from w ww. ja v a 2 s . c o m*/ public FieldSelectorResult accept(String string) { return FieldSelectorResult.LAZY_LOAD; } }); for (StatisticsGenerator sg : generators) { sg.amendStatistics(r, doc); } } Map<String, Long> result = statistics; statistics = null; return result; }
From source file:org.netbeans.modules.jackpot30.ide.usages.hints.RemotelyUnused.java
License:Open Source License
@TriggerTreeKind({ Kind.VARIABLE, Kind.METHOD }) public static ErrorDescription hint(HintContext ctx) throws URISyntaxException, IOException { Element toSearch = ctx.getInfo().getTrees().getElement(ctx.getPath()); if (toSearch == null) return null; if (!toSearch.getKind().isField() && toSearch.getKind() != ElementKind.METHOD && toSearch.getKind() != ElementKind.CONSTRUCTOR) return null; if (toSearch.getKind() == ElementKind.METHOD && ctx.getInfo().getElementUtilities().overridesMethod((ExecutableElement) toSearch)) return null; final String serialized = JavaUtils.serialize(ElementHandle.create(toSearch)); for (RemoteIndex idx : RemoteIndex.loadIndices()) { String result = LocalCache.runOverLocalCache(idx, new Task<IndexReader, String>() { @Override/*from w w w. j a v a2 s. c o m*/ public String run(IndexReader reader, AtomicBoolean cancel) throws IOException { Query query = new TermQuery(new Term("usagesSignature", serialized)); Searcher s = new IndexSearcher(reader); BitSet matchingDocuments = new BitSet(reader.maxDoc()); Collector c = new BitSetCollector(matchingDocuments); s.search(query, c); for (int docNum = matchingDocuments.nextSetBit(0); docNum >= 0; docNum = matchingDocuments .nextSetBit(docNum + 1)) { if (cancel.get()) return VAL_UNKNOWN; final Document doc = reader.document(docNum); return doc.get("usagesUsages"); } return VAL_UNKNOWN; } }, null, new AtomicBoolean()/*XXX*/); if (result == null) { URI resolved = new URI(idx.remote.toExternalForm() + "/usages/search?path=" + WebUtilities.escapeForQuery(idx.remoteSegment) + "&signatures=" + WebUtilities.escapeForQuery(serialized)); String response = WebUtilities.requestStringResponse(resolved, new AtomicBoolean()); if (response != null) { result = response.trim().isEmpty() ? VAL_UNUSED : VAL_USED; } else { result = VAL_UNKNOWN; } final String resultFin = result; LocalCache.saveToLocalCache(idx, new Task<IndexWriter, Void>() { @Override public Void run(IndexWriter p, AtomicBoolean cancel) throws IOException { Document doc = new Document(); doc.add(new Field("usagesSignature", serialized, Store.NO, Index.NOT_ANALYZED)); doc.add(new Field("usagesUsages", resultFin, Store.YES, Index.NO)); p.addDocument(doc); return null; } }); } if (!VAL_UNUSED.equals(result)) return null; } return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.ERR_NoUsages()); }
From source file:org.netbeans.modules.jackpot30.impl.duplicates.indexing.RemoteDuplicatesIndex.java
License:Open Source License
private static Map<String, Collection<? extends String>> containsHash(IndexReader reader, Iterable<? extends String> hashes, AtomicBoolean cancel) throws IOException { Map<String, Collection<? extends String>> result = new LinkedHashMap<String, Collection<? extends String>>(); for (String hash : hashes) { if (cancel.get()) return Collections.emptyMap(); Collection<String> found = new LinkedList<String>(); Query query = new TermQuery(new Term("hash", hash)); Searcher s = new IndexSearcher(reader); BitSet matchingDocuments = new BitSet(reader.maxDoc()); Collector c = new BitSetCollector(matchingDocuments); s.search(query, c);// w w w. j a va2 s . c om boolean wasFound = false; for (int docNum = matchingDocuments.nextSetBit(0); docNum >= 0; docNum = matchingDocuments .nextSetBit(docNum + 1)) { if (cancel.get()) return Collections.emptyMap(); final Document doc = reader.document(docNum); found.addAll(Arrays.asList(doc.getValues("path"))); wasFound = true; } if (wasFound) { result.put(hash, found); } } return result; }
From source file:org.netbeans.modules.jackpot30.indexer.usages.IndexerImplTest.java
License:Open Source License
public void testSubdirIndexing() throws IOException { final FileObject root = FileUtil.toFileObject(getWorkDir()); FileObject aFile = FileUtil.createData(root, "a/A.java"); copyToFile(aFile, "public class A {}"); FileObject bFile = FileUtil.createData(root, "b/B.java"); copyToFile(bFile, "public class B {}"); Directory store = new RAMDirectory(); IndexWriter iw = new IndexWriter(store, new KeywordAnalyzer(), IndexWriter.MaxFieldLength.UNLIMITED); IndexAccessor.current = new IndexAccessor(iw, root.getFileObject("a")); doIndex(root, aFile, bFile);//from ww w. jav a 2s . c o m iw.close(); IndexReader ir = IndexReader.open(store); int maxDocs = ir.maxDoc(); boolean foundA = false; for (int i = 0; i < maxDocs; i++) { Fieldable f = ir.document(i).getFieldable("file"); if (f != null) { assertFalse(f.stringValue(), f.stringValue().contains("B")); if (f.stringValue().contains("A.java")) { foundA = true; } } } assertTrue(foundA); }
From source file:org.openrdf.sail.lucene.LuceneIndex.java
License:BSD License
private void logIndexStats() { try {/*ww w. java 2 s .co m*/ IndexReader reader = null; try { reader = getIndexReader(); Document doc; int totalFields = 0; Set<String> ids = new HashSet<String>(); String[] idArray; int count = 0; for (int i = 0; i < reader.maxDoc(); i++) { if (reader.isDeleted(i)) continue; doc = reader.document(i); totalFields += doc.getFields().size(); count++; idArray = doc.getValues("id"); for (String id : idArray) ids.add(id); } logger.info("Total documents in the index: " + reader.numDocs() + ", number of deletable documents in the index: " + reader.numDeletedDocs() + ", valid documents: " + count + ", total fields in all documents: " + totalFields + ", average number of fields per document: " + ((double) totalFields) / reader.numDocs()); logger.info("Distinct ids in the index: " + ids.size()); } finally { if (currentMonitor != null) { currentMonitor.closeWhenPossible(); currentMonitor = null; } } } catch (IOException e) { logger.warn(e.getMessage(), e); } }
From source file:org.openrdf.sail.lucene3.LuceneIndex.java
License:BSD License
private void logIndexStats() { try {/* w w w . ja v a2 s . c o m*/ IndexReader reader = null; try { reader = getIndexReader(); Document doc; int totalFields = 0; Set<String> ids = new HashSet<String>(); String[] idArray; int count = 0; for (int i = 0; i < reader.maxDoc(); i++) { if (isDeleted(reader, i)) continue; doc = readDocument(reader, i, null); totalFields += doc.getFields().size(); count++; idArray = doc.getValues("id"); for (String id : idArray) ids.add(id); } logger.info("Total documents in the index: " + reader.numDocs() + ", number of deletable documents in the index: " + reader.numDeletedDocs() + ", valid documents: " + count + ", total fields in all documents: " + totalFields + ", average number of fields per document: " + ((double) totalFields) / reader.numDocs()); logger.info("Distinct ids in the index: " + ids.size()); } finally { if (currentMonitor != null) { currentMonitor.closeWhenPossible(); currentMonitor = null; } } } catch (IOException e) { logger.warn(e.getMessage(), e); } }