Apache Lucene open-source search software
Added DOCS_AND_CUSTOM_FREQS whose 'length' is docFreq (#15584)
Michael Sokolov5158a52a082b
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex e27cb8a9a9c..a94ecc0bf15 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -261,6 +261,9 @@ API Changes* GITHUB#15803: Add ReaderUtil#partitionByLeaf to partition doc IDs fromScoreDoc hits by leaf reader. (Zihan Xu)+* GITHUB#15584: Add support for termdoc fields that use custom term freqs (via IndexOptions.DOCS_AND_CUSTOM_FREQS).+ IndexWriter counts their terms rather than summing their freqs. Use+New Features---------------------diff --git a/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene101/Lucene101PostingsReader.java b/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene101/Lucene101PostingsReader.javaindex 62f9b49f652..08df1b13eff 100644--- a/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene101/Lucene101PostingsReader.java+++ b/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene101/Lucene101PostingsReader.java@@ -227,12 +227,11 @@ public final class Lucene101PostingsReader extends PostingsReaderBase {termState.singletonDocID += BitUtil.zigZagDecode(l >>> 1);}- if (fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0) {+ if (fieldInfo.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS)) {termState.posStartFP += in.readVLong();if (fieldInfo- .getIndexOptions()- .compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS)- >= 0+ .getIndexOptions()+ .subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS)|| fieldInfo.hasPayloads()) {termState.payStartFP += in.readVLong();}@@ -403,10 +402,9 @@ public final class Lucene101PostingsReader extends PostingsReaderBase {public BlockPostingsEnum(FieldInfo fieldInfo, int flags, boolean needsImpacts)throws IOException {options = fieldInfo.getIndexOptions();- indexHasFreq = options.compareTo(IndexOptions.DOCS_AND_FREQS) >= 0;- indexHasPos = options.compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0;- indexHasOffsets =- options.compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS) >= 0;+ indexHasFreq = options.subsumes(IndexOptions.DOCS_AND_FREQS);+ indexHasPos = options.subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);+ indexHasOffsets = options.subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);indexHasPayloads = fieldInfo.hasPayloads();indexHasOffsetsOrPayloads = indexHasOffsets || indexHasPayloads;diff --git a/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene103/Lucene103PostingsReader.java b/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene103/Lucene103PostingsReader.javaindex f4d3beb1daf..617550ec6a1 100644--- a/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene103/Lucene103PostingsReader.java+++ b/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene103/Lucene103PostingsReader.java@@ -229,12 +229,11 @@ public final class Lucene103PostingsReader extends PostingsReaderBase {termState.singletonDocID += BitUtil.zigZagDecode(l >>> 1);}- if (fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0) {+ if (fieldInfo.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS)) {termState.posStartFP += in.readVLong();if (fieldInfo- .getIndexOptions()- .compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS)- >= 0+ .getIndexOptions()+ .subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS)|| fieldInfo.hasPayloads()) {termState.payStartFP += in.readVLong();}@@ -405,10 +404,9 @@ public final class Lucene103PostingsReader extends PostingsReaderBase {public BlockPostingsEnum(FieldInfo fieldInfo, int flags, boolean needsImpacts)throws IOException {options = fieldInfo.getIndexOptions();- indexHasFreq = options.compareTo(IndexOptions.DOCS_AND_FREQS) >= 0;- indexHasPos = options.compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0;- indexHasOffsets =- options.compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS) >= 0;+ indexHasFreq = options.subsumes(IndexOptions.DOCS_AND_FREQS);+ indexHasPos = options.subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);+ indexHasOffsets = options.subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);indexHasPayloads = fieldInfo.hasPayloads();indexHasOffsetsOrPayloads = indexHasOffsets || indexHasPayloads;diff --git a/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene90/Lucene90FieldInfosFormat.java b/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene90/Lucene90FieldInfosFormat.javaindex 65c7864fa9d..bd3f822666c 100644--- a/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene90/Lucene90FieldInfosFormat.java+++ b/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene90/Lucene90FieldInfosFormat.java@@ -270,7 +270,7 @@ public final class Lucene90FieldInfosFormat extends FieldInfosFormat {// We "mirror" IndexOptions enum values with the constants below; let's try to ensure if we add// a new IndexOption while this format is// still used for writing, we remember to fix this encoding:- assert IndexOptions.values().length == 5;+ assert IndexOptions.values().length == 6;}private static byte indexOptionsByte(IndexOptions indexOptions) {@@ -285,6 +285,8 @@ public final class Lucene90FieldInfosFormat extends FieldInfosFormat {return 3;case DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS:return 4;+ case DOCS_AND_CUSTOM_FREQS:+ return 5;default:// BUG:throw new AssertionError("unhandled IndexOptions: " + indexOptions);@@ -303,6 +305,8 @@ public final class Lucene90FieldInfosFormat extends FieldInfosFormat {return IndexOptions.DOCS_AND_FREQS_AND_POSITIONS;case 4:return IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS;+ case 5:+ return IndexOptions.DOCS_AND_CUSTOM_FREQS;default:// BUGthrow new CorruptIndexException("invalid IndexOptions byte: " + b, input);diff --git a/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene90/Lucene90PostingsReader.java b/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene90/Lucene90PostingsReader.javaindex d06c8010ed5..27345b94ada 100644--- a/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene90/Lucene90PostingsReader.java+++ b/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene90/Lucene90PostingsReader.java@@ -193,10 +193,9 @@ public final class Lucene90PostingsReader extends PostingsReaderBase {throws IOException {final IntBlockTermState termState = (IntBlockTermState) _termState;final boolean fieldHasPositions =- fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0;+ fieldInfo.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);final boolean fieldHasOffsets =- fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS)- >= 0;+ fieldInfo.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);final boolean fieldHasPayloads = fieldInfo.hasPayloads();if (absolute) {@@ -244,7 +243,7 @@ public final class Lucene90PostingsReader extends PostingsReaderBase {throws IOException {boolean indexHasPositions =- fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0;+ fieldInfo.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);if (indexHasPositions == false|| PostingsEnum.featureRequested(flags, PostingsEnum.POSITIONS) == false) {@@ -281,10 +280,9 @@ public final class Lucene90PostingsReader extends PostingsReaderBase {}final boolean indexHasPositions =- fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0;+ fieldInfo.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);final boolean indexHasOffsets =- fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS)- >= 0;+ fieldInfo.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);final boolean indexHasPayloads = fieldInfo.hasPayloads();if (indexHasPositions == false@@ -352,14 +350,12 @@ public final class Lucene90PostingsReader extends PostingsReaderBase {public BlockDocsEnum(FieldInfo fieldInfo) throws IOException {this.startDocIn = Lucene90PostingsReader.this.docIn;this.docIn = null;- indexHasFreq = fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS) >= 0;- indexHasPos =- fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0;+ indexHasFreq = fieldInfo.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS);+ indexHasPos = fieldInfo.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);indexHasOffsets =fieldInfo- .getIndexOptions()- .compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS)- >= 0;+ .getIndexOptions()+ .subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);indexHasPayloads = fieldInfo.hasPayloads();// We set the last element of docBuffer to NO_MORE_DOCS, it helps save conditionals in// advance()@@ -368,11 +364,9 @@ public final class Lucene90PostingsReader extends PostingsReaderBase {public boolean canReuse(IndexInput docIn, FieldInfo fieldInfo) {return docIn == startDocIn- && indexHasFreq- == (fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS) >= 0)+ && indexHasFreq == (fieldInfo.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS))&& indexHasPos- == (fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS)- >= 0)+ == (fieldInfo.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS))&& indexHasPayloads == fieldInfo.hasPayloads();}@@ -646,9 +640,8 @@ public final class Lucene90PostingsReader extends PostingsReaderBase {public EverythingEnum(FieldInfo fieldInfo) throws IOException {indexHasOffsets =fieldInfo- .getIndexOptions()- .compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS)- >= 0;+ .getIndexOptions()+ .subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);indexHasPayloads = fieldInfo.hasPayloads();this.startDocIn = Lucene90PostingsReader.this.docIn;@@ -688,9 +681,8 @@ public final class Lucene90PostingsReader extends PostingsReaderBase {return docIn == startDocIn&& indexHasOffsets== (fieldInfo- .getIndexOptions()- .compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS)- >= 0)+ .getIndexOptions()+ .subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS))&& indexHasPayloads == fieldInfo.hasPayloads();}@@ -1076,14 +1068,13 @@ public final class Lucene90PostingsReader extends PostingsReaderBase {public BlockImpactsDocsEnum(FieldInfo fieldInfo, IntBlockTermState termState)throws IOException {- indexHasFreqs = fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS) >= 0;+ indexHasFreqs = fieldInfo.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS);final boolean indexHasPositions =- fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0;+ fieldInfo.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);final boolean indexHasOffsets =fieldInfo- .getIndexOptions()- .compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS)- >= 0;+ .getIndexOptions()+ .subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);final boolean indexHasPayloads = fieldInfo.hasPayloads();this.docIn = Lucene90PostingsReader.this.docIn.clone();@@ -1306,9 +1297,8 @@ public final class Lucene90PostingsReader extends PostingsReaderBase {throws IOException {indexHasOffsets =fieldInfo- .getIndexOptions()- .compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS)- >= 0;+ .getIndexOptions()+ .subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);indexHasPayloads = fieldInfo.hasPayloads();this.docIn = Lucene90PostingsReader.this.docIn.clone();@@ -1623,14 +1613,12 @@ public final class Lucene90PostingsReader extends PostingsReaderBase {public BlockImpactsEverythingEnum(FieldInfo fieldInfo, IntBlockTermState termState, int flags)throws IOException {- indexHasFreq = fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS) >= 0;- indexHasPos =- fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0;+ indexHasFreq = fieldInfo.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS);+ indexHasPos = fieldInfo.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);indexHasOffsets =fieldInfo- .getIndexOptions()- .compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS)- >= 0;+ .getIndexOptions()+ .subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);indexHasPayloads = fieldInfo.hasPayloads();needsPositions = PostingsEnum.featureRequested(flags, PostingsEnum.POSITIONS);diff --git a/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene90/blocktree/FieldReader.java b/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene90/blocktree/FieldReader.javaindex 16eac747bf9..9675cb0311e 100644--- a/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene90/blocktree/FieldReader.java+++ b/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene90/blocktree/FieldReader.java@@ -163,20 +163,19 @@ public final class FieldReader extends Terms {@Overridepublic boolean hasFreqs() {- return fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS) >= 0;+ return fieldInfo.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS);}@Overridepublic boolean hasOffsets() {return fieldInfo- .getIndexOptions()- .compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS)- >= 0;+ .getIndexOptions()+ .subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);}@Overridepublic boolean hasPositions() {- return fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0;+ return fieldInfo.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);}@Overridediff --git a/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene912/Lucene912PostingsReader.java b/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene912/Lucene912PostingsReader.javaindex 77353ed63da..6945a67a20b 100644--- a/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene912/Lucene912PostingsReader.java+++ b/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene912/Lucene912PostingsReader.java@@ -237,12 +237,11 @@ public final class Lucene912PostingsReader extends PostingsReaderBase {termState.singletonDocID += BitUtil.zigZagDecode(l >>> 1);}- if (fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0) {+ if (fieldInfo.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS)) {termState.posStartFP += in.readVLong();if (fieldInfo- .getIndexOptions()- .compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS)- >= 0+ .getIndexOptions()+ .subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS)|| fieldInfo.hasPayloads()) {termState.payStartFP += in.readVLong();}@@ -258,7 +257,7 @@ public final class Lucene912PostingsReader extends PostingsReaderBase {public PostingsEnum postings(FieldInfo fieldInfo, BlockTermState termState, PostingsEnum reuse, int flags)throws IOException {- if (fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) < 0+ if (!fieldInfo.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS)|| PostingsEnum.featureRequested(flags, PostingsEnum.POSITIONS) == false) {return (reuse instanceof BlockDocsEnum blockDocsEnum&& blockDocsEnum.canReuse(docIn, fieldInfo)@@ -278,18 +277,17 @@ public final class Lucene912PostingsReader extends PostingsReaderBase {public ImpactsEnum impacts(FieldInfo fieldInfo, BlockTermState state, int flags)throws IOException {final IndexOptions options = fieldInfo.getIndexOptions();- final boolean indexHasPositions =- options.compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0;+ final boolean indexHasPositions = options.subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);if (state.docFreq >= BLOCK_SIZE) {- if (options.compareTo(IndexOptions.DOCS_AND_FREQS) >= 0+ if (options.subsumes(IndexOptions.DOCS_AND_FREQS)&& (indexHasPositions == false|| PostingsEnum.featureRequested(flags, PostingsEnum.POSITIONS) == false)) {return new BlockImpactsDocsEnum(indexHasPositions, (IntBlockTermState) state);}if (indexHasPositions- && (options.compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS) < 0+ && (!options.subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS)|| PostingsEnum.featureRequested(flags, PostingsEnum.OFFSETS) == false)&& (fieldInfo.hasPayloads() == false|| PostingsEnum.featureRequested(flags, PostingsEnum.PAYLOADS) == false)) {@@ -341,7 +339,7 @@ public final class Lucene912PostingsReader extends PostingsReaderBase {protected IndexInput docIn;protected AbstractPostingsEnum(FieldInfo fieldInfo) {- indexHasFreq = fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS) >= 0;+ indexHasFreq = fieldInfo.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS);// We set the last element of docBuffer to NO_MORE_DOCS, it helps save conditionals in// advance()docBuffer[BLOCK_SIZE] = NO_MORE_DOCS;@@ -399,7 +397,7 @@ public final class Lucene912PostingsReader extends PostingsReaderBase {public boolean canReuse(IndexInput docIn, FieldInfo fieldInfo) {final IndexOptions options = fieldInfo.getIndexOptions();return docIn == Lucene912PostingsReader.this.docIn- && indexHasFreq == (options.compareTo(IndexOptions.DOCS_AND_FREQS) >= 0);+ && indexHasFreq == (options.subsumes(IndexOptions.DOCS_AND_FREQS));}public PostingsEnum reset(IntBlockTermState termState, int flags) throws IOException {@@ -651,9 +649,8 @@ public final class Lucene912PostingsReader extends PostingsReaderBase {super(fieldInfo);indexHasOffsets =fieldInfo- .getIndexOptions()- .compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS)- >= 0;+ .getIndexOptions()+ .subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);indexHasPayloads = fieldInfo.hasPayloads();indexHasOffsetsOrPayloads = indexHasOffsets || indexHasPayloads;@@ -688,9 +685,8 @@ public final class Lucene912PostingsReader extends PostingsReaderBase {return docIn == Lucene912PostingsReader.this.docIn&& indexHasOffsets== (fieldInfo- .getIndexOptions()- .compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS)- >= 0)+ .getIndexOptions()+ .subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS))&& indexHasPayloads == fieldInfo.hasPayloads();}@@ -1465,9 +1461,8 @@ public final class Lucene912PostingsReader extends PostingsReaderBase {throws IOException {super(termState);final IndexOptions options = fieldInfo.getIndexOptions();- indexHasFreq = options.compareTo(IndexOptions.DOCS_AND_FREQS) >= 0;- indexHasOffsets =- options.compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS) >= 0;+ indexHasFreq = options.subsumes(IndexOptions.DOCS_AND_FREQS);+ indexHasOffsets = options.subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);indexHasPayloads = fieldInfo.hasPayloads();indexHasOffsetsOrPayloads = indexHasOffsets || indexHasPayloads;diff --git a/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene99/Lucene99PostingsReader.java b/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene99/Lucene99PostingsReader.javaindex b17ceee4cb0..1cfdeed2884 100644--- a/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene99/Lucene99PostingsReader.java+++ b/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene99/Lucene99PostingsReader.java@@ -176,10 +176,9 @@ public final class Lucene99PostingsReader extends PostingsReaderBase {throws IOException {final IntBlockTermState termState = (IntBlockTermState) _termState;final boolean fieldHasPositions =- fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0;+ fieldInfo.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);final boolean fieldHasOffsets =- fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS)- >= 0;+ fieldInfo.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);final boolean fieldHasPayloads = fieldInfo.hasPayloads();if (absolute) {@@ -227,7 +226,7 @@ public final class Lucene99PostingsReader extends PostingsReaderBase {throws IOException {boolean indexHasPositions =- fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0;+ fieldInfo.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);if (indexHasPositions == false|| PostingsEnum.featureRequested(flags, PostingsEnum.POSITIONS) == false) {@@ -264,10 +263,9 @@ public final class Lucene99PostingsReader extends PostingsReaderBase {}final boolean indexHasPositions =- fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0;+ fieldInfo.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);final boolean indexHasOffsets =- fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS)- >= 0;+ fieldInfo.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);final boolean indexHasPayloads = fieldInfo.hasPayloads();if (indexHasPositions == false@@ -338,14 +336,12 @@ public final class Lucene99PostingsReader extends PostingsReaderBase {public BlockDocsEnum(FieldInfo fieldInfo) throws IOException {this.startDocIn = Lucene99PostingsReader.this.docIn;this.docIn = null;- indexHasFreq = fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS) >= 0;- indexHasPos =- fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0;+ indexHasFreq = fieldInfo.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS);+ indexHasPos = fieldInfo.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);indexHasOffsets =fieldInfo- .getIndexOptions()- .compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS)- >= 0;+ .getIndexOptions()+ .subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);indexHasPayloads = fieldInfo.hasPayloads();// We set the last element of docBuffer to NO_MORE_DOCS, it helps save conditionals in// advance()@@ -354,11 +350,9 @@ public final class Lucene99PostingsReader extends PostingsReaderBase {public boolean canReuse(IndexInput docIn, FieldInfo fieldInfo) {return docIn == startDocIn- && indexHasFreq- == (fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS) >= 0)+ && indexHasFreq == (fieldInfo.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS))&& indexHasPos- == (fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS)- >= 0)+ == (fieldInfo.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS))&& indexHasPayloads == fieldInfo.hasPayloads();}@@ -644,9 +638,8 @@ public final class Lucene99PostingsReader extends PostingsReaderBase {public EverythingEnum(FieldInfo fieldInfo) throws IOException {indexHasOffsets =fieldInfo- .getIndexOptions()- .compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS)- >= 0;+ .getIndexOptions()+ .subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);indexHasPayloads = fieldInfo.hasPayloads();this.startDocIn = Lucene99PostingsReader.this.docIn;@@ -686,9 +679,8 @@ public final class Lucene99PostingsReader extends PostingsReaderBase {return docIn == startDocIn&& indexHasOffsets== (fieldInfo- .getIndexOptions()- .compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS)- >= 0)+ .getIndexOptions()+ .subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS))&& indexHasPayloads == fieldInfo.hasPayloads();}@@ -1082,14 +1074,13 @@ public final class Lucene99PostingsReader extends PostingsReaderBase {public BlockImpactsDocsEnum(FieldInfo fieldInfo, IntBlockTermState termState)throws IOException {- indexHasFreqs = fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS) >= 0;+ indexHasFreqs = fieldInfo.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS);final boolean indexHasPositions =- fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0;+ fieldInfo.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);final boolean indexHasOffsets =fieldInfo- .getIndexOptions()- .compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS)- >= 0;+ .getIndexOptions()+ .subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);final boolean indexHasPayloads = fieldInfo.hasPayloads();this.docIn = Lucene99PostingsReader.this.docIn.clone();@@ -1304,9 +1295,8 @@ public final class Lucene99PostingsReader extends PostingsReaderBase {throws IOException {indexHasOffsets =fieldInfo- .getIndexOptions()- .compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS)- >= 0;+ .getIndexOptions()+ .subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);indexHasPayloads = fieldInfo.hasPayloads();this.docIn = Lucene99PostingsReader.this.docIn.clone();@@ -1620,14 +1610,12 @@ public final class Lucene99PostingsReader extends PostingsReaderBase {public BlockImpactsEverythingEnum(FieldInfo fieldInfo, IntBlockTermState termState, int flags)throws IOException {- indexHasFreq = fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS) >= 0;- indexHasPos =- fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0;+ indexHasFreq = fieldInfo.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS);+ indexHasPos = fieldInfo.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);indexHasOffsets =fieldInfo- .getIndexOptions()- .compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS)- >= 0;+ .getIndexOptions()+ .subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);indexHasPayloads = fieldInfo.hasPayloads();needsPositions = PostingsEnum.featureRequested(flags, PostingsEnum.POSITIONS);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.javaindex 91d18ff9250..c14f977916c 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@@ -256,20 +256,19 @@ public class BlockTermsReader extends FieldsProducer {@Overridepublic boolean hasFreqs() {- return fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS) >= 0;+ return fieldInfo.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS);}@Overridepublic boolean hasOffsets() {return fieldInfo- .getIndexOptions()- .compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS)- >= 0;+ .getIndexOptions()+ .subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);}@Overridepublic boolean hasPositions() {- return fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0;+ return fieldInfo.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);}@Overridediff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/blockterms/BlockTermsWriter.java b/lucene/codecs/src/java/org/apache/lucene/codecs/blockterms/BlockTermsWriter.javaindex 3a7dacaa527..aa19f833767 100644--- a/lucene/codecs/src/java/org/apache/lucene/codecs/blockterms/BlockTermsWriter.java+++ b/lucene/codecs/src/java/org/apache/lucene/codecs/blockterms/BlockTermsWriter.java@@ -272,7 +272,7 @@ public class BlockTermsWriter extends FieldsConsumer {fieldInfo,numTerms,termsStartPointer,- fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS) >= 0+ fieldInfo.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS)? sumTotalTermFreq: -1,sumDocFreq,diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/blocktreeords/OrdsFieldReader.java b/lucene/codecs/src/java/org/apache/lucene/codecs/blocktreeords/OrdsFieldReader.javaindex 5f83f9e12a5..cd53971ec0e 100644--- a/lucene/codecs/src/java/org/apache/lucene/codecs/blocktreeords/OrdsFieldReader.java+++ b/lucene/codecs/src/java/org/apache/lucene/codecs/blocktreeords/OrdsFieldReader.java@@ -126,20 +126,19 @@ final class OrdsFieldReader extends Terms {@Overridepublic boolean hasFreqs() {- return fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS) >= 0;+ return fieldInfo.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS);}@Overridepublic boolean hasOffsets() {return fieldInfo- .getIndexOptions()- .compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS)- >= 0;+ .getIndexOptions()+ .subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);}@Overridepublic boolean hasPositions() {- return fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0;+ return fieldInfo.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);}@Overridediff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/memory/DirectPostingsFormat.java b/lucene/codecs/src/java/org/apache/lucene/codecs/memory/DirectPostingsFormat.javaindex 054f3c2d080..24b6fe188be 100644--- a/lucene/codecs/src/java/org/apache/lucene/codecs/memory/DirectPostingsFormat.java+++ b/lucene/codecs/src/java/org/apache/lucene/codecs/memory/DirectPostingsFormat.java@@ -303,10 +303,12 @@ public final class DirectPostingsFormat extends PostingsFormat {this.minSkipCount = minSkipCount;- hasFreq = fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS) > 0;- hasPos = fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS) > 0;+ hasFreq = fieldInfo.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS);+ hasPos = fieldInfo.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);hasOffsets =- fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) > 0;+ fieldInfo+ .getIndexOptions()+ .subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);hasPayloads = fieldInfo.hasPayloads();BytesRef term;diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/memory/FSTTermsReader.java b/lucene/codecs/src/java/org/apache/lucene/codecs/memory/FSTTermsReader.javaindex 943298ffbf9..d95336ef9b5 100644--- a/lucene/codecs/src/java/org/apache/lucene/codecs/memory/FSTTermsReader.java+++ b/lucene/codecs/src/java/org/apache/lucene/codecs/memory/FSTTermsReader.java@@ -215,20 +215,19 @@ public class FSTTermsReader extends FieldsProducer {@Overridepublic boolean hasFreqs() {- return fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS) >= 0;+ return fieldInfo.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS);}@Overridepublic boolean hasOffsets() {return fieldInfo- .getIndexOptions()- .compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS)- >= 0;+ .getIndexOptions()+ .subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);}@Overridepublic boolean hasPositions() {- return fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0;+ return fieldInfo.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);}@Overridediff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/memory/FSTTermsWriter.java b/lucene/codecs/src/java/org/apache/lucene/codecs/memory/FSTTermsWriter.javaindex 73d864d3579..2ca771526f7 100644--- a/lucene/codecs/src/java/org/apache/lucene/codecs/memory/FSTTermsWriter.java+++ b/lucene/codecs/src/java/org/apache/lucene/codecs/memory/FSTTermsWriter.java@@ -156,7 +156,7 @@ public class FSTTermsWriter extends FieldsConsumer {continue;}FieldInfo fieldInfo = fieldInfos.fieldInfo(field);- boolean hasFreq = fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS) >= 0;+ boolean hasFreq = fieldInfo.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS);TermsEnum termsEnum = terms.iterator();TermsWriter termsWriter = new TermsWriter(fieldInfo);diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextFieldInfosFormat.java b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextFieldInfosFormat.javaindex ee8c111e5b8..2286a229b04 100644--- a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextFieldInfosFormat.java+++ b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextFieldInfosFormat.java@@ -254,7 +254,7 @@ public class SimpleTextFieldInfosFormat extends FieldInfosFormat {SimpleTextUtil.write(out, INDEXOPTIONS);IndexOptions indexOptions = fi.getIndexOptions();- assert indexOptions.compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0+ assert indexOptions.subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS)|| !fi.hasPayloads();SimpleTextUtil.write(out, indexOptions.toString(), scratch);SimpleTextUtil.writeNewline(out);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.javaindex e7e4bc0b0f4..3ad520df477 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@@ -229,7 +229,7 @@ class SimpleTextFieldsReader extends FieldsProducer {@Overridepublic PostingsEnum postings(PostingsEnum reuse, int flags) throws IOException {- boolean hasPositions = indexOptions.compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0;+ boolean hasPositions = indexOptions.subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);if (hasPositions && PostingsEnum.featureRequested(flags, PostingsEnum.POSITIONS)) {SimpleTextPostingsEnum docsAndPositionsEnum;@@ -467,9 +467,8 @@ class SimpleTextFieldsReader extends FieldsProducer {long fp, IndexOptions indexOptions, int docFreq, long skipPointer) throws IOException {nextDocStart = fp;docID = -1;- readPositions = indexOptions.compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0;- readOffsets =- indexOptions.compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS) >= 0;+ readPositions = indexOptions.subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);+ readOffsets = indexOptions.subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);if (!readOffsets) {startOffset = -1;endOffset = -1;@@ -780,20 +779,19 @@ class SimpleTextFieldsReader extends FieldsProducer {@Overridepublic boolean hasFreqs() {- return fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS) >= 0;+ return fieldInfo.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS);}@Overridepublic boolean hasOffsets() {return fieldInfo- .getIndexOptions()- .compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS)- >= 0;+ .getIndexOptions()+ .subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);}@Overridepublic boolean hasPositions() {- return fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0;+ return fieldInfo.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);}@Overridediff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/uniformsplit/DeltaBaseTermStateSerializer.java b/lucene/codecs/src/java/org/apache/lucene/codecs/uniformsplit/DeltaBaseTermStateSerializer.javaindex af15ab034ee..d55da01b62d 100644--- a/lucene/codecs/src/java/org/apache/lucene/codecs/uniformsplit/DeltaBaseTermStateSerializer.java+++ b/lucene/codecs/src/java/org/apache/lucene/codecs/uniformsplit/DeltaBaseTermStateSerializer.java@@ -103,9 +103,9 @@ public class DeltaBaseTermStateSerializer implements Accountable {throws IOException {IndexOptions indexOptions = fieldInfo.getIndexOptions();boolean hasFreqs = indexOptions != IndexOptions.DOCS;- boolean hasPositions = indexOptions.compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0;+ boolean hasPositions = indexOptions.subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);boolean hasOffsets =- indexOptions.compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS) >= 0;+ indexOptions.subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);boolean hasPayloads = fieldInfo.hasPayloads();IntBlockTermState intTermState = (IntBlockTermState) termState;@@ -160,7 +160,7 @@ public class DeltaBaseTermStateSerializer implements Accountable {throws IOException {IndexOptions indexOptions = fieldInfo.getIndexOptions();boolean hasFreqs = indexOptions != IndexOptions.DOCS;- boolean hasPositions = indexOptions.compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0;+ boolean hasPositions = indexOptions.subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);IntBlockTermState intTermState =reuse != null ? reset((IntBlockTermState) reuse) : new IntBlockTermState();@@ -179,7 +179,7 @@ public class DeltaBaseTermStateSerializer implements Accountable {if (hasPositions) {intTermState.posStartFP = basePosStartFP + termStatesInput.readVLong();boolean hasOffsets =- indexOptions.compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS) >= 0;+ indexOptions.subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);if (hasOffsets || fieldInfo.hasPayloads()) {intTermState.payStartFP = basePayStartFP + termStatesInput.readVLong();}diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/uniformsplit/FieldMetadata.java b/lucene/codecs/src/java/org/apache/lucene/codecs/uniformsplit/FieldMetadata.javaindex 478e39fbf3b..716b41a50f7 100644--- a/lucene/codecs/src/java/org/apache/lucene/codecs/uniformsplit/FieldMetadata.java+++ b/lucene/codecs/src/java/org/apache/lucene/codecs/uniformsplit/FieldMetadata.java@@ -211,7 +211,7 @@ public class FieldMetadata {output.writeVLong(fieldMetadata.numTerms);output.writeVLong(fieldMetadata.sumDocFreq);- if (fieldMetadata.fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS) >= 0) {+ if (fieldMetadata.fieldInfo.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS)) {assert fieldMetadata.sumTotalTermFreq >= fieldMetadata.sumDocFreq: "sumTotalFQ: "+ fieldMetadata.sumTotalTermFreq@@ -254,7 +254,7 @@ public class FieldMetadata {fieldMetadata.sumDocFreq = input.readVLong();fieldMetadata.sumTotalTermFreq = fieldMetadata.sumDocFreq;- if (fieldMetadata.fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS) >= 0) {+ if (fieldMetadata.fieldInfo.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS)) {fieldMetadata.sumTotalTermFreq += input.readVLong();if (fieldMetadata.sumTotalTermFreq < fieldMetadata.sumDocFreq) {// #positions must be >= #postings.diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/uniformsplit/UniformSplitTerms.java b/lucene/codecs/src/java/org/apache/lucene/codecs/uniformsplit/UniformSplitTerms.javaindex a7af58a5750..82892cff467 100644--- a/lucene/codecs/src/java/org/apache/lucene/codecs/uniformsplit/UniformSplitTerms.java+++ b/lucene/codecs/src/java/org/apache/lucene/codecs/uniformsplit/UniformSplitTerms.java@@ -118,26 +118,23 @@ public class UniformSplitTerms extends Terms {@Overridepublic boolean hasFreqs() {- return fieldMetadata.getFieldInfo().getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS)- >= 0;+ return fieldMetadata.getFieldInfo().getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS);}@Overridepublic boolean hasOffsets() {return fieldMetadata- .getFieldInfo()- .getIndexOptions()- .compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS)- >= 0;+ .getFieldInfo()+ .getIndexOptions()+ .subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);}@Overridepublic boolean hasPositions() {return fieldMetadata- .getFieldInfo()- .getIndexOptions()- .compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS)- >= 0;+ .getFieldInfo()+ .getIndexOptions()+ .subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);}@Overridediff --git a/lucene/core/src/java/org/apache/lucene/codecs/PushPostingsWriterBase.java b/lucene/core/src/java/org/apache/lucene/codecs/PushPostingsWriterBase.javaindex 1f1b050ccc6..f8ab8db0c5e 100644--- a/lucene/core/src/java/org/apache/lucene/codecs/PushPostingsWriterBase.java+++ b/lucene/core/src/java/org/apache/lucene/codecs/PushPostingsWriterBase.java@@ -87,10 +87,9 @@ public abstract class PushPostingsWriterBase extends PostingsWriterBase {this.fieldInfo = fieldInfo;indexOptions = fieldInfo.getIndexOptions();- writeFreqs = indexOptions.compareTo(IndexOptions.DOCS_AND_FREQS) >= 0;- writePositions = indexOptions.compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0;- writeOffsets =- indexOptions.compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS) >= 0;+ writeFreqs = indexOptions.subsumes(IndexOptions.DOCS_AND_FREQS);+ writePositions = indexOptions.subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);+ writeOffsets = indexOptions.subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);writePayloads = fieldInfo.hasPayloads();if (writeFreqs == false) {diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene103/blocktree/FieldReader.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene103/blocktree/FieldReader.javaindex ceacb9507f2..ba2e9f7f66e 100644--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene103/blocktree/FieldReader.java+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene103/blocktree/FieldReader.java@@ -115,20 +115,19 @@ public final class FieldReader extends Terms {@Overridepublic boolean hasFreqs() {- return fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS) >= 0;+ return fieldInfo.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS);}@Overridepublic boolean hasOffsets() {return fieldInfo- .getIndexOptions()- .compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS)- >= 0;+ .getIndexOptions()+ .subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);}@Overridepublic boolean hasPositions() {- return fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0;+ return fieldInfo.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);}@Overridediff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene104/Lucene104PostingsReader.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene104/Lucene104PostingsReader.javaindex 409ff4f8a3a..eafe644c9b2 100644--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene104/Lucene104PostingsReader.java+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene104/Lucene104PostingsReader.java@@ -233,12 +233,11 @@ public final class Lucene104PostingsReader extends PostingsReaderBase {termState.singletonDocID += BitUtil.zigZagDecode(l >>> 1);}- if (fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0) {+ if (fieldInfo.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS)) {termState.posStartFP += in.readVLong();if (fieldInfo- .getIndexOptions()- .compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS)- >= 0+ .getIndexOptions()+ .subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS)|| fieldInfo.hasPayloads()) {termState.payStartFP += in.readVLong();}@@ -409,10 +408,9 @@ public final class Lucene104PostingsReader extends PostingsReaderBase {public BlockPostingsEnum(FieldInfo fieldInfo, int flags, boolean needsImpacts)throws IOException {options = fieldInfo.getIndexOptions();- indexHasFreq = options.compareTo(IndexOptions.DOCS_AND_FREQS) >= 0;- indexHasPos = options.compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0;- indexHasOffsets =- options.compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS) >= 0;+ indexHasFreq = options.subsumes(IndexOptions.DOCS_AND_FREQS);+ indexHasPos = options.subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);+ indexHasOffsets = options.subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);indexHasPayloads = fieldInfo.hasPayloads();indexHasOffsetsOrPayloads = indexHasOffsets || indexHasPayloads;diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene94/Lucene94FieldInfosFormat.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene94/Lucene94FieldInfosFormat.javaindex c4cdd722c54..acd4bf55152 100644--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene94/Lucene94FieldInfosFormat.java+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene94/Lucene94FieldInfosFormat.java@@ -364,7 +364,7 @@ public final class Lucene94FieldInfosFormat extends FieldInfosFormat {// We "mirror" IndexOptions enum values with the constants below; let's try to ensure if we add// a new IndexOption while this format is// still used for writing, we remember to fix this encoding:- assert IndexOptions.values().length == 5;+ assert IndexOptions.values().length == 6;}private static byte indexOptionsByte(IndexOptions indexOptions) {@@ -379,6 +379,8 @@ public final class Lucene94FieldInfosFormat extends FieldInfosFormat {return 3;case DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS:return 4;+ case DOCS_AND_CUSTOM_FREQS:+ return 5;default:// BUG:throw new AssertionError("unhandled IndexOptions: " + indexOptions);@@ -397,6 +399,8 @@ public final class Lucene94FieldInfosFormat extends FieldInfosFormat {return IndexOptions.DOCS_AND_FREQS_AND_POSITIONS;case 4:return IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS;+ case 5:+ return IndexOptions.DOCS_AND_CUSTOM_FREQS;default:// BUGthrow new CorruptIndexException("invalid IndexOptions byte: " + b, input);diff --git a/lucene/core/src/java/org/apache/lucene/codecs/perfield/PerFieldMergeState.java b/lucene/core/src/java/org/apache/lucene/codecs/perfield/PerFieldMergeState.javaindex 15e65091aed..9d8e1fb597d 100644--- a/lucene/core/src/java/org/apache/lucene/codecs/perfield/PerFieldMergeState.java+++ b/lucene/core/src/java/org/apache/lucene/codecs/perfield/PerFieldMergeState.java@@ -109,11 +109,10 @@ final class PerFieldMergeState {this.filtered.add(fi);hasVectors |= fi.hasTermVectors();hasPostings |= fi.getIndexOptions() != IndexOptions.NONE;- hasProx |= fi.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0;+ hasProx |= fi.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);hasFreq |= fi.getIndexOptions() != IndexOptions.DOCS;hasOffsets |=- fi.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS)- >= 0;+ fi.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);hasNorms |= fi.hasNorms();hasDocValues |= fi.getDocValuesType() != DocValuesType.NONE;hasPayloads |= fi.hasPayloads();diff --git a/lucene/core/src/java/org/apache/lucene/document/Field.java b/lucene/core/src/java/org/apache/lucene/document/Field.javaindex 8017627a53c..18b629fa8fe 100644--- a/lucene/core/src/java/org/apache/lucene/document/Field.java+++ b/lucene/core/src/java/org/apache/lucene/document/Field.java@@ -206,7 +206,7 @@ public class Field implements IndexableField {if (type == null) {throw new IllegalArgumentException("type must not be null");}- if (type.indexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS) >= 0+ if (type.indexOptions().subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS)|| type.storeTermVectorOffsets()) {throw new IllegalArgumentException("It doesn't make sense to index offsets on binary fields");}diff --git a/lucene/core/src/java/org/apache/lucene/index/CheckIndex.java b/lucene/core/src/java/org/apache/lucene/index/CheckIndex.javaindex dc324272263..e9c46b1c48e 100644--- a/lucene/core/src/java/org/apache/lucene/index/CheckIndex.java+++ b/lucene/core/src/java/org/apache/lucene/index/CheckIndex.java@@ -1558,7 +1558,7 @@ public final class CheckIndex implements Closeable {// term vectors cannot omit TF:final boolean expectedHasFreqs =- (isVectors || fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS) >= 0);+ (isVectors || fieldInfo.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS));if (hasFreqs != expectedHasFreqs) {throw new CheckIndexException(@@ -1572,7 +1572,7 @@ public final class CheckIndex implements Closeable {if (isVectors == false) {final boolean expectedHasPositions =- fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0;+ fieldInfo.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);if (hasPositions != expectedHasPositions) {throw new CheckIndexException("field \""@@ -1596,9 +1596,8 @@ public final class CheckIndex implements Closeable {final boolean expectedHasOffsets =fieldInfo- .getIndexOptions()- .compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS)- >= 0;+ .getIndexOptions()+ .subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);if (hasOffsets != expectedHasOffsets) {throw new CheckIndexException("field \""@@ -4226,7 +4225,7 @@ public final class CheckIndex implements Closeable {Terms terms = tfv.terms(field);TermsEnum termsEnum = terms.iterator();final boolean postingsHasFreq =- fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS) >= 0;+ fieldInfo.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS);final boolean postingsHasPayload = fieldInfo.hasPayloads();final boolean vectorsHasPayload = terms.hasPayloads();diff --git a/lucene/core/src/java/org/apache/lucene/index/FieldInfo.java b/lucene/core/src/java/org/apache/lucene/index/FieldInfo.javaindex e87bd2e8c16..15a4aac4ad6 100644--- a/lucene/core/src/java/org/apache/lucene/index/FieldInfo.java+++ b/lucene/core/src/java/org/apache/lucene/index/FieldInfo.java@@ -136,7 +136,7 @@ public final class FieldInfo {}if (indexOptions != IndexOptions.NONE) {// Cannot store payloads unless positions are indexed:- if (indexOptions.compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) < 0 && storePayloads) {+ if (!indexOptions.subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) && storePayloads) {throw new IllegalArgumentException("indexed field '" + name + "' cannot have payloads without positions");}@@ -614,7 +614,7 @@ public final class FieldInfo {}void setStorePayloads() {- if (indexOptions.compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0) {+ if (indexOptions.subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS)) {storePayloads = true;}this.checkConsistency();@@ -713,4 +713,12 @@ public final class FieldInfo {public boolean isParentField() {return isParentField;}++ /**+ * Returns true if the field has custom term frequencies indexed that are to be treated as opaque+ * values, while the term frequency for statistical and scoring purposes is treated as 1.+ */+ public boolean isTermDocField() {+ return indexOptions == IndexOptions.DOCS_AND_CUSTOM_FREQS;+ }}diff --git a/lucene/core/src/java/org/apache/lucene/index/FieldInfos.java b/lucene/core/src/java/org/apache/lucene/index/FieldInfos.javaindex df129c73af4..5bf4bbca524 100644--- a/lucene/core/src/java/org/apache/lucene/index/FieldInfos.java+++ b/lucene/core/src/java/org/apache/lucene/index/FieldInfos.java@@ -112,11 +112,10 @@ public class FieldInfos implements Iterable<FieldInfo> {hasTermVectors |= info.hasTermVectors();hasPostings |= info.getIndexOptions() != IndexOptions.NONE;- hasProx |= info.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0;+ hasProx |= info.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);hasFreq |= info.getIndexOptions() != IndexOptions.DOCS;hasOffsets |=- info.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS)- >= 0;+ info.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);hasNorms |= info.hasNorms();hasDocValues |= info.getDocValuesType() != DocValuesType.NONE;hasPayloads |= info.hasPayloads();diff --git a/lucene/core/src/java/org/apache/lucene/index/FreqProxFields.java b/lucene/core/src/java/org/apache/lucene/index/FreqProxFields.javaindex 3878765addc..11dd1d674f7 100644--- a/lucene/core/src/java/org/apache/lucene/index/FreqProxFields.java+++ b/lucene/core/src/java/org/apache/lucene/index/FreqProxFields.java@@ -93,7 +93,7 @@ class FreqProxFields extends Fields {@Overridepublic boolean hasFreqs() {- return terms.indexOptions.compareTo(IndexOptions.DOCS_AND_FREQS) >= 0;+ return terms.indexOptions.subsumes(IndexOptions.DOCS_AND_FREQS);}@Override@@ -101,8 +101,7 @@ class FreqProxFields extends Fields {// NOTE: the in-memory buffer may have indexed offsets// because that's what FieldInfo said when we started,// but during indexing this may have been downgraded:- return terms.indexOptions.compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS)- >= 0;+ return terms.indexOptions.subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);}@Override@@ -110,7 +109,7 @@ class FreqProxFields extends Fields {// NOTE: the in-memory buffer may have indexed positions// because that's what FieldInfo said when we started,// but during indexing this may have been downgraded:- return terms.indexOptions.compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0;+ return terms.indexOptions.subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);}@Overridediff --git a/lucene/core/src/java/org/apache/lucene/index/FreqProxTermsWriter.java b/lucene/core/src/java/org/apache/lucene/index/FreqProxTermsWriter.javaindex d3200d757de..34c18a5f98d 100644--- a/lucene/core/src/java/org/apache/lucene/index/FreqProxTermsWriter.java+++ b/lucene/core/src/java/org/apache/lucene/index/FreqProxTermsWriter.java@@ -176,7 +176,7 @@ final class FreqProxTermsWriter extends TermsHash {@Overridepublic PostingsEnum postings(PostingsEnum reuse, final int flags) throws IOException {- if (indexOptions.compareTo(IndexOptions.DOCS_AND_FREQS) >= 0+ if (indexOptions.subsumes(IndexOptions.DOCS_AND_FREQS)&& PostingsEnum.featureRequested(flags, PostingsEnum.FREQS)) {final PostingsEnum inReuse;final SortingPostingsEnum wrapReuse;@@ -196,9 +196,9 @@ final class FreqProxTermsWriter extends TermsHash {// ask for everything. if that assumption changes in the future, we can// factor in whether 'flags' says offsets are not required.final boolean storePositions =- indexOptions.compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0;+ indexOptions.subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);final boolean storeOffsets =- indexOptions.compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS) >= 0;+ indexOptions.subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);wrapReuse.reset(docMap, inDocsAndPositions, storePositions, storeOffsets);return wrapReuse;}diff --git a/lucene/core/src/java/org/apache/lucene/index/FreqProxTermsWriterPerField.java b/lucene/core/src/java/org/apache/lucene/index/FreqProxTermsWriterPerField.javaindex 04d949f5ef9..4235ff4e942 100644--- a/lucene/core/src/java/org/apache/lucene/index/FreqProxTermsWriterPerField.java+++ b/lucene/core/src/java/org/apache/lucene/index/FreqProxTermsWriterPerField.java@@ -34,6 +34,7 @@ final class FreqProxTermsWriterPerField extends TermsHashPerField {final boolean hasFreq;final boolean hasProx;final boolean hasOffsets;+ final boolean isTermDoc;PayloadAttribute payloadAttribute;OffsetAttribute offsetAttribute;TermFrequencyAttribute termFreqAtt;@@ -47,9 +48,7 @@ final class FreqProxTermsWriterPerField extends TermsHashPerField {FieldInfo fieldInfo,TermsHashPerField nextPerField) {super(- fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0- ? 2- : 1,+ fieldInfo.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) ? 2 : 1,termsHash.intPool,termsHash.bytePool,termsHash.termBytePool,@@ -59,9 +58,10 @@ final class FreqProxTermsWriterPerField extends TermsHashPerField {fieldInfo.getIndexOptions());this.fieldState = invertState;this.fieldInfo = fieldInfo;- hasFreq = indexOptions.compareTo(IndexOptions.DOCS_AND_FREQS) >= 0;- hasProx = indexOptions.compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0;- hasOffsets = indexOptions.compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS) >= 0;+ hasFreq = indexOptions.subsumes(IndexOptions.DOCS_AND_FREQS);+ hasProx = indexOptions.subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);+ hasOffsets = indexOptions.subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);+ isTermDoc = fieldInfo.isTermDocField();}@Override@@ -190,6 +190,9 @@ final class FreqProxTermsWriterPerField extends TermsHashPerField {}fieldState.uniqueTermCount++;} else {+ if (isTermDoc) {+ throw new DuplicateTermException("field '" + getFieldName() + "' has duplicate term");+ }postings.termFreqs[termID] = Math.addExact(postings.termFreqs[termID], getTermFreq());fieldState.maxTermFrequency =Math.max(fieldState.maxTermFrequency, postings.termFreqs[termID]);@@ -212,7 +215,6 @@ final class FreqProxTermsWriterPerField extends TermsHashPerField {+ "\": cannot index positions while using custom TermFrequencyAttribute");}}-return freq;}@@ -223,10 +225,10 @@ final class FreqProxTermsWriterPerField extends TermsHashPerField {@OverrideParallelPostingsArray createPostingsArray(int size) {- boolean hasFreq = indexOptions.compareTo(IndexOptions.DOCS_AND_FREQS) >= 0;- boolean hasProx = indexOptions.compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0;+ boolean hasFreq = indexOptions.subsumes(IndexOptions.DOCS_AND_FREQS);+ boolean hasProx = indexOptions.subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);boolean hasOffsets =- indexOptions.compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS) >= 0;+ indexOptions.subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);return new FreqProxPostingsArray(size, hasFreq, hasProx, hasOffsets);}diff --git a/lucene/core/src/java/org/apache/lucene/index/IndexOptions.java b/lucene/core/src/java/org/apache/lucene/index/IndexOptions.javaindex 911fbbaec6c..1ad28d1eafa 100644--- a/lucene/core/src/java/org/apache/lucene/index/IndexOptions.java+++ b/lucene/core/src/java/org/apache/lucene/index/IndexOptions.java@@ -25,6 +25,7 @@ import org.apache.lucene.search.PhraseQuery;*/public enum IndexOptions {// NOTE: order is important here; enum ordinals can be used to compare options.+/** Not indexed */NONE,/**@@ -48,4 +49,26 @@ public enum IndexOptions {* the positions.*/DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS,+ /**+ * Like DOCS_AND_FREQS except the "custom freqs" are treated as scores and do not represent term+ * counts. Each term can only occur once per document, so its frequency in that sense is always 1.+ */+ DOCS_AND_CUSTOM_FREQS;++ /**+ * Return true if the index format encoding this IndexOptions includes the bits required for the+ * other index format. This function provides a partial ordering of the options. The options can't+ * be totally ordered because DOCS_AND_FREQS and DOCS_AND_CUSTOM_FREQS, which are encoded using+ * the same bits, are treated as distinct options due to their different interpretations.+ */+ public boolean subsumes(IndexOptions other) {+ // treat DOCS_AND_CUSTOM_FREQS and DOCS_AND_FREQS for the purpose of subsumes+ if (this == DOCS_AND_CUSTOM_FREQS) {+ return DOCS_AND_FREQS.subsumes(other);+ }+ if (other == DOCS_AND_CUSTOM_FREQS) {+ return subsumes(DOCS_AND_FREQS);+ }+ return compareTo(other) >= 0;+ }}diff --git a/lucene/core/src/java/org/apache/lucene/index/IndexingChain.java b/lucene/core/src/java/org/apache/lucene/index/IndexingChain.javaindex f1f94b0db64..d6210063541 100644--- a/lucene/core/src/java/org/apache/lucene/index/IndexingChain.java+++ b/lucene/core/src/java/org/apache/lucene/index/IndexingChain.java@@ -1241,7 +1241,7 @@ final class IndexingChain implements Accountable {* this field name in this document.*/public void invert(int docID, IndexableField field, boolean first) throws IOException {- assert field.fieldType().indexOptions().compareTo(IndexOptions.DOCS) >= 0;+ assert field.fieldType().indexOptions().subsumes(IndexOptions.DOCS);if (first) {// First time we're seeing this field (indexed) in this document@@ -1341,8 +1341,13 @@ final class IndexingChain implements Accountable {invertState.lastStartOffset = startOffset;try {- invertState.length =- Math.addExact(invertState.length, invertState.termFreqAttribute.getTermFrequency());+ if (fieldInfo.isTermDocField()) {+ invertState.length = Math.addExact(invertState.length, 1);+ } else {+ invertState.length =+ Math.addExact(+ invertState.length, invertState.termFreqAttribute.getTermFrequency());+ }} catch (ArithmeticException ae) {throw new IllegalArgumentException("too many tokens for field \"" + field.name() + "\"", ae);@@ -1376,6 +1381,9 @@ final class IndexingChain implements Accountable {}// Document will be deleted above:throw new IllegalArgumentException(msg, e);+ } catch (TermsHashPerField.DuplicateTermException e) {+ throw new IllegalArgumentException(+ "Document update skipped due to duplicate termdoc term", e);} catch (Throwable th) {onAbortingException(th);throw th;@@ -1415,7 +1423,7 @@ final class IndexingChain implements Accountable {}final IndexableFieldType fieldType = field.fieldType();if (fieldType.tokenized()- || fieldType.indexOptions().compareTo(IndexOptions.DOCS_AND_FREQS) > 0+ || fieldType.indexOptions().subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS)|| fieldType.storeTermVectorPositions()|| fieldType.storeTermVectorOffsets()|| fieldType.storeTermVectorPayloads()) {diff --git a/lucene/core/src/java/org/apache/lucene/index/TermsHashPerField.java b/lucene/core/src/java/org/apache/lucene/index/TermsHashPerField.javaindex a0c5b50ba22..3f7be53606c 100644--- a/lucene/core/src/java/org/apache/lucene/index/TermsHashPerField.java+++ b/lucene/core/src/java/org/apache/lucene/index/TermsHashPerField.java@@ -193,13 +193,15 @@ abstract class TermsHashPerField implements Comparable<TermsHashPerField> {// term text into textStart address// Get the text & hash of this term.int termID = bytesHash.add(termBytes);- // System.out.println("add term=" + termBytesRef.utf8ToString() + " doc=" + docState.docID + "- // termID=" + termID);if (termID >= 0) { // New posting// Init stream slicesinitStreamSlices(termID, docID);} else {- termID = positionStreamSlice(termID, docID);+ try {+ termID = positionStreamSlice(termID, docID);+ } catch (DuplicateTermException e) {+ throw new DuplicateTermException(e.getMessage() + " '" + termBytes.utf8ToString() + "'");+ }}if (doNextCall) {nextPerField.add(postingsArray.textStarts[termID], docID);@@ -360,4 +362,10 @@ abstract class TermsHashPerField implements Comparable<TermsHashPerField> {/** Creates a new postings array of the specified size. */abstract ParallelPostingsArray createPostingsArray(int size);++ static class DuplicateTermException extends IllegalStateException {+ DuplicateTermException(String msg) {+ super(msg);+ }+ }}diff --git a/lucene/core/src/test/org/apache/lucene/index/TestCodecs.java b/lucene/core/src/test/org/apache/lucene/index/TestCodecs.javaindex 5409b3f629c..092f1ba3a23 100644--- a/lucene/core/src/test/org/apache/lucene/index/TestCodecs.java+++ b/lucene/core/src/test/org/apache/lucene/index/TestCodecs.java@@ -674,25 +674,23 @@ public class TestCodecs extends LuceneTestCase {@Overridepublic boolean hasFreqs() {- return fieldData.fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS) >= 0;+ return fieldData.fieldInfo.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS);}@Overridepublic boolean hasOffsets() {return fieldData- .fieldInfo- .getIndexOptions()- .compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS)- >= 0;+ .fieldInfo+ .getIndexOptions()+ .subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);}@Overridepublic boolean hasPositions() {return fieldData- .fieldInfo- .getIndexOptions()- .compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS)- >= 0;+ .fieldInfo+ .getIndexOptions()+ .subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);}@Overridediff --git a/lucene/core/src/test/org/apache/lucene/index/TestCustomTermFreq.java b/lucene/core/src/test/org/apache/lucene/index/TestCustomTermFreq.javaindex fe76b2b3af0..cf435aada95 100644--- a/lucene/core/src/test/org/apache/lucene/index/TestCustomTermFreq.java+++ b/lucene/core/src/test/org/apache/lucene/index/TestCustomTermFreq.java@@ -71,35 +71,82 @@ public class TestCustomTermFreq extends LuceneTestCase {}public void testSingletonTermsOneDoc() throws Exception {- Directory dir = newDirectory();- IndexWriter w = new IndexWriter(dir, new IndexWriterConfig(new MockAnalyzer(random())));+ try (Directory dir = newDirectory();+ IndexWriter w = new IndexWriter(dir, new IndexWriterConfig(new MockAnalyzer(random())))) {++ Document doc = new Document();+ FieldType fieldType = getTermDocFieldType();+ Field field =+ new Field(+ "field",+ new CannedTermFreqs(new String[] {"foo", "bar"}, new int[] {42, 128}),+ fieldType);+ doc.add(field);+ w.addDocument(doc);+ try (IndexReader r = DirectoryReader.open(w)) {+ PostingsEnum postings =+ MultiTerms.getTermPostingsEnum(+ r, "field", newBytesRef("bar"), (int) PostingsEnum.FREQS);+ assertNotNull(postings);+ assertEquals(0, postings.nextDoc());+ assertEquals(128, postings.freq());+ assertEquals(NO_MORE_DOCS, postings.nextDoc());++ postings =+ MultiTerms.getTermPostingsEnum(+ r, "field", newBytesRef("foo"), (int) PostingsEnum.FREQS);+ assertNotNull(postings);+ assertEquals(0, postings.nextDoc());+ assertEquals(42, postings.freq());+ assertEquals(NO_MORE_DOCS, postings.nextDoc());+ }+ }+ }- Document doc = new Document();- FieldType fieldType = new FieldType(TextField.TYPE_NOT_STORED);- fieldType.setIndexOptions(IndexOptions.DOCS_AND_FREQS);- Field field =- new Field(- "field",- new CannedTermFreqs(new String[] {"foo", "bar"}, new int[] {42, 128}),- fieldType);- doc.add(field);- w.addDocument(doc);- IndexReader r = DirectoryReader.open(w);- PostingsEnum postings =- MultiTerms.getTermPostingsEnum(r, "field", newBytesRef("bar"), (int) PostingsEnum.FREQS);- assertNotNull(postings);- assertEquals(0, postings.nextDoc());- assertEquals(128, postings.freq());- assertEquals(NO_MORE_DOCS, postings.nextDoc());+ public void testRepeatedTerms() throws Exception {+ try (Directory dir = newDirectory();+ IndexWriter w = new IndexWriter(dir, new IndexWriterConfig(new MockAnalyzer(random())))) {++ Document doc = new Document();+ FieldType fieldType = getTermDocFieldType();+ Field field =+ new Field(+ "field",+ new CannedTermFreqs(new String[] {"foo", "foo"}, new int[] {42, 128}),+ fieldType);+ doc.add(field);+ IllegalArgumentException e =+ expectThrows(IllegalArgumentException.class, () -> w.addDocument(doc));+ assertEquals("Document update skipped due to duplicate termdoc term", e.getMessage());+ TermsHashPerField.DuplicateTermException dte =+ (TermsHashPerField.DuplicateTermException) e.getCause();+ assertEquals("field 'field' has duplicate term 'foo'", dte.getMessage());++ // ensure IndexWriter remains open and doc was skipped+ field =+ new Field("field", new CannedTermFreqs(new String[] {"foo"}, new int[] {42}), fieldType);+ Document okDoc = new Document();+ okDoc.add(field);+ w.addDocument(okDoc);+ try (IndexReader r = DirectoryReader.open(w)) {+ assertEquals(1, r.numDocs());+ }+ }+ }- postings =- MultiTerms.getTermPostingsEnum(r, "field", newBytesRef("foo"), (int) PostingsEnum.FREQS);- assertNotNull(postings);- assertEquals(0, postings.nextDoc());- assertEquals(42, postings.freq());- assertEquals(NO_MORE_DOCS, postings.nextDoc());+ private static FieldType getTermDocFieldType() {+ FieldType fieldType = new FieldType(TextField.TYPE_NOT_STORED);+ fieldType.setIndexOptions(IndexOptions.DOCS_AND_CUSTOM_FREQS);+ return fieldType;+ }- IOUtils.close(r, w, dir);+ private static FieldType getLegacyFieldType() {+ // It is possible to store custom term frequencies in fields that are *not* labeled as term-doc+ // fields. These will sum term frequencies rather than counts and have more places where they+ // may overflow these aggregates.+ FieldType fieldType = new FieldType(TextField.TYPE_NOT_STORED);+ fieldType.setIndexOptions(IndexOptions.DOCS_AND_FREQS);+ return fieldType;}public void testSingletonTermsTwoDocs() throws Exception {@@ -107,8 +154,7 @@ public class TestCustomTermFreq extends LuceneTestCase {IndexWriter w = new IndexWriter(dir, new IndexWriterConfig(new MockAnalyzer(random())));Document doc = new Document();- FieldType fieldType = new FieldType(TextField.TYPE_NOT_STORED);- fieldType.setIndexOptions(IndexOptions.DOCS_AND_FREQS);+ FieldType fieldType = getTermDocFieldType();Field field =new Field("field",@@ -149,36 +195,37 @@ public class TestCustomTermFreq extends LuceneTestCase {}public void testRepeatTermsOneDoc() throws Exception {- Directory dir = newDirectory();- IndexWriter w = new IndexWriter(dir, new IndexWriterConfig(new MockAnalyzer(random())));-- Document doc = new Document();- FieldType fieldType = new FieldType(TextField.TYPE_NOT_STORED);- fieldType.setIndexOptions(IndexOptions.DOCS_AND_FREQS);- Field field =- new Field(- "field",- new CannedTermFreqs(- new String[] {"foo", "bar", "foo", "bar"}, new int[] {42, 128, 17, 100}),- fieldType);- doc.add(field);- w.addDocument(doc);- IndexReader r = DirectoryReader.open(w);- PostingsEnum postings =- MultiTerms.getTermPostingsEnum(r, "field", newBytesRef("bar"), (int) PostingsEnum.FREQS);- assertNotNull(postings);- assertEquals(0, postings.nextDoc());- assertEquals(228, postings.freq());- assertEquals(NO_MORE_DOCS, postings.nextDoc());-- postings =- MultiTerms.getTermPostingsEnum(r, "field", newBytesRef("foo"), (int) PostingsEnum.FREQS);- assertNotNull(postings);- assertEquals(0, postings.nextDoc());- assertEquals(59, postings.freq());- assertEquals(NO_MORE_DOCS, postings.nextDoc());-- IOUtils.close(r, w, dir);+ // With legacy field type, we sum the individual term freqs+ try (Directory dir = newDirectory();+ IndexWriter w = new IndexWriter(dir, new IndexWriterConfig(new MockAnalyzer(random())))) {+ Document doc = new Document();+ FieldType fieldType = getLegacyFieldType();+ Field field =+ new Field(+ "field",+ new CannedTermFreqs(+ new String[] {"foo", "bar", "foo", "bar"}, new int[] {42, 128, 17, 100}),+ fieldType);+ doc.add(field);+ w.addDocument(doc);+ try (IndexReader r = DirectoryReader.open(w)) {+ PostingsEnum postings =+ MultiTerms.getTermPostingsEnum(+ r, "field", newBytesRef("bar"), (int) PostingsEnum.FREQS);+ assertNotNull(postings);+ assertEquals(0, postings.nextDoc());+ assertEquals(228, postings.freq());+ assertEquals(NO_MORE_DOCS, postings.nextDoc());++ postings =+ MultiTerms.getTermPostingsEnum(+ r, "field", newBytesRef("foo"), (int) PostingsEnum.FREQS);+ assertNotNull(postings);+ assertEquals(0, postings.nextDoc());+ assertEquals(59, postings.freq());+ assertEquals(NO_MORE_DOCS, postings.nextDoc());+ }+ }}public void testRepeatTermsTwoDocs() throws Exception {@@ -186,8 +233,7 @@ public class TestCustomTermFreq extends LuceneTestCase {IndexWriter w = new IndexWriter(dir, new IndexWriterConfig(new MockAnalyzer(random())));Document doc = new Document();- FieldType fieldType = new FieldType(TextField.TYPE_NOT_STORED);- fieldType.setIndexOptions(IndexOptions.DOCS_AND_FREQS);+ FieldType fieldType = getLegacyFieldType();Field field =new Field("field",@@ -198,7 +244,6 @@ public class TestCustomTermFreq extends LuceneTestCase {w.addDocument(doc);doc = new Document();- fieldType.setIndexOptions(IndexOptions.DOCS_AND_FREQS);field =new Field("field",@@ -235,19 +280,17 @@ public class TestCustomTermFreq extends LuceneTestCase {IndexWriter w = new IndexWriter(dir, new IndexWriterConfig(new MockAnalyzer(random())));Document doc = new Document();- FieldType fieldType = new FieldType(TextField.TYPE_NOT_STORED);- fieldType.setIndexOptions(IndexOptions.DOCS_AND_FREQS);+ FieldType fieldType = getLegacyFieldType();Field field =new Field("field",new CannedTermFreqs(- new String[] {"foo", "bar", "foo", "bar"}, new int[] {42, 128, 17, 100}),+ new String[] {"foo", "bar", "foo", "bar", "baz"}, new int[] {42, 128, 17, 100, 99}),fieldType);doc.add(field);w.addDocument(doc);doc = new Document();- fieldType.setIndexOptions(IndexOptions.DOCS_AND_FREQS);field =new Field("field",@@ -268,6 +311,39 @@ public class TestCustomTermFreq extends LuceneTestCase {IOUtils.close(r, w, dir);}+ public void testTotalTermFreqTermDoc() throws Exception {+ try (Directory dir = newDirectory();+ IndexWriter w = new IndexWriter(dir, new IndexWriterConfig(new MockAnalyzer(random())))) {++ Document doc = new Document();+ FieldType fieldType = getTermDocFieldType();+ Field field =+ new Field(+ "field",+ new CannedTermFreqs(new String[] {"foo", "bar"}, new int[] {42, 128}),+ fieldType);+ doc.add(field);+ w.addDocument(doc);++ doc = new Document();+ field =+ new Field(+ "field",+ new CannedTermFreqs(new String[] {"foo", "bar"}, new int[] {50, 60}),+ fieldType);+ doc.add(field);+ w.addDocument(doc);++ try (IndexReader r = DirectoryReader.open(w)) {+ TermsEnum termsEnum = MultiTerms.getTerms(r, "field").iterator();+ assertTrue(termsEnum.seekExact(newBytesRef("foo")));+ assertEquals(92, termsEnum.totalTermFreq());+ assertTrue(termsEnum.seekExact(newBytesRef("bar")));+ assertEquals(188, termsEnum.totalTermFreq());+ }+ }+ }+// you can't index proximity with custom term freqs:public void testInvalidProx() throws Exception {Directory dir = newDirectory();@@ -352,13 +428,52 @@ public class TestCustomTermFreq extends LuceneTestCase {IOUtils.close(r, w, dir);}+ public void testNoOverflowInt() throws Exception {+ try (Directory dir = newDirectory();+ IndexWriter w = new IndexWriter(dir, new IndexWriterConfig(new MockAnalyzer(random())))) {++ // Using the termdoc field type enables us to store large term frequencies that would+ // otherwise overflow totalTermFreq+ FieldType fieldType = getTermDocFieldType();++ Document doc2 = new Document();+ Field field =+ new Field(+ "field",+ new CannedTermFreqs(new String[] {"foo", "bar"}, new int[] {3, Integer.MAX_VALUE}),+ fieldType);+ doc2.add(field);+ w.addDocument(doc2);+ try (IndexReader r = DirectoryReader.open(w)) {+ assertEquals(1, r.numDocs());+ TermsEnum termsEnum = MultiTerms.getTerms(r, "field").iterator();+ assertTrue(termsEnum.seekExact(newBytesRef("foo")));+ assertEquals(3, termsEnum.totalTermFreq());++ PostingsEnum postings = termsEnum.postings(null);+ assertNotNull(postings);+ assertEquals(0, postings.nextDoc());+ assertEquals(3, postings.freq());+ assertEquals(NO_MORE_DOCS, postings.nextDoc());++ assertTrue(termsEnum.seekExact(newBytesRef("bar")));+ assertEquals(Integer.MAX_VALUE, termsEnum.totalTermFreq());++ postings = termsEnum.postings(null);+ assertNotNull(postings);+ assertEquals(0, postings.nextDoc());+ assertEquals(Integer.MAX_VALUE, postings.freq());+ assertEquals(NO_MORE_DOCS, postings.nextDoc());+ }+ }+ }+public void testInvalidTermVectorPositions() throws Exception {Directory dir = newDirectory();IndexWriter w = new IndexWriter(dir, new IndexWriterConfig(new MockAnalyzer(random())));Document doc = new Document();- FieldType fieldType = new FieldType(TextField.TYPE_NOT_STORED);- fieldType.setIndexOptions(IndexOptions.DOCS_AND_FREQS);+ FieldType fieldType = getTermDocFieldType();fieldType.setStoreTermVectors(true);fieldType.setStoreTermVectorPositions(true);Field field =@@ -385,8 +500,7 @@ public class TestCustomTermFreq extends LuceneTestCase {IndexWriter w = new IndexWriter(dir, new IndexWriterConfig(new MockAnalyzer(random())));Document doc = new Document();- FieldType fieldType = new FieldType(TextField.TYPE_NOT_STORED);- fieldType.setIndexOptions(IndexOptions.DOCS_AND_FREQS);+ FieldType fieldType = getTermDocFieldType();fieldType.setStoreTermVectors(true);fieldType.setStoreTermVectorOffsets(true);Field field =@@ -413,25 +527,22 @@ public class TestCustomTermFreq extends LuceneTestCase {IndexWriter w = new IndexWriter(dir, new IndexWriterConfig(new MockAnalyzer(random())));Document doc = new Document();- FieldType fieldType = new FieldType(TextField.TYPE_NOT_STORED);- fieldType.setIndexOptions(IndexOptions.DOCS_AND_FREQS);+ FieldType fieldType = getTermDocFieldType();fieldType.setStoreTermVectors(true);Field field =new Field("field",- new CannedTermFreqs(- new String[] {"foo", "bar", "foo", "bar"}, new int[] {42, 128, 17, 100}),+ new CannedTermFreqs(new String[] {"foo", "bar"}, new int[] {42, 128}),fieldType);doc.add(field);w.addDocument(doc);doc = new Document();- fieldType.setIndexOptions(IndexOptions.DOCS_AND_FREQS);+ fieldType.setIndexOptions(IndexOptions.DOCS_AND_CUSTOM_FREQS);field =new Field("field",- new CannedTermFreqs(- new String[] {"foo", "bar", "foo", "bar"}, new int[] {50, 60, 70, 80}),+ new CannedTermFreqs(new String[] {"foo", "bar"}, new int[] {50, 60}),fieldType);doc.add(field);w.addDocument(doc);@@ -441,37 +552,37 @@ public class TestCustomTermFreq extends LuceneTestCase {Fields fields = r.termVectors().get(0);TermsEnum termsEnum = fields.terms("field").iterator();assertTrue(termsEnum.seekExact(newBytesRef("bar")));- assertEquals(228, termsEnum.totalTermFreq());+ assertEquals(128, termsEnum.totalTermFreq());PostingsEnum postings = termsEnum.postings(null);assertNotNull(postings);assertEquals(0, postings.nextDoc());- assertEquals(228, postings.freq());+ assertEquals(128, postings.freq());assertEquals(NO_MORE_DOCS, postings.nextDoc());assertTrue(termsEnum.seekExact(newBytesRef("foo")));- assertEquals(59, termsEnum.totalTermFreq());+ assertEquals(42, termsEnum.totalTermFreq());postings = termsEnum.postings(null);assertNotNull(postings);assertEquals(0, postings.nextDoc());- assertEquals(59, postings.freq());+ assertEquals(42, postings.freq());assertEquals(NO_MORE_DOCS, postings.nextDoc());fields = r.termVectors().get(1);termsEnum = fields.terms("field").iterator();assertTrue(termsEnum.seekExact(newBytesRef("bar")));- assertEquals(140, termsEnum.totalTermFreq());+ assertEquals(60, termsEnum.totalTermFreq());postings = termsEnum.postings(null);assertNotNull(postings);assertEquals(0, postings.nextDoc());- assertEquals(140, postings.freq());+ assertEquals(60, postings.freq());assertEquals(NO_MORE_DOCS, postings.nextDoc());assertTrue(termsEnum.seekExact(newBytesRef("foo")));- assertEquals(120, termsEnum.totalTermFreq());+ assertEquals(50, termsEnum.totalTermFreq());postings = termsEnum.postings(null);assertNotNull(postings);assertEquals(0, postings.nextDoc());- assertEquals(120, postings.freq());+ assertEquals(50, postings.freq());assertEquals(NO_MORE_DOCS, postings.nextDoc());IOUtils.close(r, w, dir);@@ -505,21 +616,19 @@ public class TestCustomTermFreq extends LuceneTestCase {IndexWriter w = new IndexWriter(dir, iwc);Document doc = new Document();- FieldType fieldType = new FieldType(TextField.TYPE_NOT_STORED);- fieldType.setIndexOptions(IndexOptions.DOCS_AND_FREQS);+ FieldType fieldType = getTermDocFieldType();Field field =new Field("field",- new CannedTermFreqs(- new String[] {"foo", "bar", "foo", "bar"}, new int[] {42, 128, 17, 100}),+ new CannedTermFreqs(new String[] {"foo", "bar"}, new int[] {42, 128}),fieldType);doc.add(field);w.addDocument(doc);FieldInvertState fis = NeverForgetsSimilarity.INSTANCE.lastState;- assertEquals(228, fis.getMaxTermFrequency());+ assertEquals(128, fis.getMaxTermFrequency());assertEquals(2, fis.getUniqueTermCount());assertEquals(0, fis.getNumOverlap());- assertEquals(287, fis.getLength());+ assertEquals(2, fis.getLength());IOUtils.close(w, dir);}diff --git a/lucene/core/src/test/org/apache/lucene/index/TestDocumentWriter.java b/lucene/core/src/test/org/apache/lucene/index/TestDocumentWriter.javaindex af5b2938d59..12987c43cc4 100644--- a/lucene/core/src/test/org/apache/lucene/index/TestDocumentWriter.java+++ b/lucene/core/src/test/org/apache/lucene/index/TestDocumentWriter.java@@ -556,7 +556,7 @@ public class TestDocumentWriter extends LuceneTestCase {{Terms terms = leafReader.terms("field");assertEquals(1, terms.getSumDocFreq());- if (ft.indexOptions().compareTo(IndexOptions.DOCS_AND_FREQS) >= 0) {+ if (ft.indexOptions().subsumes(IndexOptions.DOCS_AND_FREQS)) {assertEquals(2, terms.getSumTotalTermFreq());} else {assertEquals(1, terms.getSumTotalTermFreq());@@ -565,7 +565,7 @@ public class TestDocumentWriter extends LuceneTestCase {assertTrue(termsEnum.seekExact(new BytesRef("a")));PostingsEnum pe = termsEnum.postings(null, PostingsEnum.ALL);assertEquals(0, pe.nextDoc());- if (ft.indexOptions().compareTo(IndexOptions.DOCS_AND_FREQS) >= 0) {+ if (ft.indexOptions().subsumes(IndexOptions.DOCS_AND_FREQS)) {assertEquals(2, pe.freq());} else {assertEquals(1, pe.freq());diff --git a/lucene/core/src/test/org/apache/lucene/search/TestSimilarity.java b/lucene/core/src/test/org/apache/lucene/search/TestSimilarity.javaindex c178e65c78d..6ec72a4a5fa 100644--- a/lucene/core/src/test/org/apache/lucene/search/TestSimilarity.java+++ b/lucene/core/src/test/org/apache/lucene/search/TestSimilarity.java@@ -18,8 +18,12 @@ package org.apache.lucene.search;import java.io.IOException;import java.util.Collection;+import java.util.function.Function;import org.apache.lucene.document.Document;import org.apache.lucene.document.Field;+import org.apache.lucene.document.FieldType;+import org.apache.lucene.document.TextField;+import org.apache.lucene.index.IndexOptions;import org.apache.lucene.index.IndexReader;import org.apache.lucene.index.LeafReaderContext;import org.apache.lucene.index.Term;@@ -127,6 +131,181 @@ public class TestSimilarity extends LuceneTestCase {store.close();}+ public void testSimilarityTermDocNoTf() throws Exception {+ Directory store = newDirectory();+ RandomIndexWriter writer =+ new RandomIndexWriter(+ random(),+ store,+ newIndexWriterConfig(new MockAnalyzer(random()))+ .setSimilarity(new SimpleSimilarity())+ .setMergePolicy(newMergePolicy(random(), false)));++ FieldType fieldType = new FieldType(TextField.TYPE_STORED);+ fieldType.setIndexOptions(IndexOptions.DOCS_AND_CUSTOM_FREQS);++ Document d1 = new Document();+ d1.add(new Field("field", "a c", fieldType));++ Document d2 = new Document();+ d2.add(new Field("field", "a c b", fieldType));++ writer.addDocument(d1);+ writer.addDocument(d2);+ IndexReader reader = writer.getReader();+ writer.close();++ IndexSearcher searcher = newSearcher(reader);+ searcher.setSimilarity(new SimpleSimilarity());++ Term a = new Term("field", "a");+ Term b = new Term("field", "b");+ Term c = new Term("field", "c");++ // when no custom term freq is provided, default to 1+ assertScore(searcher, new TermQuery(a), 1f);+ assertScore(searcher, new TermQuery(b), 1f);+ assertScore(searcher, new TermQuery(c), 1f);++ BooleanQuery.Builder bq = new BooleanQuery.Builder();+ bq.add(new TermQuery(a), BooleanClause.Occur.SHOULD);+ bq.add(new TermQuery(b), BooleanClause.Occur.SHOULD);+ // System.out.println(bq.toString("field"));+ searcher.search(+ bq.build(),+ new CollectorManager<SimpleCollector, Void>() {+ @Override+ public SimpleCollector newCollector() {+ // score is the sum(1) for each matching term in the doc+ int[] scores = {1, 2};+ return new ScoreAssertingCollector() {+ private int base = 0;++ @Override+ public void collect(int doc) throws IOException {+ // System.out.println("Doc=" + doc + " score=" + scorer.score());+ assertEquals(scores[base + doc], scorer.score(), 0);+ }++ @Override+ protected void doSetNextReader(LeafReaderContext context) {+ base = context.docBase;+ }+ };+ }++ @Override+ public Void reduce(Collection<SimpleCollector> collectors) {+ return null;+ }+ });+ reader.close();+ store.close();+ }++ public void testTermDocSimilarity() throws IOException {+ try (Directory store = newDirectory()) {+ Function<CharSequence, Integer> termFreq =+ term ->+ switch (term.toString()) {+ case "a" -> 17;+ case "b" -> Integer.MAX_VALUE;+ case "d" -> Integer.MAX_VALUE;+ default -> 1;+ };++ RandomIndexWriter writer =+ new RandomIndexWriter(+ random(),+ store,+ newIndexWriterConfig(new MockAnalyzer(random(), termFreq))+ .setSimilarity(new SimpleSimilarity())+ .setMergePolicy(newMergePolicy(random(), false)));++ FieldType type = new FieldType(TextField.TYPE_STORED);+ type.setIndexOptions(IndexOptions.DOCS_AND_CUSTOM_FREQS);++ Document d1 = new Document();+ d1.add(new Field("field", "a c", type));++ Document d2 = new Document();+ d2.add(new Field("field", "a c b d", type));++ writer.addDocument(d1);+ writer.addDocument(d2);+ try (IndexReader reader = writer.getReader()) {+ writer.close();++ IndexSearcher searcher = newSearcher(reader);+ searcher.setSimilarity(new SimpleSimilarity());++ Term a = new Term("field", "a");+ Term b = new Term("field", "b");+ Term c = new Term("field", "c");+ Term d = new Term("field", "d");++ assertScore(searcher, new TermQuery(a), 17f);+ assertScore(searcher, new TermQuery(b), Integer.MAX_VALUE);+ assertScore(searcher, new TermQuery(c), 1f);++ BooleanQuery.Builder bq = new BooleanQuery.Builder();+ bq.add(new TermQuery(a), BooleanClause.Occur.SHOULD);+ bq.add(new TermQuery(b), BooleanClause.Occur.SHOULD);+ // BooleanScorer takes the sum of scores of disjunctive terms+ // but loss of floating precision means MAX_VALUE + 17 == MAX_VALUE.+ assertResults(searcher, bq.build(), new float[] {17f, (float) Integer.MAX_VALUE});++ bq = new BooleanQuery.Builder();+ bq.add(new TermQuery(b), BooleanClause.Occur.SHOULD);+ bq.add(new TermQuery(d), BooleanClause.Occur.SHOULD);+ // here we see we can sum max int value twice; there are no problems with overflow+ assertResults(searcher, bq.build(), new float[] {-1, 2 * (float) Integer.MAX_VALUE});+ }+ }+ }++ private static void assertResults(IndexSearcher searcher, Query query, float[] scores)+ throws IOException {+ CollectorManager<SimpleCollector, Integer> collector =+ new CollectorManager<>() {+ int count = 0;++ @Override+ public SimpleCollector newCollector() {+ return new ScoreAssertingCollector() {+ private int base = 0;++ @Override+ public void collect(int doc) throws IOException {+ // System.out.println("Doc=" + doc + " score=" + scorer.score());+ assertEquals(scores[doc + base], scorer.score(), 0);+ // not thread-safe, but I think it's OK?+ ++count;+ }++ @Override+ protected void doSetNextReader(LeafReaderContext context) {+ base = context.docBase;+ }+ };+ }++ @Override+ public Integer reduce(Collection<SimpleCollector> collectors) {+ return count;+ }+ };++ int expectedCount = 0;+ for (float score : scores) {+ if (score >= 0) {+ expectedCount++;+ }+ }+ int count = searcher.search(query, collector);+ assertEquals(expectedCount, count);+ }+private static void assertScore(IndexSearcher searcher, Query query, float score)throws IOException {searcher.search(diff --git a/lucene/highlighter/src/java/org/apache/lucene/search/matchhighlight/MatchRegionRetriever.java b/lucene/highlighter/src/java/org/apache/lucene/search/matchhighlight/MatchRegionRetriever.javaindex d967fe2fb77..703e8c41221 100644--- a/lucene/highlighter/src/java/org/apache/lucene/search/matchhighlight/MatchRegionRetriever.java+++ b/lucene/highlighter/src/java/org/apache/lucene/search/matchhighlight/MatchRegionRetriever.java@@ -492,6 +492,7 @@ public class MatchRegionRetriever {case DOCS_AND_FREQS_AND_POSITIONS:return new OffsetsFromPositions(field, analyzer);+ case DOCS_AND_CUSTOM_FREQS:case DOCS_AND_FREQS:case DOCS:// By default retrieve offsets from individual tokensdiff --git a/lucene/luke/src/java/org/apache/lucene/luke/app/desktop/components/DocumentsPanelProvider.java b/lucene/luke/src/java/org/apache/lucene/luke/app/desktop/components/DocumentsPanelProvider.javaindex 61752b45d15..ae993b92455 100644--- a/lucene/luke/src/java/org/apache/lucene/luke/app/desktop/components/DocumentsPanelProvider.java+++ b/lucene/luke/src/java/org/apache/lucene/luke/app/desktop/components/DocumentsPanelProvider.java@@ -1125,6 +1125,9 @@ public final class DocumentsPanelProvider implements DocumentsTabOperator {case DOCS_AND_FREQS:sb.append("df--");break;+ case DOCS_AND_CUSTOM_FREQS:+ sb.append("dc--");+ break;case DOCS_AND_FREQS_AND_POSITIONS:sb.append("dfp-");break;diff --git a/lucene/memory/src/java/org/apache/lucene/index/memory/MemoryIndex.java b/lucene/memory/src/java/org/apache/lucene/index/memory/MemoryIndex.javaindex 62548cb6ca1..6a8e59680f4 100644--- a/lucene/memory/src/java/org/apache/lucene/index/memory/MemoryIndex.java+++ b/lucene/memory/src/java/org/apache/lucene/index/memory/MemoryIndex.java@@ -633,7 +633,7 @@ public class MemoryIndex {}if (tokenStream != null) {storeTerms(info, tokenStream, positionIncrementGap, offsetGap);- } else if (field.fieldType().indexOptions().compareTo(IndexOptions.DOCS) >= 0) {+ } else if (field.fieldType().indexOptions().subsumes(IndexOptions.DOCS)) {BytesRef binaryValue = field.binaryValue();if (binaryValue == null) {throw new IllegalArgumentException(diff --git a/lucene/sandbox/src/java/org/apache/lucene/sandbox/codecs/idversion/VersionFieldReader.java b/lucene/sandbox/src/java/org/apache/lucene/sandbox/codecs/idversion/VersionFieldReader.javaindex 8c2f18ec529..18d279eb8f3 100644--- a/lucene/sandbox/src/java/org/apache/lucene/sandbox/codecs/idversion/VersionFieldReader.java+++ b/lucene/sandbox/src/java/org/apache/lucene/sandbox/codecs/idversion/VersionFieldReader.java@@ -126,20 +126,19 @@ final class VersionFieldReader extends Terms {@Overridepublic boolean hasFreqs() {- return fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS) >= 0;+ return fieldInfo.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS);}@Overridepublic boolean hasOffsets() {return fieldInfo- .getIndexOptions()- .compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS)- >= 0;+ .getIndexOptions()+ .subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);}@Overridepublic boolean hasPositions() {- return fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0;+ return fieldInfo.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);}@Overridediff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/analysis/MockAnalyzer.java b/lucene/test-framework/src/java/org/apache/lucene/tests/analysis/MockAnalyzer.javaindex cb3e7f855e1..7850e7442ab 100644--- a/lucene/test-framework/src/java/org/apache/lucene/tests/analysis/MockAnalyzer.java+++ b/lucene/test-framework/src/java/org/apache/lucene/tests/analysis/MockAnalyzer.java@@ -19,6 +19,7 @@ package org.apache.lucene.tests.analysis;import java.util.HashMap;import java.util.Map;import java.util.Random;+import java.util.function.Function;import org.apache.lucene.analysis.Analyzer;import org.apache.lucene.analysis.TokenFilter;import org.apache.lucene.analysis.TokenStream;@@ -46,6 +47,7 @@ public final class MockAnalyzer extends Analyzer {private final CharacterRunAutomaton runAutomaton;private final boolean lowerCase;private final CharacterRunAutomaton filter;+ private final Function<CharSequence, Integer> customTermFreq;private int positionIncrementGap;private Integer offsetGap;private final Random random;@@ -65,38 +67,65 @@ public final class MockAnalyzer extends Analyzer {Random random,CharacterRunAutomaton runAutomaton,boolean lowerCase,- CharacterRunAutomaton filter) {+ CharacterRunAutomaton filter,+ Function<CharSequence, Integer> customTermFreq) {super(PER_FIELD_REUSE_STRATEGY);// TODO: this should be solved in a different way; Random should not be shared (!).this.random = new Random(random.nextLong());this.runAutomaton = runAutomaton;this.lowerCase = lowerCase;this.filter = filter;+ this.customTermFreq = customTermFreq;}/**- * Calls {@link #MockAnalyzer(Random, CharacterRunAutomaton, boolean, CharacterRunAutomaton)- * MockAnalyzer(random, runAutomaton, lowerCase, MockTokenFilter.EMPTY_STOPSET, false}).+ * Calls {@link #MockAnalyzer(Random, CharacterRunAutomaton, boolean, CharacterRunAutomaton,+ * Function) MockAnalyzer(random, runAutomaton, lowerCase, MockTokenFilter.EMPTY_STOPSET, null}).+ */+ public MockAnalyzer(+ Random random,+ CharacterRunAutomaton runAutomaton,+ boolean lowerCase,+ CharacterRunAutomaton filter) {+ this(random, runAutomaton, lowerCase, filter, null);+ }++ /**+ * Calls {@link #MockAnalyzer(Random, CharacterRunAutomaton, boolean, CharacterRunAutomaton,+ * Function) MockAnalyzer(random, runAutomaton, lowerCase, MockTokenFilter.EMPTY_STOPSET, null}).*/public MockAnalyzer(Random random, CharacterRunAutomaton runAutomaton, boolean lowerCase) {- this(random, runAutomaton, lowerCase, MockTokenFilter.EMPTY_STOPSET);+ this(random, runAutomaton, lowerCase, MockTokenFilter.EMPTY_STOPSET, null);+ }++ /**+ * Create a Whitespace-lowercasing analyzer with no stopwords removal and custom term frequencies.+ *+ * <p>Calls {@link #MockAnalyzer(Random, CharacterRunAutomaton, boolean, CharacterRunAutomaton,+ * Function) MockAnalyzer(random, MockTokenizer.WHITESPACE, true, MockTokenFilter.EMPTY_STOPSET,+ * customTermFreq}).+ */+ public MockAnalyzer(Random random, Function<CharSequence, Integer> customTermFreq) {+ this(random, MockTokenizer.WHITESPACE, true, MockTokenFilter.EMPTY_STOPSET, customTermFreq);}/*** Create a Whitespace-lowercasing analyzer with no stopwords removal.*- * <p>Calls {@link #MockAnalyzer(Random, CharacterRunAutomaton, boolean, CharacterRunAutomaton)- * MockAnalyzer(random, MockTokenizer.WHITESPACE, true, MockTokenFilter.EMPTY_STOPSET, false}).+ * <p>Calls {@link #MockAnalyzer(Random, CharacterRunAutomaton, boolean, CharacterRunAutomaton,+ * Function) MockAnalyzer(random, MockTokenizer.WHITESPACE, true, MockTokenFilter.EMPTY_STOPSET,+ * null}).*/public MockAnalyzer(Random random) {- this(random, MockTokenizer.WHITESPACE, true);+ this(random, MockTokenizer.WHITESPACE, true, MockTokenFilter.EMPTY_STOPSET, null);}@Overridepublic TokenStreamComponents createComponents(String fieldName) {MockTokenizer tokenizer = new MockTokenizer(runAutomaton, lowerCase, maxTokenLength);tokenizer.setEnableChecks(enableChecks);- MockTokenFilter filt = new MockTokenFilter(tokenizer, filter);+ MockTokenFilter filt;+ filt = new MockTokenFilter(tokenizer, filter, customTermFreq);return new TokenStreamComponents(tokenizer, maybePayload(filt, fieldName));}diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/analysis/MockTokenFilter.java b/lucene/test-framework/src/java/org/apache/lucene/tests/analysis/MockTokenFilter.javaindex 991e4f45b7b..a44d73ca999 100644--- a/lucene/test-framework/src/java/org/apache/lucene/tests/analysis/MockTokenFilter.java+++ b/lucene/test-framework/src/java/org/apache/lucene/tests/analysis/MockTokenFilter.java@@ -21,10 +21,12 @@ import static org.apache.lucene.util.automaton.Automata.makeString;import java.io.IOException;import java.util.Arrays;+import java.util.function.Function;import org.apache.lucene.analysis.TokenFilter;import org.apache.lucene.analysis.TokenStream;import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute;+import org.apache.lucene.analysis.tokenattributes.TermFrequencyAttribute;import org.apache.lucene.util.automaton.CharacterRunAutomaton;import org.apache.lucene.util.automaton.Operations;@@ -85,8 +87,9 @@ public final class MockTokenFilter extends TokenFilter {private final CharacterRunAutomaton filter;private final CharTermAttribute termAtt = addAttribute(CharTermAttribute.class);- private final PositionIncrementAttribute posIncrAtt =- addAttribute(PositionIncrementAttribute.class);+ private final PositionIncrementAttribute posIncrAtt;+ private final TermFrequencyAttribute termFreqAtt;+ private final Function<CharSequence, Integer> customTermFreq;private int skippedPositions;/**@@ -98,6 +101,32 @@ public final class MockTokenFilter extends TokenFilter {public MockTokenFilter(TokenStream input, CharacterRunAutomaton filter) {super(input);this.filter = filter;+ termFreqAtt = null;+ customTermFreq = null;+ posIncrAtt = addAttribute(PositionIncrementAttribute.class);+ }++ /**+ * Create a new MockTokenFilter that will generate custom term frequencies.+ *+ * @param input TokenStream to filter+ * @param filter DFA representing the terms that should be removed.+ * @param customTermFreq optional method mapping term to score+ */+ public MockTokenFilter(+ TokenStream input,+ CharacterRunAutomaton filter,+ Function<CharSequence, Integer> customTermFreq) {+ super(input);+ this.filter = filter;+ this.customTermFreq = customTermFreq;+ if (customTermFreq != null) {+ posIncrAtt = null;+ termFreqAtt = addAttribute(TermFrequencyAttribute.class);+ } else {+ posIncrAtt = addAttribute(PositionIncrementAttribute.class);+ termFreqAtt = null;+ }}@Override@@ -109,10 +138,17 @@ public final class MockTokenFilter extends TokenFilter {skippedPositions = 0;while (input.incrementToken()) {if (!filter.run(termAtt.buffer(), 0, termAtt.length())) {- posIncrAtt.setPositionIncrement(posIncrAtt.getPositionIncrement() + skippedPositions);+ if (termFreqAtt != null) {+ termFreqAtt.setTermFrequency(customTermFreq.apply(termAtt));+ }+ if (posIncrAtt != null) {+ posIncrAtt.setPositionIncrement(posIncrAtt.getPositionIncrement() + skippedPositions);+ }return true;}- skippedPositions += posIncrAtt.getPositionIncrement();+ if (posIncrAtt != null) {+ skippedPositions += posIncrAtt.getPositionIncrement();+ }}// reached EOS -- return falsereturn false;@@ -121,7 +157,9 @@ public final class MockTokenFilter extends TokenFilter {@Overridepublic void end() throws IOException {super.end();- posIncrAtt.setPositionIncrement(posIncrAtt.getPositionIncrement() + skippedPositions);+ if (posIncrAtt != null) {+ posIncrAtt.setPositionIncrement(posIncrAtt.getPositionIncrement() + skippedPositions);+ }}@Overridediff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/codecs/asserting/AssertingPostingsFormat.java b/lucene/test-framework/src/java/org/apache/lucene/tests/codecs/asserting/AssertingPostingsFormat.javaindex 72074227491..007eb7edbf9 100644--- a/lucene/test-framework/src/java/org/apache/lucene/tests/codecs/asserting/AssertingPostingsFormat.java+++ b/lucene/test-framework/src/java/org/apache/lucene/tests/codecs/asserting/AssertingPostingsFormat.java@@ -140,14 +140,13 @@ public final class AssertingPostingsFormat extends PostingsFormat {BytesRefBuilder lastTerm = null;PostingsEnum postingsEnum = null;- boolean hasFreqs = fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS) >= 0;+ boolean hasFreqs = fieldInfo.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS);boolean hasPositions =- fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0;+ fieldInfo.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);boolean hasOffsets =fieldInfo- .getIndexOptions()- .compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS)- >= 0;+ .getIndexOptions()+ .subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);boolean hasPayloads = terms.hasPayloads();assert hasPositions == terms.hasPositions();diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/codecs/ramonly/RAMOnlyPostingsFormat.java b/lucene/test-framework/src/java/org/apache/lucene/tests/codecs/ramonly/RAMOnlyPostingsFormat.javaindex be547fa29ef..4077fa1344c 100644--- a/lucene/test-framework/src/java/org/apache/lucene/tests/codecs/ramonly/RAMOnlyPostingsFormat.java+++ b/lucene/test-framework/src/java/org/apache/lucene/tests/codecs/ramonly/RAMOnlyPostingsFormat.java@@ -130,18 +130,17 @@ public final class RAMOnlyPostingsFormat extends PostingsFormat {@Overridepublic boolean hasFreqs() {- return info.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS) >= 0;+ return info.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS);}@Overridepublic boolean hasOffsets() {- return info.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS)- >= 0;+ return info.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);}@Overridepublic boolean hasPositions() {- return info.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0;+ return info.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);}@Override@@ -209,9 +208,8 @@ public final class RAMOnlyPostingsFormat extends PostingsFormat {FieldInfo fieldInfo = state.fieldInfos.fieldInfo(field);if (fieldInfo- .getIndexOptions()- .compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS)- >= 0) {+ .getIndexOptions()+ .subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS)) {throw new UnsupportedOperationException("this codec cannot index offsets");}@@ -226,11 +224,10 @@ public final class RAMOnlyPostingsFormat extends PostingsFormat {int enumFlags;IndexOptions indexOptions = fieldInfo.getIndexOptions();- boolean writeFreqs = indexOptions.compareTo(IndexOptions.DOCS_AND_FREQS) >= 0;- boolean writePositions =- indexOptions.compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0;+ boolean writeFreqs = indexOptions.subsumes(IndexOptions.DOCS_AND_FREQS);+ boolean writePositions = indexOptions.subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);boolean writeOffsets =- indexOptions.compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS) >= 0;+ indexOptions.subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);boolean writePayloads = fieldInfo.hasPayloads();if (writeFreqs == false) {diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/index/BaseFieldInfoFormatTestCase.java b/lucene/test-framework/src/java/org/apache/lucene/tests/index/BaseFieldInfoFormatTestCase.javaindex ec05e023865..eda4653fe08 100644--- a/lucene/test-framework/src/java/org/apache/lucene/tests/index/BaseFieldInfoFormatTestCase.java+++ b/lucene/test-framework/src/java/org/apache/lucene/tests/index/BaseFieldInfoFormatTestCase.java@@ -300,7 +300,7 @@ public abstract class BaseFieldInfoFormatTestCase extends BaseIndexFileFormatTesif (fieldType.indexOptions() != IndexOptions.NONE) {storeTermVectors = fieldType.storeTermVectors();omitNorms = fieldType.omitNorms();- if (fieldType.indexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0) {+ if (fieldType.indexOptions().subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS)) {storePayloads = random().nextBoolean();}}@@ -357,7 +357,7 @@ public abstract class BaseFieldInfoFormatTestCase extends BaseIndexFileFormatTesif (r.nextBoolean()) {type.setStoreTermVectors(true);- if (type.indexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0) {+ if (type.indexOptions().subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS)) {type.setStoreTermVectorPositions(r.nextBoolean());type.setStoreTermVectorOffsets(r.nextBoolean());if (type.storeTermVectorPositions()) {diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/index/BasePostingsFormatTestCase.java b/lucene/test-framework/src/java/org/apache/lucene/tests/index/BasePostingsFormatTestCase.javaindex 67c4b8fc642..385a52b193e 100644--- a/lucene/test-framework/src/java/org/apache/lucene/tests/index/BasePostingsFormatTestCase.java+++ b/lucene/test-framework/src/java/org/apache/lucene/tests/index/BasePostingsFormatTestCase.java@@ -1693,7 +1693,7 @@ public abstract class BasePostingsFormatTestCase extends BaseIndexFileFormatTest@Overrideprotected void addRandomFields(Document doc) {for (IndexOptions opts : IndexOptions.values()) {- if (opts == IndexOptions.NONE) {+ if (opts == IndexOptions.NONE || opts == IndexOptions.DOCS_AND_CUSTOM_FREQS) {continue;}FieldType ft = new FieldType();diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/index/RandomPostingsTester.java b/lucene/test-framework/src/java/org/apache/lucene/tests/index/RandomPostingsTester.javaindex 6940f6599ab..d1b4c3e6214 100644--- a/lucene/test-framework/src/java/org/apache/lucene/tests/index/RandomPostingsTester.java+++ b/lucene/test-framework/src/java/org/apache/lucene/tests/index/RandomPostingsTester.java@@ -325,7 +325,7 @@ public class RandomPostingsTester {fixedPayloads = random.nextBoolean();byte[] payloadBytes = new byte[payloadSize];payload = new BytesRef(payloadBytes);- doPositions = IndexOptions.DOCS_AND_FREQS_AND_POSITIONS.compareTo(options) <= 0;+ doPositions = options.subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);}@Override@@ -562,20 +562,19 @@ public class RandomPostingsTester {@Overridepublic boolean hasFreqs() {- return fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS) >= 0;+ return fieldInfo.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS);}@Overridepublic boolean hasOffsets() {return fieldInfo- .getIndexOptions()- .compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS)- >= 0;+ .getIndexOptions()+ .subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);}@Overridepublic boolean hasPositions() {- return fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0;+ return fieldInfo.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);}@Override@@ -658,11 +657,11 @@ public class RandomPostingsTester {@Overridepublic final PostingsEnum postings(PostingsEnum reuse, int flags) throws IOException {if (PostingsEnum.featureRequested(flags, PostingsEnum.POSITIONS)) {- if (maxAllowed.compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) < 0) {+ if (!maxAllowed.subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS)) {return null;}if (PostingsEnum.featureRequested(flags, PostingsEnum.OFFSETS)- && maxAllowed.compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS) < 0) {+ && !maxAllowed.subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS)) {return null;}if (PostingsEnum.featureRequested(flags, PostingsEnum.PAYLOADS) && allowPayloads == false) {@@ -670,7 +669,7 @@ public class RandomPostingsTester {}}if (PostingsEnum.featureRequested(flags, PostingsEnum.FREQS)- && maxAllowed.compareTo(IndexOptions.DOCS_AND_FREQS) < 0) {+ && !maxAllowed.subsumes(IndexOptions.DOCS_AND_FREQS)) {return null;}return getSeedPostings(@@ -731,7 +730,7 @@ public class RandomPostingsTester {IndexOptions.values()[alwaysTestMax ? maxIndexOption : TestUtil.nextInt(random, 1, maxIndexOption)];boolean doPayloads =- indexOptions.compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0 && allowPayloads;+ indexOptions.subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) && allowPayloads;newFieldInfoArray[fieldUpto] =new FieldInfo(@@ -887,19 +886,18 @@ public class RandomPostingsTester {assertEquals(expected.docFreq, termsEnum.docFreq());boolean allowFreqs =- fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS) >= 0- && maxTestOptions.compareTo(IndexOptions.DOCS_AND_FREQS) >= 0;+ fieldInfo.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS)+ && maxTestOptions.subsumes(IndexOptions.DOCS_AND_FREQS);boolean doCheckFreqs = allowFreqs && (alwaysTestMax || random.nextInt(3) <= 2);boolean allowPositions =- fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0- && maxTestOptions.compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0;+ fieldInfo.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS)+ && maxTestOptions.subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);boolean doCheckPositions = allowPositions && (alwaysTestMax || random.nextInt(3) <= 2);boolean allowOffsets =- fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS)- >= 0- && maxTestOptions.compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS) >= 0;+ fieldInfo.getIndexOptions().subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS)+ && maxTestOptions.subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);boolean doCheckOffsets = allowOffsets && (alwaysTestMax || random.nextInt(3) <= 2);boolean doCheckPayloads =@@ -1204,10 +1202,9 @@ public class RandomPostingsTester {System.out.println(" skip check offsets");}}- } else if (fieldInfo- .getIndexOptions()- .compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS)- < 0) {+ } else if (!fieldInfo+ .getIndexOptions()+ .subsumes(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS)) {if (LuceneTestCase.VERBOSE) {System.out.println(" now check offsets are -1");}diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/search/similarities/BaseSimilarityTestCase.java b/lucene/test-framework/src/java/org/apache/lucene/tests/search/similarities/BaseSimilarityTestCase.javaindex 4dbfa9f7910..fba0941d987 100644--- a/lucene/test-framework/src/java/org/apache/lucene/tests/search/similarities/BaseSimilarityTestCase.java+++ b/lucene/test-framework/src/java/org/apache/lucene/tests/search/similarities/BaseSimilarityTestCase.java@@ -21,6 +21,8 @@ import java.util.Random;import org.apache.lucene.document.Document;import org.apache.lucene.document.FieldType;import org.apache.lucene.document.TextField;+import org.apache.lucene.index.IndexOptions;+import org.apache.lucene.index.IndexableField;import org.apache.lucene.index.LeafReader;import org.apache.lucene.search.Explanation;import org.apache.lucene.search.FieldStats;@@ -30,6 +32,7 @@ import org.apache.lucene.search.similarities.Similarity;import org.apache.lucene.search.similarities.Similarity.BulkSimScorer;import org.apache.lucene.search.similarities.Similarity.SimScorer;import org.apache.lucene.store.Directory;+import org.apache.lucene.tests.analysis.MockAnalyzer;import org.apache.lucene.tests.index.RandomIndexWriter;import org.apache.lucene.tests.search.CheckHits;import org.apache.lucene.tests.util.LuceneTestCase;@@ -56,11 +59,26 @@ public abstract class BaseSimilarityTestCase extends LuceneTestCase {public static void beforeClass() throws Exception {// with normsDIR = newDirectory();- RandomIndexWriter writer = new RandomIndexWriter(random(), DIR);- Document doc = new Document();FieldType fieldType = new FieldType(TextField.TYPE_NOT_STORED);fieldType.setOmitNorms(true);- doc.add(newField("field", "value", fieldType));+ if (random().nextBoolean()) {+ fieldType.setIndexOptions(IndexOptions.DOCS_AND_FREQS);+ } else {+ fieldType.setIndexOptions(IndexOptions.DOCS_AND_CUSTOM_FREQS);+ }+ IndexableField field = newField("field", "value", fieldType);+ RandomIndexWriter writer;+ if (field.fieldType().indexOptions() == IndexOptions.DOCS_AND_CUSTOM_FREQS) {+ writer =+ new RandomIndexWriter(+ random(),+ DIR,+ new MockAnalyzer(random(), _ -> random().nextBoolean() ? 100 : Integer.MAX_VALUE));+ } else {+ writer = new RandomIndexWriter(random(), DIR);+ }+ Document doc = new Document();+ doc.add(field);writer.addDocument(doc);READER = getOnlyLeafReader(writer.getReader());writer.close();diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/util/LuceneTestCase.java b/lucene/test-framework/src/java/org/apache/lucene/tests/util/LuceneTestCase.javaindex 5175ce4ffd1..0c618321d1e 100644--- a/lucene/test-framework/src/java/org/apache/lucene/tests/util/LuceneTestCase.java+++ b/lucene/test-framework/src/java/org/apache/lucene/tests/util/LuceneTestCase.java@@ -1557,7 +1557,7 @@ public abstract class LuceneTestCase extends Assert {newType.setStored(true); // randomly store it}if (newType.indexOptions() != IndexOptions.NONE) {- if (!newType.storeTermVectors() && random.nextBoolean()) {+ if (!newType.storeTermVectors() && random.nextBoolean() && false) {newType.setStoreTermVectors(true);if (!newType.storeTermVectorPositions()) {newType.setStoreTermVectorPositions(random.nextBoolean());@@ -1571,12 +1571,15 @@ public abstract class LuceneTestCase extends Assert {if (value instanceof String && !newType.storeTermVectorOffsets()) {newType.setStoreTermVectorOffsets(random.nextBoolean());}-if (VERBOSE) {System.out.println("NOTE: LuceneTestCase: upgrade name=" + name + " type=" + newType);}}}+ // nocommit dial back the frequency of this:+ if (!newType.storeTermVectors() && true /*random.nextBoolean()*/) {+ // can't use TERM_DOC with term vectors+ }newType.freeze();fieldToType.put(name, newType);