diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index dae948a827e..bc1c0a6fc68 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -119,10 +119,10 @@ API Changes * LUCENE-6218, LUCENE-6220: Add Collector.needsScores() and needsScores parameter to Query.createWeight(). (Robert Muir, Adrien Grand) -* LUCENE-4524: Merge DocsEnum and DocsAndPositionsEnum into a single - PostingsEnum iterator. TermsEnum.docs() and TermsEnum.docsAndPositions() - are replaced by TermsEnum.postings(). (Alan Woodward, Simon Willnauer, - Robert Muir) +* LUCENE-4524, LUCENE-6246, LUCENE-6256: Merge DocsEnum and DocsAndPositionsEnum + into a single PostingsEnum iterator. TermsEnum.docs() and TermsEnum.docsAndPositions() + are replaced by TermsEnum.postings(). + (Alan Woodward, Simon Willnauer, Robert Muir, Ryan Ernst) * LUCENE-6222: Removed TermFilter, use a QueryWrapperFilter(TermQuery) instead. This will be as efficient now that queries can opt out from @@ -143,10 +143,6 @@ API Changes * LUCENE-6245: Force Filter subclasses to implement toString API from Query. (Ryan Ernst) -* LUCENE-6256: Change PostingsEnum.nextPosition() to consistently return -1 when - positions are not available. - (Ryan Ernst) - * LUCENE-6268: Replace FieldValueFilter and DocValuesRangeFilter with equivalent queries that support approximations. (Adrien Grand) diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/blockterms/BlockTermsReader.java b/lucene/codecs/src/java/org/apache/lucene/codecs/blockterms/BlockTermsReader.java index 8001657aeea..d50c2d24155 100644 --- a/lucene/codecs/src/java/org/apache/lucene/codecs/blockterms/BlockTermsReader.java +++ b/lucene/codecs/src/java/org/apache/lucene/codecs/blockterms/BlockTermsReader.java @@ -653,7 +653,7 @@ public class BlockTermsReader extends FieldsProducer { @Override public PostingsEnum postings(Bits liveDocs, PostingsEnum reuse, int flags) throws IOException { - if (PostingsEnum.requiresPositions(flags)) { + if (PostingsEnum.featureRequested(flags, PostingsEnum.POSITIONS)) { if (fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) < 0) { // Positions were not indexed: return null; diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/blocktreeords/OrdsIntersectTermsEnum.java b/lucene/codecs/src/java/org/apache/lucene/codecs/blocktreeords/OrdsIntersectTermsEnum.java index 2e4daaeba8f..c267f9dbb75 100644 --- a/lucene/codecs/src/java/org/apache/lucene/codecs/blocktreeords/OrdsIntersectTermsEnum.java +++ b/lucene/codecs/src/java/org/apache/lucene/codecs/blocktreeords/OrdsIntersectTermsEnum.java @@ -204,7 +204,7 @@ final class OrdsIntersectTermsEnum extends TermsEnum { @Override public PostingsEnum postings(Bits skipDocs, PostingsEnum reuse, int flags) throws IOException { - if (PostingsEnum.requiresPositions(flags)) { + if (PostingsEnum.featureRequested(flags, PostingsEnum.POSITIONS)) { if (fr.fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) < 0) { // Positions were not indexed: return null; diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/blocktreeords/OrdsSegmentTermsEnum.java b/lucene/codecs/src/java/org/apache/lucene/codecs/blocktreeords/OrdsSegmentTermsEnum.java index 4f0182f2be7..32455f9ff48 100644 --- a/lucene/codecs/src/java/org/apache/lucene/codecs/blocktreeords/OrdsSegmentTermsEnum.java +++ b/lucene/codecs/src/java/org/apache/lucene/codecs/blocktreeords/OrdsSegmentTermsEnum.java @@ -925,7 +925,7 @@ public final class OrdsSegmentTermsEnum extends TermsEnum { @Override public PostingsEnum postings(Bits skipDocs, PostingsEnum reuse, int flags) throws IOException { - if (PostingsEnum.requiresPositions(flags)) { + if (PostingsEnum.featureRequested(flags, PostingsEnum.POSITIONS)) { if (fr.fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) < 0) { // Positions were not indexed: return null; diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextFieldsReader.java b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextFieldsReader.java index 2c5eb4dfba8..cf23fc00e97 100644 --- a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextFieldsReader.java +++ b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextFieldsReader.java @@ -209,7 +209,7 @@ class SimpleTextFieldsReader extends FieldsProducer { @Override public PostingsEnum postings(Bits liveDocs, PostingsEnum reuse, int flags) throws IOException { - if (PostingsEnum.requiresPositions(flags)) { + if (PostingsEnum.featureRequested(flags, PostingsEnum.POSITIONS)) { if (indexOptions.compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) < 0) { // Positions were not indexed return null; diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextTermVectorsReader.java b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextTermVectorsReader.java index b8d74e3302c..cc63d47a6e1 100644 --- a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextTermVectorsReader.java +++ b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextTermVectorsReader.java @@ -389,7 +389,7 @@ public class SimpleTextTermVectorsReader extends TermVectorsReader { @Override public PostingsEnum postings(Bits liveDocs, PostingsEnum reuse, int flags) throws IOException { - if (PostingsEnum.requiresPositions(flags)) { + if (PostingsEnum.featureRequested(flags, PostingsEnum.POSITIONS)) { SimpleTVPostings postings = current.getValue(); if (postings.positions == null && postings.startOffsets == null) { return null; @@ -402,7 +402,7 @@ public class SimpleTextTermVectorsReader extends TermVectorsReader { // TODO: reuse SimpleTVDocsEnum e = new SimpleTVDocsEnum(); - e.reset(liveDocs, (flags & PostingsEnum.FREQS) == 0 ? 1 : current.getValue().freq); + e.reset(liveDocs, PostingsEnum.featureRequested(flags, PostingsEnum.FREQS) == false ? 1 : current.getValue().freq); return e; } diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene50/Lucene50PostingsReader.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene50/Lucene50PostingsReader.java index aa28d973727..6a710a6e69b 100644 --- a/lucene/core/src/java/org/apache/lucene/codecs/lucene50/Lucene50PostingsReader.java +++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene50/Lucene50PostingsReader.java @@ -214,8 +214,8 @@ public final class Lucene50PostingsReader extends PostingsReaderBase { if (!indexHasPositions) return null; - if ((!indexHasOffsets || (flags & PostingsEnum.OFFSETS) == 0) && - (!indexHasPayloads || (flags & PostingsEnum.PAYLOADS) == 0)) { + if ((!indexHasOffsets || PostingsEnum.featureRequested(flags, PostingsEnum.OFFSETS) == false) && + (!indexHasPayloads || PostingsEnum.featureRequested(flags, PostingsEnum.PAYLOADS) == false)) { BlockPostingsEnum docsAndPositionsEnum; if (reuse instanceof BlockPostingsEnum) { docsAndPositionsEnum = (BlockPostingsEnum) reuse; @@ -317,7 +317,7 @@ public final class Lucene50PostingsReader extends PostingsReaderBase { } doc = -1; - this.needsFreq = (flags & PostingsEnum.FREQS) != 0; + this.needsFreq = PostingsEnum.featureRequested(flags, PostingsEnum.FREQS); if (indexHasFreq == false || needsFreq == false) { Arrays.fill(freqBuffer, 1); } @@ -981,8 +981,8 @@ public final class Lucene50PostingsReader extends PostingsReaderBase { lastPosBlockFP = posTermStartFP + termState.lastPosBlockOffset; } - this.needsOffsets = (flags & PostingsEnum.OFFSETS) != 0; - this.needsPayloads = (flags & PostingsEnum.PAYLOADS) != 0; + this.needsOffsets = PostingsEnum.featureRequested(flags, PostingsEnum.OFFSETS); + this.needsPayloads = PostingsEnum.featureRequested(flags, PostingsEnum.PAYLOADS); doc = -1; accum = 0; diff --git a/lucene/core/src/java/org/apache/lucene/index/FreqProxFields.java b/lucene/core/src/java/org/apache/lucene/index/FreqProxFields.java index 6d319b794e4..07887c2692e 100644 --- a/lucene/core/src/java/org/apache/lucene/index/FreqProxFields.java +++ b/lucene/core/src/java/org/apache/lucene/index/FreqProxFields.java @@ -244,7 +244,7 @@ class FreqProxFields extends Fields { throw new IllegalArgumentException("did not index positions"); } - if (!terms.hasOffsets && (flags & PostingsEnum.OFFSETS) == PostingsEnum.OFFSETS) { + if (!terms.hasOffsets && PostingsEnum.featureRequested(flags, PostingsEnum.OFFSETS)) { // Caller wants offsets but we didn't index them; // don't lie: throw new IllegalArgumentException("did not index offsets"); @@ -264,7 +264,7 @@ class FreqProxFields extends Fields { FreqProxDocsEnum docsEnum; - if (!terms.hasFreq && (flags & PostingsEnum.FREQS) != 0) { + if (!terms.hasFreq && PostingsEnum.featureRequested(flags, PostingsEnum.FREQS)) { // Caller wants freqs but we didn't index them; // don't lie: throw new IllegalArgumentException("did not index freq"); diff --git a/lucene/core/src/java/org/apache/lucene/index/MultiFields.java b/lucene/core/src/java/org/apache/lucene/index/MultiFields.java index ccca99a969d..3acc1532891 100644 --- a/lucene/core/src/java/org/apache/lucene/index/MultiFields.java +++ b/lucene/core/src/java/org/apache/lucene/index/MultiFields.java @@ -151,7 +151,7 @@ public final class MultiFields extends Fields { * term does not exist or positions were not indexed. * @see #getTermPositionsEnum(IndexReader, Bits, String, BytesRef, int) */ public static PostingsEnum getTermPositionsEnum(IndexReader r, Bits liveDocs, String field, BytesRef term) throws IOException { - return getTermPositionsEnum(r, liveDocs, field, term, PostingsEnum.OFFSETS | PostingsEnum.PAYLOADS); + return getTermPositionsEnum(r, liveDocs, field, term, PostingsEnum.ALL); } /** Returns {@link PostingsEnum} for the specified diff --git a/lucene/core/src/java/org/apache/lucene/index/PostingsEnum.java b/lucene/core/src/java/org/apache/lucene/index/PostingsEnum.java index fe8418ced87..374ae268988 100644 --- a/lucene/core/src/java/org/apache/lucene/index/PostingsEnum.java +++ b/lucene/core/src/java/org/apache/lucene/index/PostingsEnum.java @@ -33,37 +33,35 @@ public abstract class PostingsEnum extends DocIdSetIterator { * Flag to pass to {@link TermsEnum#postings(Bits, PostingsEnum, int)} if you don't * require per-document postings in the returned enum. */ - public static final int NONE = 0x0; + public static final short NONE = 0; /** Flag to pass to {@link TermsEnum#postings(Bits, PostingsEnum, int)} * if you require term frequencies in the returned enum. */ - public static final int FREQS = 0x1; + public static final short FREQS = 1 << 3; /** Flag to pass to {@link TermsEnum#postings(Bits, PostingsEnum, int)} * if you require term positions in the returned enum. */ - public static final int POSITIONS = 0x3; + public static final short POSITIONS = FREQS | 1 << 4; /** Flag to pass to {@link TermsEnum#postings(Bits, PostingsEnum, int)} * if you require offsets in the returned enum. */ - public static final int OFFSETS = 0x7; + public static final short OFFSETS = POSITIONS | 1 << 5; /** Flag to pass to {@link TermsEnum#postings(Bits, PostingsEnum, int)} * if you require payloads in the returned enum. */ - public static final int PAYLOADS = 0xB; + public static final short PAYLOADS = POSITIONS | 1 << 6; /** * Flag to pass to {@link TermsEnum#postings(Bits, PostingsEnum, int)} * to get positions, payloads and offsets in the returned enum */ - public static final int ALL = POSITIONS | PAYLOADS; + public static final short ALL = OFFSETS | PAYLOADS; /** - * Returns true if the passed in flags require positions to be indexed - * @param flags the postings flags - * @return true if the passed in flags require positions to be indexed + * Returns true if the given feature is requested in the flags, false otherwise. */ - public static boolean requiresPositions(int flags) { - return ((flags & POSITIONS) >= POSITIONS); + public static boolean featureRequested(int flags, short feature) { + return (flags & feature) == feature; } private AttributeSource atts = null; diff --git a/lucene/core/src/test/org/apache/lucene/index/TestPayloads.java b/lucene/core/src/test/org/apache/lucene/index/TestPayloads.java index 49f752e3f44..8ae554caa21 100644 --- a/lucene/core/src/test/org/apache/lucene/index/TestPayloads.java +++ b/lucene/core/src/test/org/apache/lucene/index/TestPayloads.java @@ -609,7 +609,7 @@ public class TestPayloads extends LuceneTestCase { writer.addDocument(doc); DirectoryReader reader = writer.getReader(); LeafReader sr = SlowCompositeReaderWrapper.wrap(reader); - PostingsEnum de = sr.postings(new Term("field", "withPayload"), PostingsEnum.POSITIONS); + PostingsEnum de = sr.postings(new Term("field", "withPayload"), PostingsEnum.PAYLOADS); de.nextDoc(); de.nextPosition(); assertEquals(new BytesRef("test"), de.getPayload()); @@ -643,7 +643,7 @@ public class TestPayloads extends LuceneTestCase { writer.addDocument(doc); DirectoryReader reader = writer.getReader(); SegmentReader sr = getOnlySegmentReader(reader); - PostingsEnum de = sr.postings(new Term("field", "withPayload"), PostingsEnum.POSITIONS); + PostingsEnum de = sr.postings(new Term("field", "withPayload"), PostingsEnum.PAYLOADS); de.nextDoc(); de.nextPosition(); assertEquals(new BytesRef("test"), de.getPayload()); diff --git a/lucene/misc/src/java/org/apache/lucene/index/SortingLeafReader.java b/lucene/misc/src/java/org/apache/lucene/index/SortingLeafReader.java index bea7c988a0a..5740f62cf35 100644 --- a/lucene/misc/src/java/org/apache/lucene/index/SortingLeafReader.java +++ b/lucene/misc/src/java/org/apache/lucene/index/SortingLeafReader.java @@ -132,7 +132,7 @@ public class SortingLeafReader extends FilterLeafReader { @Override public PostingsEnum postings(Bits liveDocs, PostingsEnum reuse, final int flags) throws IOException { - if (PostingsEnum.requiresPositions(flags)) { + if (PostingsEnum.featureRequested(flags, PostingsEnum.POSITIONS)) { final PostingsEnum inReuse; final SortingPostingsEnum wrapReuse; if (reuse != null && reuse instanceof SortingPostingsEnum) { @@ -171,7 +171,7 @@ public class SortingLeafReader extends FilterLeafReader { } final PostingsEnum inDocs = in.postings(newToOld(liveDocs), inReuse, flags); - final boolean withFreqs = indexOptions.compareTo(IndexOptions.DOCS_AND_FREQS) >=0 && (flags & PostingsEnum.FREQS) != 0; + final boolean withFreqs = indexOptions.compareTo(IndexOptions.DOCS_AND_FREQS) >=0 && PostingsEnum.featureRequested(flags, PostingsEnum.FREQS); return new SortingDocsEnum(docMap.size(), wrapReuse, inDocs, withFreqs, docMap); } diff --git a/lucene/sandbox/src/java/org/apache/lucene/codecs/idversion/IDVersionPostingsReader.java b/lucene/sandbox/src/java/org/apache/lucene/codecs/idversion/IDVersionPostingsReader.java index 4ffa359efec..0466225287d 100644 --- a/lucene/sandbox/src/java/org/apache/lucene/codecs/idversion/IDVersionPostingsReader.java +++ b/lucene/sandbox/src/java/org/apache/lucene/codecs/idversion/IDVersionPostingsReader.java @@ -66,7 +66,7 @@ final class IDVersionPostingsReader extends PostingsReaderBase { public PostingsEnum postings(FieldInfo fieldInfo, BlockTermState termState, Bits liveDocs, PostingsEnum reuse, int flags) throws IOException { SingleDocsEnum docsEnum; - if (PostingsEnum.requiresPositions(flags)) { + if (PostingsEnum.featureRequested(flags, PostingsEnum.POSITIONS)) { SinglePostingsEnum posEnum; if (reuse instanceof SinglePostingsEnum) { diff --git a/lucene/test-framework/src/java/org/apache/lucene/index/BasePostingsFormatTestCase.java b/lucene/test-framework/src/java/org/apache/lucene/index/BasePostingsFormatTestCase.java index 6779713c8f2..099949ade32 100644 --- a/lucene/test-framework/src/java/org/apache/lucene/index/BasePostingsFormatTestCase.java +++ b/lucene/test-framework/src/java/org/apache/lucene/index/BasePostingsFormatTestCase.java @@ -643,18 +643,18 @@ public abstract class BasePostingsFormatTestCase extends BaseIndexFileFormatTest if (liveDocs != null) { throw new IllegalArgumentException("liveDocs must be null"); } - if (PostingsEnum.requiresPositions(flags)) { + if (PostingsEnum.featureRequested(flags, PostingsEnum.POSITIONS)) { if (maxAllowed.compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) < 0) { return null; } - if ((flags & PostingsEnum.OFFSETS) == PostingsEnum.OFFSETS && maxAllowed.compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS) < 0) { + if (PostingsEnum.featureRequested(flags, PostingsEnum.OFFSETS) && maxAllowed.compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS) < 0) { return null; } - if ((flags & PostingsEnum.PAYLOADS) == PostingsEnum.PAYLOADS && allowPayloads == false) { + if (PostingsEnum.featureRequested(flags, PostingsEnum.PAYLOADS) && allowPayloads == false) { return null; } } - if ((flags & PostingsEnum.FREQS) != 0 && maxAllowed.compareTo(IndexOptions.DOCS_AND_FREQS) < 0) { + if (PostingsEnum.featureRequested(flags, PostingsEnum.FREQS) && maxAllowed.compareTo(IndexOptions.DOCS_AND_FREQS) < 0) { return null; } return getSeedPostings(current.getKey().utf8ToString(), current.getValue().seed, false, maxAllowed, allowPayloads); diff --git a/lucene/test-framework/src/java/org/apache/lucene/util/TestUtil.java b/lucene/test-framework/src/java/org/apache/lucene/util/TestUtil.java index 0b243076e41..e152348e6e8 100644 --- a/lucene/test-framework/src/java/org/apache/lucene/util/TestUtil.java +++ b/lucene/test-framework/src/java/org/apache/lucene/util/TestUtil.java @@ -1017,7 +1017,7 @@ public final class TestUtil { case 0: posFlags = PostingsEnum.POSITIONS; break; case 1: posFlags = PostingsEnum.OFFSETS; break; case 2: posFlags = PostingsEnum.PAYLOADS; break; - default: posFlags = PostingsEnum.OFFSETS | PostingsEnum.PAYLOADS; break; + default: posFlags = PostingsEnum.ALL; break; } PostingsEnum docsAndPositions = termsEnum.postings(liveDocs, null, posFlags); if (docsAndPositions != null) {