Apache Lucene open-source search software
Merged /lucene/dev/trunk:r1441770-1442810
Robert Muir6b8e374c44db
diff --cc lucene/core/src/java/org/apache/lucene/search/FieldCache.javaindex fba2b6447b4,1a3b26baa79..1b08ebe393a--- a/lucene/core/src/java/org/apache/lucene/search/FieldCache.java+++ b/lucene/core/src/java/org/apache/lucene/search/FieldCache.java@@@ -21,14 -21,14 +21,16 @@@ import java.io.IOExceptionimport java.io.PrintStream;import org.apache.lucene.analysis.NumericTokenStream; // for javadocs-import org.apache.lucene.document.IntField; // for javadocs+import org.apache.lucene.document.DoubleField; // for javadocsimport org.apache.lucene.document.FloatField; // for javadocs+import org.apache.lucene.document.IntField; // for javadocsimport org.apache.lucene.document.LongField; // for javadocs-import org.apache.lucene.document.DoubleField; // for javadocs-import org.apache.lucene.index.DocTermOrds;import org.apache.lucene.index.AtomicReader;+import org.apache.lucene.index.BinaryDocValues;+import org.apache.lucene.index.DocTermOrds;+import org.apache.lucene.index.SortedDocValues;+ import org.apache.lucene.index.Terms;+ import org.apache.lucene.index.TermsEnum;import org.apache.lucene.util.Bits;import org.apache.lucene.util.BytesRef;import org.apache.lucene.util.NumericUtils;@@@ -633,8 -697,9 +682,8 @@@ public interface FieldCachereturn b.toString();}-}-+/*** EXPERT: Generates an array of CacheEntry objects representing all items* currently in the FieldCache.diff --cc lucene/core/src/java/org/apache/lucene/search/FieldCacheImpl.javaindex 2c5ea000bb4,9430e507558..b5bcc5f92f9--- a/lucene/core/src/java/org/apache/lucene/search/FieldCacheImpl.java+++ b/lucene/core/src/java/org/apache/lucene/search/FieldCacheImpl.java@@@ -297,21 -353,15 +297,17 @@@ class FieldCacheImpl implements FieldCasetDocsWithField = false;}}- final TermsEnum termsEnum = parser.termsEnum(terms);- assert termsEnum != null : "TermsEnum must not be null";+- final TermsEnum termsEnum = terms.iterator(null);++ final TermsEnum termsEnum = termsEnum(terms);+DocsEnum docs = null;+ FixedBitSet docsWithField = null;while(true) {final BytesRef term = termsEnum.next();if (term == null) {break;}- try {- visitTerm(term);- } catch (StopFillCacheException stop) {- break;- }- final byte termval = parser.parseByte(term);++ visitTerm(term);docs = termsEnum.docs(null, docs, DocsEnum.FLAG_NONE);while (true) {final int docID = docs.nextDoc();@@@ -329,111 -379,10 +325,117 @@@}}}+ }+++ protected abstract TermsEnum termsEnum(Terms terms) throws IOException;+ protected abstract void visitTerm(BytesRef term);+ protected abstract void visitDoc(int docID);+ }++ // null Bits means no docs matched+ void setDocsWithField(AtomicReader reader, String field, Bits docsWithField) {+ final int maxDoc = reader.maxDoc();+ final Bits bits;+ if (docsWithField == null) {+ bits = new Bits.MatchNoBits(maxDoc);+ } else if (docsWithField instanceof FixedBitSet) {+ final int numSet = ((FixedBitSet) docsWithField).cardinality();+ if (numSet >= maxDoc) {+ // The cardinality of the BitSet is maxDoc if all documents have a value.+ assert numSet == maxDoc;+ bits = new Bits.MatchAllBits(maxDoc);+ } else {+ bits = docsWithField;+ }+ } else {+ bits = docsWithField;+ }+ caches.get(DocsWithFieldCache.class).put(reader, new CacheKey(field, null), bits);+ }++ // inherit javadocs+ public Bytes getBytes (AtomicReader reader, String field, boolean setDocsWithField) throws IOException {+ return getBytes(reader, field, null, setDocsWithField);+ }++ // inherit javadocs+ public Bytes getBytes(AtomicReader reader, String field, ByteParser parser, boolean setDocsWithField)+ throws IOException {+ final NumericDocValues valuesIn = reader.getNumericDocValues(field);+ if (valuesIn != null) {+ // Not cached here by FieldCacheImpl (cached instead+ // per-thread by SegmentReader):+ return new Bytes() {+ @Override+ public byte get(int docID) {+ return (byte) valuesIn.get(docID);+ }+ };+ } else {+ return (Bytes) caches.get(Byte.TYPE).get(reader, new CacheKey(field, parser), setDocsWithField);+ }+ }++ static class BytesFromArray extends Bytes {+ private final byte[] values;++ public BytesFromArray(byte[] values) {+ this.values = values;+ }++ @Override+ public byte get(int docID) {+ return values[docID];+ }+ }++ static final class ByteCache extends Cache {+ ByteCache(FieldCacheImpl wrapper) {+ super(wrapper);+ }++ @Override+ protected Object createValue(AtomicReader reader, CacheKey key, boolean setDocsWithField)+ throws IOException {++ int maxDoc = reader.maxDoc();+ final byte[] values;+ final ByteParser parser = (ByteParser) key.custom;+ if (parser == null) {+ // Confusing: must delegate to wrapper (vs simply+ // setting parser = DEFAULT_SHORT_PARSER) so cache+ // key includes DEFAULT_SHORT_PARSER:+ return wrapper.getBytes(reader, key.field, DEFAULT_BYTE_PARSER, setDocsWithField);+ }++ values = new byte[maxDoc];++ Uninvert u = new Uninvert() {+ private byte currentValue;++ @Override+ public void visitTerm(BytesRef term) {+ currentValue = parser.parseByte(term);+ }++ @Override+ public void visitDoc(int docID) {+ values[docID] = currentValue;+ }++++ @Override++ protected TermsEnum termsEnum(Terms terms) throws IOException {++ return parser.termsEnum(terms);++ }+ };++ u.uninvert(reader, key.field, setDocsWithField);+if (setDocsWithField) {- wrapper.setDocsWithField(reader, field, docsWithField);+ wrapper.setDocsWithField(reader, key.field, u.docsWithField);}- return retArray;++ return new BytesFromArray(values);}}@@@ -479,40 -405,57 +481,45 @@@}@Override- protected Object createValue(AtomicReader reader, Entry entryKey, boolean setDocsWithField)+ protected Object createValue(AtomicReader reader, CacheKey key, boolean setDocsWithField)throws IOException {- String field = entryKey.field;- ShortParser parser = (ShortParser) entryKey.custom;++ int maxDoc = reader.maxDoc();+ final short[] values;+ final ShortParser parser = (ShortParser) key.custom;if (parser == null) {- return wrapper.getShorts(reader, field, FieldCache.DEFAULT_SHORT_PARSER, setDocsWithField);+ // Confusing: must delegate to wrapper (vs simply+ // setting parser = DEFAULT_SHORT_PARSER) so cache+ // key includes DEFAULT_SHORT_PARSER:+ return wrapper.getShorts(reader, key.field, DEFAULT_SHORT_PARSER, setDocsWithField);}- final int maxDoc = reader.maxDoc();- final short[] retArray = new short[maxDoc];- Terms terms = reader.terms(field);- FixedBitSet docsWithField = null;- if (terms != null) {- if (setDocsWithField) {- final int termsDocCount = terms.getDocCount();- assert termsDocCount <= maxDoc;- if (termsDocCount == maxDoc) {- // Fast case: all docs have this field:- wrapper.setDocsWithField(reader, field, new Bits.MatchAllBits(maxDoc));- setDocsWithField = false;++ values = new short[maxDoc];+ Uninvert u = new Uninvert() {+ private short currentValue;++ @Override+ public void visitTerm(BytesRef term) {+ currentValue = parser.parseShort(term);}- }- final TermsEnum termsEnum = parser.termsEnum(terms);- assert termsEnum != null : "TermsEnum must not be null";- DocsEnum docs = null;- while(true) {- final BytesRef term = termsEnum.next();- if (term == null) {- break;++ @Override+ public void visitDoc(int docID) {+ values[docID] = currentValue;}- final short termval = parser.parseShort(term);- docs = termsEnum.docs(null, docs, DocsEnum.FLAG_NONE);- while (true) {- final int docID = docs.nextDoc();- if (docID == DocIdSetIterator.NO_MORE_DOCS) {- break;- }- retArray[docID] = termval;- if (setDocsWithField) {- if (docsWithField == null) {- // Lazy init- docsWithField = new FixedBitSet(maxDoc);- }- docsWithField.set(docID);- }++++ @Override++ protected TermsEnum termsEnum(Terms terms) throws IOException {++ return parser.termsEnum(terms);+ }- }- }+ };++ u.uninvert(reader, key.field, setDocsWithField);+if (setDocsWithField) {- wrapper.setDocsWithField(reader, field, docsWithField);+ wrapper.setDocsWithField(reader, key.field, u.docsWithField);}- return retArray;+ return new ShortsFromArray(values);}}@@@ -570,64 -499,79 +577,69 @@@}@Override- protected Object createValue(AtomicReader reader, Entry entryKey, boolean setDocsWithField)+ protected Object createValue(final AtomicReader reader, CacheKey key, boolean setDocsWithField)throws IOException {- String field = entryKey.field;- IntParser parser = (IntParser) entryKey.custom;++ final IntParser parser = (IntParser) key.custom;if (parser == null) {+ // Confusing: must delegate to wrapper (vs simply+ // setting parser =+ // DEFAULT_INT_PARSER/NUMERIC_UTILS_INT_PARSER) so+ // cache key includes+ // DEFAULT_INT_PARSER/NUMERIC_UTILS_INT_PARSER:try {- return wrapper.getInts(reader, field, DEFAULT_INT_PARSER, setDocsWithField);+ return wrapper.getInts(reader, key.field, DEFAULT_INT_PARSER, setDocsWithField);} catch (NumberFormatException ne) {- return wrapper.getInts(reader, field, NUMERIC_UTILS_INT_PARSER, setDocsWithField);+ return wrapper.getInts(reader, key.field, NUMERIC_UTILS_INT_PARSER, setDocsWithField);}}- final int maxDoc = reader.maxDoc();- int[] retArray = null;- Terms terms = reader.terms(field);- FixedBitSet docsWithField = null;- if (terms != null) {- if (setDocsWithField) {- final int termsDocCount = terms.getDocCount();- assert termsDocCount <= maxDoc;- if (termsDocCount == maxDoc) {- // Fast case: all docs have this field:- wrapper.setDocsWithField(reader, field, new Bits.MatchAllBits(maxDoc));- setDocsWithField = false;+ final HoldsOneThing<int[]> valuesRef = new HoldsOneThing<int[]>();++ Uninvert u = new Uninvert() {+ private int currentValue;+ private int[] values;++ @Override+ public void visitTerm(BytesRef term) {+ currentValue = parser.parseInt(term);+ if (values == null) {+ // Lazy alloc so for the numeric field case+ // (which will hit a NumberFormatException+ // when we first try the DEFAULT_INT_PARSER),+ // we don't double-alloc:+ values = new int[reader.maxDoc()];+ valuesRef.set(values);+ }}- }- final TermsEnum termsEnum = parser.termsEnum(terms);- assert termsEnum != null : "TermsEnum must not be null";- DocsEnum docs = null;- while(true) {- final BytesRef term = termsEnum.next();- if (term == null) {- break;++ @Override+ public void visitDoc(int docID) {+ values[docID] = currentValue;}- final int termval = parser.parseInt(term);- if (retArray == null) {- // late init so numeric fields don't double allocate- retArray = new int[maxDoc];++++ @Override++ protected TermsEnum termsEnum(Terms terms) throws IOException {++ return parser.termsEnum(terms);+ }+ };- docs = termsEnum.docs(null, docs, DocsEnum.FLAG_NONE);- while (true) {- final int docID = docs.nextDoc();- if (docID == DocIdSetIterator.NO_MORE_DOCS) {- break;- }- retArray[docID] = termval;- if (setDocsWithField) {- if (docsWithField == null) {- // Lazy init- docsWithField = new FixedBitSet(maxDoc);- }- docsWithField.set(docID);- }- }- }- }+ u.uninvert(reader, key.field, setDocsWithField);- if (retArray == null) {- // no values- retArray = new int[maxDoc];- }if (setDocsWithField) {- wrapper.setDocsWithField(reader, field, docsWithField);+ wrapper.setDocsWithField(reader, key.field, u.docsWithField);+ }+ int[] values = valuesRef.get();+ if (values == null) {+ values = new int[reader.maxDoc()];}- return retArray;+ return new IntsFromArray(values);}}-- @Override+public Bits getDocsWithField(AtomicReader reader, String field)throws IOException {- return (Bits) caches.get(DocsWithFieldCache.class).get(reader, new Entry(field, null), false);+ return (Bits) caches.get(DocsWithFieldCache.class).get(reader, new CacheKey(field, null), false);}static final class DocsWithFieldCache extends Cache {@@@ -739,59 -650,72 +751,64 @@@}@Override- protected Object createValue(AtomicReader reader, Entry entryKey, boolean setDocsWithField)+ protected Object createValue(final AtomicReader reader, CacheKey key, boolean setDocsWithField)throws IOException {- String field = entryKey.field;- FloatParser parser = (FloatParser) entryKey.custom;++ final FloatParser parser = (FloatParser) key.custom;if (parser == null) {+ // Confusing: must delegate to wrapper (vs simply+ // setting parser =+ // DEFAULT_FLOAT_PARSER/NUMERIC_UTILS_FLOAT_PARSER) so+ // cache key includes+ // DEFAULT_FLOAT_PARSER/NUMERIC_UTILS_FLOAT_PARSER:try {- return wrapper.getFloats(reader, field, DEFAULT_FLOAT_PARSER, setDocsWithField);+ return wrapper.getFloats(reader, key.field, DEFAULT_FLOAT_PARSER, setDocsWithField);} catch (NumberFormatException ne) {- return wrapper.getFloats(reader, field, NUMERIC_UTILS_FLOAT_PARSER, setDocsWithField);+ return wrapper.getFloats(reader, key.field, NUMERIC_UTILS_FLOAT_PARSER, setDocsWithField);}}- final int maxDoc = reader.maxDoc();- float[] retArray = null;- Terms terms = reader.terms(field);- FixedBitSet docsWithField = null;- if (terms != null) {- if (setDocsWithField) {- final int termsDocCount = terms.getDocCount();- assert termsDocCount <= maxDoc;- if (termsDocCount == maxDoc) {- // Fast case: all docs have this field:- wrapper.setDocsWithField(reader, field, new Bits.MatchAllBits(maxDoc));- setDocsWithField = false;- }- }- final TermsEnum termsEnum = parser.termsEnum(terms);- assert termsEnum != null : "TermsEnum must not be null";- DocsEnum docs = null;- while(true) {- final BytesRef term = termsEnum.next();- if (term == null) {- break;+ final HoldsOneThing<float[]> valuesRef = new HoldsOneThing<float[]>();++ Uninvert u = new Uninvert() {+ private float currentValue;+ private float[] values;++ @Override+ public void visitTerm(BytesRef term) {+ currentValue = parser.parseFloat(term);+ if (values == null) {+ // Lazy alloc so for the numeric field case+ // (which will hit a NumberFormatException+ // when we first try the DEFAULT_INT_PARSER),+ // we don't double-alloc:+ values = new float[reader.maxDoc()];+ valuesRef.set(values);+ }}- final float termval = parser.parseFloat(term);- if (retArray == null) {- // late init so numeric fields don't double allocate- retArray = new float[maxDoc];++ @Override+ public void visitDoc(int docID) {+ values[docID] = currentValue;}+- docs = termsEnum.docs(null, docs, DocsEnum.FLAG_NONE);- while (true) {- final int docID = docs.nextDoc();- if (docID == DocIdSetIterator.NO_MORE_DOCS) {- break;- }- retArray[docID] = termval;- if (setDocsWithField) {- if (docsWithField == null) {- // Lazy init- docsWithField = new FixedBitSet(maxDoc);- }- docsWithField.set(docID);- }++ @Override++ protected TermsEnum termsEnum(Terms terms) throws IOException {++ return parser.termsEnum(terms);+ }- }- }+ };++ u.uninvert(reader, key.field, setDocsWithField);- if (retArray == null) {- // no values- retArray = new float[maxDoc];- }if (setDocsWithField) {- wrapper.setDocsWithField(reader, field, docsWithField);+ wrapper.setDocsWithField(reader, key.field, u.docsWithField);+ }++ float[] values = valuesRef.get();+ if (values == null) {+ values = new float[reader.maxDoc()];}- return retArray;+ return new FloatsFromArray(values);}}@@@ -837,58 -738,72 +854,63 @@@}@Override- protected Object createValue(AtomicReader reader, Entry entryKey, boolean setDocsWithField)+ protected Object createValue(final AtomicReader reader, CacheKey key, boolean setDocsWithField)throws IOException {- String field = entryKey.field;- FieldCache.LongParser parser = (FieldCache.LongParser) entryKey.custom;++ final LongParser parser = (LongParser) key.custom;if (parser == null) {+ // Confusing: must delegate to wrapper (vs simply+ // setting parser =+ // DEFAULT_LONG_PARSER/NUMERIC_UTILS_LONG_PARSER) so+ // cache key includes+ // DEFAULT_LONG_PARSER/NUMERIC_UTILS_LONG_PARSER:try {- return wrapper.getLongs(reader, field, DEFAULT_LONG_PARSER, setDocsWithField);+ return wrapper.getLongs(reader, key.field, DEFAULT_LONG_PARSER, setDocsWithField);} catch (NumberFormatException ne) {- return wrapper.getLongs(reader, field, NUMERIC_UTILS_LONG_PARSER, setDocsWithField);+ return wrapper.getLongs(reader, key.field, NUMERIC_UTILS_LONG_PARSER, setDocsWithField);}}- final int maxDoc = reader.maxDoc();- long[] retArray = null;- Terms terms = reader.terms(field);- FixedBitSet docsWithField = null;- if (terms != null) {- if (setDocsWithField) {- final int termsDocCount = terms.getDocCount();- assert termsDocCount <= maxDoc;- if (termsDocCount == maxDoc) {- // Fast case: all docs have this field:- wrapper.setDocsWithField(reader, field, new Bits.MatchAllBits(maxDoc));- setDocsWithField = false;+ final HoldsOneThing<long[]> valuesRef = new HoldsOneThing<long[]>();++ Uninvert u = new Uninvert() {+ private long currentValue;+ private long[] values;++ @Override+ public void visitTerm(BytesRef term) {+ currentValue = parser.parseLong(term);+ if (values == null) {+ // Lazy alloc so for the numeric field case+ // (which will hit a NumberFormatException+ // when we first try the DEFAULT_INT_PARSER),+ // we don't double-alloc:+ values = new long[reader.maxDoc()];+ valuesRef.set(values);+ }}- }- final TermsEnum termsEnum = parser.termsEnum(terms);- assert termsEnum != null : "TermsEnum must not be null";- DocsEnum docs = null;- while(true) {- final BytesRef term = termsEnum.next();- if (term == null) {- break;++ @Override+ public void visitDoc(int docID) {+ values[docID] = currentValue;}- final long termval = parser.parseLong(term);- if (retArray == null) {- // late init so numeric fields don't double allocate- retArray = new long[maxDoc];++++ @Override++ protected TermsEnum termsEnum(Terms terms) throws IOException {++ return parser.termsEnum(terms);+ }+ };- docs = termsEnum.docs(null, docs, DocsEnum.FLAG_NONE);- while (true) {- final int docID = docs.nextDoc();- if (docID == DocIdSetIterator.NO_MORE_DOCS) {- break;- }- retArray[docID] = termval;- if (setDocsWithField) {- if (docsWithField == null) {- // Lazy init- docsWithField = new FixedBitSet(maxDoc);- }- docsWithField.set(docID);- }- }- }- }+ u.uninvert(reader, key.field, setDocsWithField);- if (retArray == null) {- // no values- retArray = new long[maxDoc];- }if (setDocsWithField) {- wrapper.setDocsWithField(reader, field, docsWithField);+ wrapper.setDocsWithField(reader, key.field, u.docsWithField);+ }+ long[] values = valuesRef.get();+ if (values == null) {+ values = new long[reader.maxDoc()];}- return retArray;+ return new LongsFromArray(values);}}@@@ -935,58 -827,70 +957,63 @@@}@Override- protected Object createValue(AtomicReader reader, Entry entryKey, boolean setDocsWithField)+ protected Object createValue(final AtomicReader reader, CacheKey key, boolean setDocsWithField)throws IOException {- String field = entryKey.field;- FieldCache.DoubleParser parser = (FieldCache.DoubleParser) entryKey.custom;++ final DoubleParser parser = (DoubleParser) key.custom;if (parser == null) {+ // Confusing: must delegate to wrapper (vs simply+ // setting parser =+ // DEFAULT_DOUBLE_PARSER/NUMERIC_UTILS_DOUBLE_PARSER) so+ // cache key includes+ // DEFAULT_DOUBLE_PARSER/NUMERIC_UTILS_DOUBLE_PARSER:try {- return wrapper.getDoubles(reader, field, DEFAULT_DOUBLE_PARSER, setDocsWithField);+ return wrapper.getDoubles(reader, key.field, DEFAULT_DOUBLE_PARSER, setDocsWithField);} catch (NumberFormatException ne) {- return wrapper.getDoubles(reader, field, NUMERIC_UTILS_DOUBLE_PARSER, setDocsWithField);+ return wrapper.getDoubles(reader, key.field, NUMERIC_UTILS_DOUBLE_PARSER, setDocsWithField);}}- final int maxDoc = reader.maxDoc();- double[] retArray = null;- Terms terms = reader.terms(field);- FixedBitSet docsWithField = null;- if (terms != null) {- if (setDocsWithField) {- final int termsDocCount = terms.getDocCount();- assert termsDocCount <= maxDoc;- if (termsDocCount == maxDoc) {- // Fast case: all docs have this field:- wrapper.setDocsWithField(reader, field, new Bits.MatchAllBits(maxDoc));- setDocsWithField = false;+ final HoldsOneThing<double[]> valuesRef = new HoldsOneThing<double[]>();++ Uninvert u = new Uninvert() {+ private double currentValue;+ private double[] values;++ @Override+ public void visitTerm(BytesRef term) {+ currentValue = parser.parseDouble(term);+ if (values == null) {+ // Lazy alloc so for the numeric field case+ // (which will hit a NumberFormatException+ // when we first try the DEFAULT_INT_PARSER),+ // we don't double-alloc:+ values = new double[reader.maxDoc()];+ valuesRef.set(values);+ }}- }- final TermsEnum termsEnum = parser.termsEnum(terms);- assert termsEnum != null : "TermsEnum must not be null";- DocsEnum docs = null;- while(true) {- final BytesRef term = termsEnum.next();- if (term == null) {- break;++ @Override+ public void visitDoc(int docID) {+ values[docID] = currentValue;}- final double termval = parser.parseDouble(term);- if (retArray == null) {- // late init so numeric fields don't double allocate- retArray = new double[maxDoc];++++ @Override++ protected TermsEnum termsEnum(Terms terms) throws IOException {++ return parser.termsEnum(terms);+ }+ };++ u.uninvert(reader, key.field, setDocsWithField);- docs = termsEnum.docs(null, docs, DocsEnum.FLAG_NONE);- while (true) {- final int docID = docs.nextDoc();- if (docID == DocIdSetIterator.NO_MORE_DOCS) {- break;- }- retArray[docID] = termval;- if (setDocsWithField) {- if (docsWithField == null) {- // Lazy init- docsWithField = new FixedBitSet(maxDoc);- }- docsWithField.set(docID);- }- }- }- }- if (retArray == null) { // no values- retArray = new double[maxDoc];- }if (setDocsWithField) {- wrapper.setDocsWithField(reader, field, docsWithField);+ wrapper.setDocsWithField(reader, key.field, u.docsWithField);+ }+ double[] values = valuesRef.get();+ if (values == null) {+ values = new double[reader.maxDoc()];}- return retArray;+ return new DoublesFromArray(values);}}diff --cc lucene/core/src/test/org/apache/lucene/search/JustCompileSearch.javaindex 5a8d67cd866,e2fde126487..e72b675b356--- a/lucene/core/src/test/org/apache/lucene/search/JustCompileSearch.java+++ b/lucene/core/src/test/org/apache/lucene/search/JustCompileSearch.java@@@ -20,6 -20,9 +20,8 @@@ package org.apache.lucene.searchimport java.io.IOException;import org.apache.lucene.index.AtomicReaderContext;-import org.apache.lucene.index.Norm;+ import org.apache.lucene.index.Terms;+ import org.apache.lucene.index.TermsEnum;import org.apache.lucene.search.similarities.Similarity;import org.apache.lucene.util.Bits;import org.apache.lucene.util.BytesRef;diff --cc lucene/core/src/test/org/apache/lucene/search/TestSort.javaindex 44df6cdc87c,95348834a49..e13170fe0f5--- a/lucene/core/src/test/org/apache/lucene/search/TestSort.java+++ b/lucene/core/src/test/org/apache/lucene/search/TestSort.java@@@ -630,13 -593,17 +637,17 @@@ public class TestSort extends LuceneTesassertSaneFieldCaches(getTestName() + " IntParser");fc.purgeAllCaches();- sort.setSort (new SortField ("parser", new FieldCache.FloatParser(){+ sort.setSort(new SortField("parser", new FieldCache.FloatParser(){@Overridepublic final float parseFloat(final BytesRef term) {- return (float) Math.sqrt( term.bytes[term.offset] );+ return (float) Math.sqrt( term.bytes[term.offset]);}- }), SortField.FIELD_DOC);- assertMatches(full, queryA, sort, "JIHGFEDCBA");+ @Override+ public TermsEnum termsEnum(Terms terms) throws IOException {+ return terms.iterator(null);+ }+ }), SortField.FIELD_DOC );+ assertMatches (full, queryA, sort, "JIHGFEDCBA");assertSaneFieldCaches(getTestName() + " FloatParser");fc.purgeAllCaches();@@@ -650,13 -622,17 +666,17 @@@assertSaneFieldCaches(getTestName() + " LongParser");fc.purgeAllCaches();- sort.setSort (new SortField ("parser", new FieldCache.DoubleParser(){+ sort.setSort(new SortField("parser", new FieldCache.DoubleParser(){@Overridepublic final double parseDouble(final BytesRef term) {- return Math.pow( term.bytes[term.offset], (term.bytes[term.offset]-'A') );+ return Math.pow( term.bytes[term.offset], (term.bytes[term.offset]-'A'));}- }), SortField.FIELD_DOC);- assertMatches(full, queryA, sort, "JIHGFEDCBA");+ @Override+ public TermsEnum termsEnum(Terms terms) throws IOException {+ return terms.iterator(null);+ }+ }), SortField.FIELD_DOC );+ assertMatches (full, queryA, sort, "JIHGFEDCBA");assertSaneFieldCaches(getTestName() + " DoubleParser");fc.purgeAllCaches();diff --cc lucene/highlighter/src/java/org/apache/lucene/search/highlight/WeightedSpanTermExtractor.javaindex 4412738cdff,56551ae9389..64cc3657658--- a/lucene/highlighter/src/java/org/apache/lucene/search/highlight/WeightedSpanTermExtractor.java+++ b/lucene/highlighter/src/java/org/apache/lucene/search/highlight/WeightedSpanTermExtractor.java@@@ -29,11 -29,18 +29,20 @@@ import java.util.TreeSetimport org.apache.lucene.analysis.CachingTokenFilter;import org.apache.lucene.analysis.TokenStream;+ import org.apache.lucene.index.AtomicReader;import org.apache.lucene.index.AtomicReaderContext;-import org.apache.lucene.index.DocValues;++import org.apache.lucene.index.BinaryDocValues;+ import org.apache.lucene.index.FieldInfos;+ import org.apache.lucene.index.Fields;+ import org.apache.lucene.index.FilterAtomicReader;import org.apache.lucene.index.IndexReader;++import org.apache.lucene.index.NumericDocValues;++import org.apache.lucene.index.SortedDocValues;import org.apache.lucene.index.Term;import org.apache.lucene.index.TermContext;+ import org.apache.lucene.index.Terms;import org.apache.lucene.index.memory.MemoryIndex;+ import org.apache.lucene.queries.CommonTermsQuery;import org.apache.lucene.search.*;import org.apache.lucene.search.spans.FieldMaskingSpanQuery;import org.apache.lucene.search.spans.SpanFirstQuery;@@@ -338,23 -347,69 +349,79 @@@ public class WeightedSpanTermExtractorreturn rv;}- protected AtomicReaderContext getLeafContextForField(String field) throws IOException {- if(wrapToCaching && !cachedTokenStream && !(tokenStream instanceof CachingTokenFilter)) {- tokenStream = new CachingTokenFilter(new OffsetLimitTokenFilter(tokenStream, maxDocCharsToAnalyze));- cachedTokenStream = true;- }- AtomicReaderContext context = readers.get(field);- if (context == null) {- MemoryIndex indexer = new MemoryIndex();- indexer.addField(field, new OffsetLimitTokenFilter(tokenStream, maxDocCharsToAnalyze));+ protected AtomicReaderContext getLeafContext() throws IOException {+ if (reader == null) {+ if(wrapToCaching && !(tokenStream instanceof CachingTokenFilter)) {+ assert !cachedTokenStream;+ tokenStream = new CachingTokenFilter(new OffsetLimitTokenFilter(tokenStream, maxDocCharsToAnalyze));+ cachedTokenStream = true;+ }+ final MemoryIndex indexer = new MemoryIndex(true);+ indexer.addField(DelegatingAtomicReader.FIELD_NAME, tokenStream);tokenStream.reset();- IndexSearcher searcher = indexer.createSearcher();+ final IndexSearcher searcher = indexer.createSearcher();// MEM index has only atomic ctx- context = (AtomicReaderContext) searcher.getTopReaderContext();- readers.put(field, context);+ reader = new DelegatingAtomicReader(((AtomicReaderContext)searcher.getTopReaderContext()).reader());+ }+ return reader.getContext();+ }++ /*+ * This reader will just delegate every call to a single field in the wrapped+ * AtomicReader. This way we only need to build this field once rather than+ * N-Times+ */+ static final class DelegatingAtomicReader extends FilterAtomicReader {+ private static final String FIELD_NAME = "shadowed_field";++ DelegatingAtomicReader(AtomicReader in) {+ super(in);}++ @Override+ public FieldInfos getFieldInfos() {+ throw new UnsupportedOperationException();+ }++ @Override+ public Fields fields() throws IOException {+ return new FilterFields(super.fields()) {+ @Override+ public Terms terms(String field) throws IOException {+ return super.terms(DelegatingAtomicReader.FIELD_NAME);+ }++ @Override+ public Iterator<String> iterator() {+ return Collections.singletonList(DelegatingAtomicReader.FIELD_NAME).iterator();+ }- return context;+ @Override+ public int size() {+ return 1;+ }+ };+ }++ @Override- public DocValues docValues(String field) throws IOException {- return super.docValues(FIELD_NAME);++ public NumericDocValues getNumericDocValues(String field) throws IOException {++ return super.getNumericDocValues(FIELD_NAME);+ }-++++ @Override++ public BinaryDocValues getBinaryDocValues(String field) throws IOException {++ return super.getBinaryDocValues(FIELD_NAME);++ }++++ @Override++ public SortedDocValues getSortedDocValues(String field) throws IOException {++ return super.getSortedDocValues(FIELD_NAME);++ }+++ @Override- public DocValues normValues(String field) throws IOException {- return super.normValues(FIELD_NAME);++ public NumericDocValues getNormValues(String field) throws IOException {++ return super.getNormValues(FIELD_NAME);+ }}/**