diff --git a/core/src/main/java/org/elasticsearch/search/fetch/version/VersionFetchSubPhase.java b/core/src/main/java/org/elasticsearch/search/fetch/version/VersionFetchSubPhase.java index 6ecc1a2988a..0ee1112d9b9 100644 --- a/core/src/main/java/org/elasticsearch/search/fetch/version/VersionFetchSubPhase.java +++ b/core/src/main/java/org/elasticsearch/search/fetch/version/VersionFetchSubPhase.java @@ -18,12 +18,10 @@ */ package org.elasticsearch.search.fetch.version; -import org.apache.lucene.index.Term; -import org.apache.lucene.util.BytesRef; +import org.apache.lucene.index.NumericDocValues; import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.lucene.uid.Versions; -import org.elasticsearch.index.mapper.Uid; -import org.elasticsearch.index.mapper.UidFieldMapper; +import org.elasticsearch.index.mapper.VersionFieldMapper; import org.elasticsearch.search.fetch.FetchSubPhase; import org.elasticsearch.search.internal.SearchContext; @@ -36,18 +34,14 @@ public final class VersionFetchSubPhase implements FetchSubPhase { if (context.version() == false) { return; } - // it might make sense to cache the TermDocs on a shared fetch context and just skip here) - // it is going to mean we work on the high level multi reader and not the lower level reader as is - // the case below... - final long version; + long version = Versions.NOT_FOUND; try { - BytesRef uid = Uid.createUidAsBytes(hitContext.hit().type(), hitContext.hit().id()); - version = Versions.loadVersion( - hitContext.readerContext().reader(), - new Term(UidFieldMapper.NAME, uid) - ); + NumericDocValues versions = hitContext.reader().getNumericDocValues(VersionFieldMapper.NAME); + if (versions != null) { + version = versions.get(hitContext.docId()); + } } catch (IOException e) { - throw new ElasticsearchException("Could not query index for _version", e); + throw new ElasticsearchException("Could not retrieve version", e); } hitContext.hit().version(version < 0 ? -1 : version); }