Search in sources :

Example 81 with LeafReader

use of org.apache.lucene.index.LeafReader in project lucene-solr by apache.

the class DocSetUtil method createDocSet.

public static DocSet createDocSet(SolrIndexSearcher searcher, Term term) throws IOException {
    // raw reader to avoid extra wrapping overhead
    DirectoryReader reader = searcher.getRawReader();
    int maxDoc = searcher.getIndexReader().maxDoc();
    int smallSetSize = smallSetSize(maxDoc);
    String field = term.field();
    BytesRef termVal = term.bytes();
    int maxCount = 0;
    int firstReader = -1;
    List<LeafReaderContext> leaves = reader.leaves();
    // use array for slightly higher scanning cost, but fewer memory allocations
    PostingsEnum[] postList = new PostingsEnum[leaves.size()];
    for (LeafReaderContext ctx : leaves) {
        assert leaves.get(ctx.ord) == ctx;
        LeafReader r = ctx.reader();
        Fields f = r.fields();
        Terms t = f.terms(field);
        // field is missing
        if (t == null)
            continue;
        TermsEnum te = t.iterator();
        if (te.seekExact(termVal)) {
            maxCount += te.docFreq();
            postList[ctx.ord] = te.postings(null, PostingsEnum.NONE);
            if (firstReader < 0)
                firstReader = ctx.ord;
        }
    }
    DocSet answer = null;
    if (maxCount == 0) {
        answer = DocSet.EMPTY;
    } else if (maxCount <= smallSetSize) {
        answer = createSmallSet(leaves, postList, maxCount, firstReader);
    } else {
        answer = createBigSet(leaves, postList, maxDoc, firstReader);
    }
    return DocSetUtil.getDocSet(answer, searcher);
}
Also used : LeafReader(org.apache.lucene.index.LeafReader) DirectoryReader(org.apache.lucene.index.DirectoryReader) Terms(org.apache.lucene.index.Terms) TermsEnum(org.apache.lucene.index.TermsEnum) Fields(org.apache.lucene.index.Fields) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) PostingsEnum(org.apache.lucene.index.PostingsEnum) BytesRef(org.apache.lucene.util.BytesRef)

Example 82 with LeafReader

use of org.apache.lucene.index.LeafReader in project lucene-solr by apache.

the class VersionInfo method getMaxVersionFromIndex.

/**
   * Returns the highest version from the index, or 0L if no versions can be found in the index.
   */
public Long getMaxVersionFromIndex(IndexSearcher searcher) throws IOException {
    String versionFieldName = versionField.getName();
    log.debug("Refreshing highest value of {} for {} version buckets from index", versionFieldName, buckets.length);
    long maxVersionInIndex = 0L;
    // if indexed, then we have terms to get the max from
    if (versionField.indexed()) {
        LeafReader leafReader = SlowCompositeReaderWrapper.wrap(searcher.getIndexReader());
        Terms versionTerms = leafReader.terms(versionFieldName);
        Long max = (versionTerms != null) ? LegacyNumericUtils.getMaxLong(versionTerms) : null;
        if (max != null) {
            maxVersionInIndex = max.longValue();
            log.debug("Found MAX value {} from Terms for {} in index", maxVersionInIndex, versionFieldName);
        } else {
            log.debug("No terms found for {}, cannot seed version bucket highest value from index", versionFieldName);
        }
    } else {
        ValueSource vs = versionField.getType().getValueSource(versionField, null);
        Map funcContext = ValueSource.newContext(searcher);
        vs.createWeight(funcContext, searcher);
        // TODO: multi-thread this
        for (LeafReaderContext ctx : searcher.getTopReaderContext().leaves()) {
            int maxDoc = ctx.reader().maxDoc();
            FunctionValues fv = vs.getValues(funcContext, ctx);
            for (int doc = 0; doc < maxDoc; doc++) {
                long v = fv.longVal(doc);
                maxVersionInIndex = Math.max(v, maxVersionInIndex);
            }
        }
    }
    return maxVersionInIndex;
}
Also used : LeafReader(org.apache.lucene.index.LeafReader) ValueSource(org.apache.lucene.queries.function.ValueSource) Terms(org.apache.lucene.index.Terms) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) FunctionValues(org.apache.lucene.queries.function.FunctionValues) Map(java.util.Map)

Example 83 with LeafReader

use of org.apache.lucene.index.LeafReader in project crate by crate.

the class LuceneBatchIterator method innerMoveNext.

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, reader);
            return true;
        }
        currentDocIdSetIt = null;
    }
    clearState();
    return false;
}
Also used : LeafReader(org.apache.lucene.index.LeafReader) Bits(org.apache.lucene.util.Bits)

Example 84 with LeafReader

use of org.apache.lucene.index.LeafReader in project elasticsearch by elastic.

the class ShardCoreKeyMap method add.

/**
     * Register a {@link LeafReader}. This is necessary so that the core cache
     * key of this reader can be found later using {@link #getCoreKeysForIndex(String)}.
     */
public void add(LeafReader reader) {
    final ShardId shardId = ShardUtils.extractShardId(reader);
    if (shardId == null) {
        throw new IllegalArgumentException("Could not extract shard id from " + reader);
    }
    final Object coreKey = reader.getCoreCacheKey();
    if (coreKeyToShard.containsKey(coreKey)) {
        // the time).
        return;
    }
    final String index = shardId.getIndexName();
    synchronized (this) {
        if (coreKeyToShard.containsKey(coreKey) == false) {
            Set<Object> objects = indexToCoreKey.get(index);
            if (objects == null) {
                objects = new HashSet<>();
                indexToCoreKey.put(index, objects);
            }
            final boolean added = objects.add(coreKey);
            assert added;
            CoreClosedListener listener = ownerCoreCacheKey -> {
                assert coreKey == ownerCoreCacheKey;
                synchronized (ShardCoreKeyMap.this) {
                    coreKeyToShard.remove(ownerCoreCacheKey);
                    final Set<Object> coreKeys = indexToCoreKey.get(index);
                    final boolean removed = coreKeys.remove(coreKey);
                    assert removed;
                    if (coreKeys.isEmpty()) {
                        indexToCoreKey.remove(index);
                    }
                }
            };
            boolean addedListener = false;
            try {
                reader.addCoreClosedListener(listener);
                addedListener = true;
                // Only add the core key to the map as a last operation so that
                // if another thread sees that the core key is already in the
                // map (like the check just before this synchronized block),
                // then it means that the closed listener has already been
                // registered.
                ShardId previous = coreKeyToShard.put(coreKey, shardId);
                assert previous == null;
            } finally {
                if (false == addedListener) {
                    try {
                        listener.onClose(coreKey);
                    } catch (IOException e) {
                        throw new RuntimeException("Blow up trying to recover from failure to add listener", e);
                    }
                }
            }
        }
    }
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) ShardId(org.elasticsearch.index.shard.ShardId) HashSet(java.util.HashSet) Collection(java.util.Collection) Map(java.util.Map) LeafReader(org.apache.lucene.index.LeafReader) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) CoreClosedListener(org.apache.lucene.index.LeafReader.CoreClosedListener) IOException(java.io.IOException) HashMap(java.util.HashMap) ShardUtils(org.elasticsearch.index.shard.ShardUtils) Collections(java.util.Collections) CoreClosedListener(org.apache.lucene.index.LeafReader.CoreClosedListener) HashSet(java.util.HashSet) Set(java.util.Set) IOException(java.io.IOException)

Example 85 with LeafReader

use of org.apache.lucene.index.LeafReader in project elasticsearch by elastic.

the class Versions method loadDocIdAndVersion.

/**
     * Load the internal doc ID and version for the uid from the reader, returning<ul>
     * <li>null if the uid wasn't found,
     * <li>a doc ID and a version otherwise
     * </ul>
     */
public static DocIdAndVersion loadDocIdAndVersion(IndexReader reader, Term term) throws IOException {
    assert term.field().equals(UidFieldMapper.NAME);
    List<LeafReaderContext> leaves = reader.leaves();
    if (leaves.isEmpty()) {
        return null;
    }
    // which are likely to be in the last segments
    for (int i = leaves.size() - 1; i >= 0; i--) {
        LeafReaderContext context = leaves.get(i);
        LeafReader leaf = context.reader();
        PerThreadIDAndVersionLookup lookup = getLookupState(leaf);
        DocIdAndVersion result = lookup.lookup(term.bytes(), leaf.getLiveDocs(), context);
        if (result != null) {
            return result;
        }
    }
    return null;
}
Also used : LeafReader(org.apache.lucene.index.LeafReader) LeafReaderContext(org.apache.lucene.index.LeafReaderContext)

Aggregations

LeafReader (org.apache.lucene.index.LeafReader)168 BytesRef (org.apache.lucene.util.BytesRef)65 Document (org.apache.lucene.document.Document)61 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)58 Directory (org.apache.lucene.store.Directory)56 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)55 DirectoryReader (org.apache.lucene.index.DirectoryReader)47 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)42 Test (org.junit.Test)36 IndexWriter (org.apache.lucene.index.IndexWriter)32 Terms (org.apache.lucene.index.Terms)30 TermsEnum (org.apache.lucene.index.TermsEnum)28 NumericDocValues (org.apache.lucene.index.NumericDocValues)24 SortedSetDocValues (org.apache.lucene.index.SortedSetDocValues)24 SortedDocValues (org.apache.lucene.index.SortedDocValues)22 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)21 IndexReader (org.apache.lucene.index.IndexReader)20 Term (org.apache.lucene.index.Term)20 SortedDocValuesField (org.apache.lucene.document.SortedDocValuesField)18 Bits (org.apache.lucene.util.Bits)18