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

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

Introduction

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

Prototype

public abstract int numDocs();

Source Link

Document

Returns the number of documents in this index.

Usage

From source file:org.elasticsearch.search.aggregations.bucket.significant.GlobalOrdinalsSignificantTermsAggregator.java

License:Apache License

@Override
public SignificantStringTerms buildEmptyAggregation() {
    // We need to account for the significance of a miss in our global stats - provide corpus size as context
    ContextIndexSearcher searcher = context.searchContext().searcher();
    IndexReader topReader = searcher.getIndexReader();
    int supersetSize = topReader.numDocs();
    return new SignificantStringTerms(0, supersetSize, name, bucketCountThresholds.getRequiredSize(),
            bucketCountThresholds.getMinDocCount(), termsAggFactory.getSignificanceHeuristic(),
            Collections.<InternalSignificantTerms.Bucket>emptyList(), pipelineAggregators(), metaData());
}

From source file:org.elasticsearch.search.aggregations.bucket.significant.SignificantLongTermsAggregator.java

License:Apache License

@Override
public SignificantLongTerms buildEmptyAggregation() {
    // We need to account for the significance of a miss in our global stats - provide corpus size as context
    ContextIndexSearcher searcher = context.searchContext().searcher();
    IndexReader topReader = searcher.getIndexReader();
    int supersetSize = topReader.numDocs();
    return new SignificantLongTerms(0, supersetSize, name, formatter, bucketCountThresholds.getRequiredSize(),
            bucketCountThresholds.getMinDocCount(), termsAggFactory.getSignificanceHeuristic(),
            Collections.<InternalSignificantTerms.Bucket>emptyList(), pipelineAggregators(), metaData());
}

From source file:org.elasticsearch.search.aggregations.bucket.significant.SignificantTextAggregator.java

License:Apache License

@Override
public SignificantStringTerms buildEmptyAggregation() {
    // We need to account for the significance of a miss in our global stats - provide corpus size as context
    ContextIndexSearcher searcher = context.searcher();
    IndexReader topReader = searcher.getIndexReader();
    int supersetSize = topReader.numDocs();
    return new SignificantStringTerms(name, bucketCountThresholds.getRequiredSize(),
            bucketCountThresholds.getMinDocCount(), pipelineAggregators(), metaData(), format, 0, supersetSize,
            significanceHeuristic, emptyList());
}

From source file:org.elasticsearch.search.query.TopDocsCollectorContext.java

License:Apache License

/**
 * Returns query total hit count if the <code>query</code> is a {@link MatchAllDocsQuery}
 * or a {@link TermQuery} and the <code>reader</code> has no deletions,
 * -1 otherwise./*from   ww  w.j  a v  a  2 s.  c  o  m*/
 */
static int shortcutTotalHitCount(IndexReader reader, Query query) throws IOException {
    while (true) {
        // remove wrappers that don't matter for counts
        // this is necessary so that we don't only optimize match_all
        // queries but also match_all queries that are nested in
        // a constant_score query
        if (query instanceof ConstantScoreQuery) {
            query = ((ConstantScoreQuery) query).getQuery();
        } else if (query instanceof BoostQuery) {
            query = ((BoostQuery) query).getQuery();
        } else {
            break;
        }
    }
    if (query.getClass() == MatchAllDocsQuery.class) {
        return reader.numDocs();
    } else if (query.getClass() == TermQuery.class && reader.hasDeletions() == false) {
        final Term term = ((TermQuery) query).getTerm();
        int count = 0;
        for (LeafReaderContext context : reader.leaves()) {
            count += context.reader().docFreq(term);
        }
        return count;
    } else {
        return -1;
    }
}

From source file:org.elasticsearch.search.query.TopDocsCollectorContext.java

License:Apache License

/**
 * Creates a {@link TopDocsCollectorContext} from the provided <code>searchContext</code>
 *//*from   w w w. j  a  v  a 2  s  .co m*/
static TopDocsCollectorContext createTopDocsCollectorContext(SearchContext searchContext, IndexReader reader,
        boolean shouldCollect) throws IOException {
    final Query query = searchContext.query();
    // top collectors don't like a size of 0
    final int totalNumDocs = Math.max(1, reader.numDocs());
    if (searchContext.size() == 0) {
        // no matter what the value of from is
        return new TotalHitCountCollectorContext(reader, query, shouldCollect);
    } else if (searchContext.scrollContext() != null) {
        // no matter what the value of from is
        int numDocs = Math.min(searchContext.size(), totalNumDocs);
        return new ScrollingTopDocsCollectorContext(searchContext.scrollContext(), searchContext.sort(),
                numDocs, searchContext.trackScores(), searchContext.numberOfShards());
    } else if (searchContext.collapse() != null) {
        int numDocs = Math.min(searchContext.from() + searchContext.size(), totalNumDocs);
        return new CollapsingTopDocsCollectorContext(searchContext.collapse(), searchContext.sort(), numDocs,
                searchContext.trackScores());
    } else {
        int numDocs = Math.min(searchContext.from() + searchContext.size(), totalNumDocs);
        final boolean rescore = searchContext.rescore().isEmpty() == false;
        if (rescore) {
            assert searchContext.sort() == null;
            for (RescoreContext rescoreContext : searchContext.rescore()) {
                numDocs = Math.max(numDocs, rescoreContext.getWindowSize());
            }
        }
        return new SimpleTopDocsCollectorContext(searchContext.sort(), searchContext.searchAfter(), numDocs,
                searchContext.trackScores()) {
            @Override
            boolean shouldRescore() {
                return rescore;
            }
        };
    }
}

From source file:org.elasticsearch.search.suggest.Suggester.java

License:Apache License

public Suggest.Suggestion<? extends Suggest.Suggestion.Entry<? extends Suggest.Suggestion.Entry.Option>> execute(
        String name, T suggestion, IndexReader indexReader, CharsRef spare) throws IOException {
    // #3469 We want to ignore empty shards
    if (indexReader.numDocs() == 0) {
        return null;
    }//from www  .j  a v  a 2  s.  com
    return innerExecute(name, suggestion, indexReader, spare);
}

From source file:org.elbe.relations.indexer.lucene.LuceneIndexer.java

License:Open Source License

@Override
public int numberOfIndexed(final File inIndexDir) throws IOException {
    int outNumber = 0;
    final IndexReader lReader = IndexReader.open(FSDirectory.getDirectory(inIndexDir));
    try {//  w w w  .j  a  va  2 s  .c o m
        outNumber = lReader.numDocs();
    } finally {
        lReader.close();
    }
    return outNumber;
}

From source file:org.exoplatform.services.jcr.impl.core.query.lucene.IndexFormatVersion.java

License:Apache License

/**
 * @return the index format version of the index used by the given
 * index reader./*from w w w . j  a v a  2s  .  c  o m*/
 */
public static IndexFormatVersion getVersion(IndexReader indexReader) {
    Collection fields = indexReader.getFieldNames(IndexReader.FieldOption.ALL);
    if ((fields.contains(FieldNames.INDEX) && fields.contains(FieldNames.PATH)) || indexReader.numDocs() == 0) {
        return IndexFormatVersion.V4;
    } else if (fields.contains(FieldNames.LOCAL_NAME)) {
        return IndexFormatVersion.V3;
    } else if (fields.contains(FieldNames.PROPERTIES_SET)) {
        return IndexFormatVersion.V2;
    } else {
        return IndexFormatVersion.V1;
    }
}

From source file:org.frontcache.cache.impl.LuceneIndexManager.java

License:Apache License

/**
 * //  www  .j a  v a2 s .c  o  m
 * @return
 */
public List<String> getKeys() {

    IndexWriter iWriter = null;
    try {
        iWriter = getIndexWriter();
        if (iWriter == null) {
            return Collections.emptyList();
        }
    } catch (Exception e1) {
        logger.debug("Error during getting indexWriter. " + e1.getMessage());
        return Collections.emptyList();
    }

    List<String> keys = new ArrayList<String>();

    IndexReader reader = null;
    try {
        reader = DirectoryReader.open(iWriter);

        for (int i = 0; i < reader.numDocs(); i++) {
            Document doc = reader.document(i);
            if (null != doc) {
                if (null != doc.get(URL_FIELD))
                    keys.add(doc.get(URL_FIELD));
                else
                    logger.error("URL is null for doc (probably corrupted after/during index time) " + doc);
            }

        }
    } catch (Exception e) {
        logger.error("Error during loading urls/keys from index", e);
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    return keys;
}

From source file:org.frontcache.cache.impl.LuceneIndexManager.java

License:Apache License

/**
 * Returns index size//  ww w .ja  v a  2 s. co  m
 * @return
 */
public int getIndexSize() {
    int n = -1;
    IndexReader reader = null;
    try {

        Directory dir = FSDirectory.open(Paths.get(INDEX_PATH));
        reader = DirectoryReader.open(dir);
        n = reader.numDocs();

    } catch (IOException e) {

        logger.error(e.getMessage(), e);
    } finally {

        if (null != reader) {
            try {
                reader.close();
            } catch (IOException e) {
                logger.error(e.getMessage(), e);
            }
        }
    }

    return n;
}