Example usage for org.apache.lucene.index IndexReader maxDoc

List of usage examples for org.apache.lucene.index IndexReader maxDoc

Introduction

In this page you can find the example usage for org.apache.lucene.index IndexReader maxDoc.

Prototype

public abstract int maxDoc();

Source Link

Document

Returns one greater than the largest possible document number.

Usage

From source file:org.opentravel.schemacompiler.index.QueryTask.java

License:Apache License

protected void displayIndex() {
    IndexSearcher searcher = null;//from  w  w  w.  java 2  s  . co  m
    try {
        searcher = searchManager.acquire();
        IndexReader reader = searcher.getIndexReader();

        for (int i = 0; i < reader.maxDoc(); i++) {
            Document doc = reader.document(i);

            System.out.println("DOCUMENT: " + doc.get(IDENTITY_FIELD));
            System.out.println("  " + BASE_NAMESPACE_FIELD + " : " + doc.get(BASE_NAMESPACE_FIELD));
            System.out.println("  " + FILENAME_FIELD + " : " + doc.get(FILENAME_FIELD));
            System.out.println("  " + STATUS_FIELD + " : " + doc.get(STATUS_FIELD));
            System.out.println("  " + VERSION_FIELD + " : " + doc.get(VERSION_FIELD));
            System.out.println("  " + VERSION_TYPE_FIELD + " : " + doc.get(VERSION_TYPE_FIELD));
        }
    } catch (Throwable t) {
        t.printStackTrace(System.out);

    } finally {
        try {
            if (searcher != null)
                searchManager.release(searcher);

        } catch (IOException e) {
            // Ignore error and continue
        }
    }
}

From source file:org.punksearch.searcher.filters.FloatRangeFilter.java

License:Open Source License

@Override
public DocIdSet getDocIdSet(IndexReader reader) throws IOException {
    BitSet bits = new BitSet(reader.maxDoc());
    DocIdBitSet docIdBitSet = new DocIdBitSet(bits);
    TermEnum enumerator = reader.terms(new Term(fieldName, ""));

    try {/*from   www . j a va 2s. com*/

        if (enumerator.term() == null) {
            return docIdBitSet;
        }

        TermDocs termDocs = reader.termDocs();
        try {
            do {
                Term term = enumerator.term();
                if (term != null && term.field().equals(fieldName)) {
                    Float termFloat = Float.valueOf(term.text());
                    if (lowerTerm == null || lowerTerm < termFloat
                            || (includeLower && lowerTerm == termFloat)) {
                        if (upperTerm != null
                                && (upperTerm < termFloat || (!includeUpper && upperTerm == termFloat))) {
                            continue;
                        }
                        // we have a good term, find the docs
                        termDocs.seek(enumerator.term());
                        while (termDocs.next()) {
                            bits.set(termDocs.doc());
                        }

                    }
                } else {
                    break;
                }
            } while (enumerator.next());

        } finally {
            termDocs.close();
        }
    } finally {
        enumerator.close();
    }

    System.out.println(lowerTerm + " " + upperTerm);

    return docIdBitSet;
}

From source file:org.punksearch.searcher.filters.NumberRangeFilter.java

License:Open Source License

@Override
public DocIdSet getDocIdSet(IndexReader reader) throws IOException {
    BitSet bits = new BitSet(reader.maxDoc());
    DocIdBitSet docIdBitSet = new DocIdBitSet(bits);
    TermEnum enumerator = reader.terms(new Term(fieldName, ""));

    try {//from  w w  w.j a v a 2 s  .c  om

        if (enumerator.term() == null) {
            return docIdBitSet;
        }

        TermDocs termDocs = reader.termDocs();
        try {
            do {
                Term term = enumerator.term();
                if (term != null && term.field().equals(fieldName)) {
                    N termValue = termTextToNumber(term.text());
                    if (lowerTerm == null || lowerTerm.compareTo(termValue) < 0
                            || (includeLower && lowerTerm.compareTo(termValue) == 0)) {
                        if (upperTerm == null || upperTerm.compareTo(termValue) > 0
                                || (includeUpper && upperTerm.compareTo(termValue) == 0)) {
                            // we have a good term, find the docs
                            termDocs.seek(enumerator.term());
                            while (termDocs.next()) {
                                bits.set(termDocs.doc());
                            }
                        }
                    }
                } else {
                    break;
                }
            } while (enumerator.next());

        } finally {
            termDocs.close();
        }
    } finally {
        enumerator.close();
    }

    return docIdBitSet;
}

From source file:org.sindice.siren.search.SirenMultiTermQueryWrapperFilter.java

License:Apache License

/**
 * Returns a DocIdSet with documents that should be
 * permitted in search results.//from w  ww . ja  va 2s .c  o m
 */
@Override
public DocIdSet getDocIdSet(IndexReader reader) throws IOException {
    final TermEnum enumerator = query.getEnum(reader);
    try {
        // if current term in enum is null, the enum is empty -> shortcut
        if (enumerator.term() == null)
            return DocIdSet.EMPTY_DOCIDSET;
        // else fill into a OpenBitSet
        final OpenBitSet bitSet = new OpenBitSet(reader.maxDoc());
        final int[] docs = new int[32];
        final int[] freqs = new int[32];
        TermDocs termDocs = reader.termDocs();
        try {
            int termCount = 0;
            do {
                Term term = enumerator.term();
                if (term == null)
                    break;
                termCount++;
                termDocs.seek(term);
                while (true) {
                    final int count = termDocs.read(docs, freqs);
                    if (count != 0) {
                        for (int i = 0; i < count; i++) {
                            int index = docs[i];
                            //                System.out.println("BITSET: " + index);
                            bitSet.set(index);
                        }
                    } else {
                        break;
                    }
                }
            } while (enumerator.next());

            query.incTotalNumberOfTerms(termCount);

        } finally {
            termDocs.close();
        }
        return bitSet;
    } finally {
        enumerator.close();
    }
}

From source file:org.sonatype.nexus.index.context.DefaultIndexingContext.java

License:Open Source License

public void merge(Directory directory, DocumentFilter filter) throws IOException {
    synchronized (indexLock) {
        closeReaders();/*from  w w  w .  ja  va2  s . c om*/

        IndexWriter w = getIndexWriter();

        IndexSearcher s = getIndexSearcher();

        IndexReader r = IndexReader.open(directory);

        try {
            int numDocs = r.maxDoc();

            for (int i = 0; i < numDocs; i++) {
                if (r.isDeleted(i)) {
                    continue;
                }

                Document d = r.document(i);

                if (filter != null && !filter.accept(d)) {
                    continue;
                }

                String uinfo = d.get(ArtifactInfo.UINFO);

                if (uinfo != null) {
                    Hits hits = s.search(new TermQuery(new Term(ArtifactInfo.UINFO, uinfo)));

                    if (hits.length() == 0) {
                        w.addDocument(IndexUtils.updateDocument(d, this, false));
                    }
                } else {
                    String deleted = d.get(ArtifactInfo.DELETED);

                    if (deleted != null) {
                        // Deleting the document loses history that it was delete,
                        // so incrementals wont work. Therefore, put the delete
                        // document in as well
                        w.deleteDocuments(new Term(ArtifactInfo.UINFO, deleted));
                        w.addDocument(d);
                    }
                }
            }

        } finally {
            r.close();
            closeReaders();
        }

        rebuildGroups();

        Date mergedTimestamp = IndexUtils.getTimestamp(directory);

        if (getTimestamp() != null && mergedTimestamp != null && mergedTimestamp.after(getTimestamp())) {
            // we have both, keep the newest
            updateTimestamp(true, mergedTimestamp);
        } else {
            updateTimestamp(true);
        }

        optimize();
    }
}

From source file:org.sonatype.nexus.index.context.IndexUtils.java

License:Open Source License

/**
 * Used to rebuild group information, for example on context which were merged, since merge() of contexts 
 * only merges the Documents with UINFO record (Artifacts).
 *///from w w w .  j  a  v  a  2s . c o  m
public static void rebuildGroups(IndexingContext context) throws IOException {
    IndexReader r = context.getIndexReader();

    Set<String> rootGroups = new LinkedHashSet<String>();
    Set<String> allGroups = new LinkedHashSet<String>();

    int numDocs = r.maxDoc();

    for (int i = 0; i < numDocs; i++) {
        if (r.isDeleted(i)) {
            continue;
        }

        Document d = r.document(i);

        String uinfo = d.get(ArtifactInfo.UINFO);

        if (uinfo != null) {
            ArtifactInfo info = IndexUtils.constructArtifactInfo(d, context);
            rootGroups.add(info.getRootGroup());
            allGroups.add(info.groupId);
        }
    }

    setRootGroups(context, rootGroups);
    setAllGroups(context, allGroups);

    context.getIndexWriter().optimize();
    context.getIndexWriter().flush();
}

From source file:org.sonatype.nexus.index.Nexus5393IndexEntryDuplicationIT.java

License:Open Source License

protected void ensureUniqueness() throws IOException {
    final IndexingContext context = indexerManager.getRepositoryIndexContext("central");
    final HashSet<String> uinfos = new HashSet<String>();
    final ArrayList<String> duplicates = new ArrayList<String>();
    final IndexSearcher indexSearcher = context.acquireIndexSearcher();
    try {//  w  w w  .  j av  a2s . c o  m
        final IndexReader r = indexSearcher.getIndexReader();
        for (int i = 0; i < r.maxDoc(); i++) {
            if (!r.isDeleted(i)) {
                final Document d = r.document(i);
                String uinfo = d.get(ArtifactInfo.UINFO);
                if (uinfo != null && !uinfos.add(uinfo)) {
                    duplicates.add(uinfo);
                }
            }
        }
    } finally {
        context.releaseIndexSearcher(indexSearcher);
    }

    // remote proxy contains only one artifact: log4j-1.2.13: so we expect out index to have no
    // dupes and only one artifact
    if (!duplicates.isEmpty() || uinfos.size() > 1) {
        Assert.fail("UINFOs are duplicated or we scanned some unexpected ones, duplicates=" + duplicates
                + ", uinfos=" + uinfos);
    }
}

From source file:org.sonatype.nexus.index.Nexus5393IndexEntryDuplicationLocalTest.java

License:Open Source License

protected void ensureUniqueness() throws IOException {
    final IndexingContext context = indexerManager.getRepositoryIndexContext("releases");
    final HashSet<String> uinfos = new HashSet<String>();
    final ArrayList<String> duplicates = new ArrayList<String>();
    final IndexSearcher indexSearcher = context.acquireIndexSearcher();
    try {/*from   w  w  w .j  a v a 2s. com*/
        final IndexReader r = indexSearcher.getIndexReader();
        for (int i = 0; i < r.maxDoc(); i++) {
            if (!r.isDeleted(i)) {
                final Document d = r.document(i);
                String uinfo = d.get(ArtifactInfo.UINFO);
                if (uinfo != null && !uinfos.add(uinfo)) {
                    duplicates.add(uinfo);
                }
            }
        }
    } finally {
        context.releaseIndexSearcher(indexSearcher);
    }

    // remote proxy contains only one artifact: log4j-1.2.13: so we expect out index to have no
    // dupes and only one artifact
    if (!duplicates.isEmpty() || uinfos.size() > 1) {
        Assert.fail("UINFOs are duplicated or we scanned some unexpected ones, duplicates=" + duplicates
                + ", uinfos=" + uinfos);
    }
}

From source file:org.sonatype.nexus.index.NexusScanningListener.java

License:Open Source License

/**
 * Used in {@code update} mode, deletes documents from index that are not found during scanning (means
 * they were deleted from the storage being scanned).
 *
 * @param contextPath/*  w  ww. j  a  v a 2  s .co  m*/
 * @return
 * @throws IOException
 */
private int removeDeletedArtifacts(final String contextPath) throws IOException {
    int deleted = 0;
    final IndexReader r = contextIndexSearcher.getIndexReader();
    for (int i = 0; i < r.maxDoc(); i++) {
        if (!r.isDeleted(i)) {
            final Document d = r.document(i);
            final String uinfo = d.get(ArtifactInfo.UINFO);
            if (uinfo != null && !processedUinfos.contains(uinfo)) {
                // file is not present in storage but is on index, delete it from index
                final String[] ra = ArtifactInfo.FS_PATTERN.split(uinfo);
                final ArtifactInfo ai = new ArtifactInfo();
                ai.repository = context.getRepositoryId();
                ai.groupId = ra[0];
                ai.artifactId = ra[1];
                ai.version = ra[2];
                if (ra.length > 3) {
                    ai.classifier = ArtifactInfo.renvl(ra[3]);
                }
                if (ra.length > 4) {
                    ai.packaging = ArtifactInfo.renvl(ra[4]);
                }

                // minimal ArtifactContext for removal
                final ArtifactContext ac = new ArtifactContext(null, null, null, ai, ai.calculateGav());
                if (contextPath == null
                        || context.getGavCalculator().gavToPath(ac.getGav()).startsWith(contextPath)) {
                    if (IndexOp.DELETED == remove(ac)) {
                        deleted++;
                    }
                }
            }
        }
    }
    return deleted;
}

From source file:org.sonatype.nexus.index.packer.DefaultIndexPacker.java

License:Open Source License

static void copyLegacyDocuments(IndexReader r, Directory targetdir, IndexingContext context)
        throws CorruptIndexException, LockObtainFailedException, IOException {
    IndexWriter w = null;//from   w ww . j ava 2s. com
    try {
        w = new IndexWriter(targetdir, false, new NexusLegacyAnalyzer(), true);

        for (int i = 0; i < r.maxDoc(); i++) {
            if (!r.isDeleted(i)) {
                w.addDocument(updateLegacyDocument(r.document(i), context));
            }
        }

        w.optimize();
        w.flush();
    } finally {
        IndexUtils.close(w);
    }
}