Skip to content

Commit

Permalink
Expose the ImpactsEnum impl in Lucene101PostingsFormat. (#14306)
Browse files Browse the repository at this point in the history
This allows access from `ScorerUtil` so that it no longer needs a static block
that creates an index to be able to introspect what implementation is used for
impacts.

Closes #14303
  • Loading branch information
jpountz authored Feb 27, 2025
1 parent 27a8d9e commit be3e39d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.lucene.codecs.PostingsWriterBase;
import org.apache.lucene.codecs.lucene90.blocktree.Lucene90BlockTreeTermsReader;
import org.apache.lucene.codecs.lucene90.blocktree.Lucene90BlockTreeTermsWriter;
import org.apache.lucene.index.ImpactsEnum;
import org.apache.lucene.index.IndexOptions;
import org.apache.lucene.index.SegmentReadState;
import org.apache.lucene.index.SegmentWriteState;
Expand Down Expand Up @@ -351,6 +352,14 @@ public final class Lucene101PostingsFormat extends PostingsFormat {

public static final int LEVEL1_MASK = LEVEL1_NUM_DOCS - 1;

/**
* Return the class that implements {@link ImpactsEnum} in this {@link PostingsFormat}. This is
* internally used to help the JVM make good inlining decisions.
*/
public static Class<? extends ImpactsEnum> getImpactsEnumImpl() {
return Lucene101PostingsReader.BlockPostingsEnum.class;
}

static final String TERMS_CODEC = "Lucene90PostingsWriterTerms";
static final String META_CODEC = "Lucene101PostingsWriterMeta";
static final String DOC_CODEC = "Lucene101PostingsWriterDoc";
Expand Down
41 changes: 5 additions & 36 deletions lucene/core/src/java/org/apache/lucene/search/ScorerUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,52 +16,21 @@
*/
package org.apache.lucene.search;

import java.io.IOException;
import java.util.stream.LongStream;
import java.util.stream.StreamSupport;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.FeatureField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.codecs.lucene101.Lucene101PostingsFormat;
import org.apache.lucene.index.ImpactsEnum;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.LeafReader;
import org.apache.lucene.index.PostingsEnum;
import org.apache.lucene.index.TermsEnum;
import org.apache.lucene.store.ByteBuffersDirectory;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.Bits;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.FixedBitSet;
import org.apache.lucene.util.PriorityQueue;

/** Util class for Scorer related methods */
class ScorerUtil {

private static final Class<?> DEFAULT_IMPACTS_ENUM_CLASS;
private static final Class<?> DEFAULT_ACCEPT_DOCS_CLASS;

static {
try (Directory dir = new ByteBuffersDirectory();
IndexWriter w = new IndexWriter(dir, new IndexWriterConfig())) {
Document doc = new Document();
doc.add(new FeatureField("field", "value", 1f));
w.addDocument(doc);
try (DirectoryReader reader = DirectoryReader.open(w)) {
LeafReader leafReader = reader.leaves().get(0).reader();
TermsEnum te = leafReader.terms("field").iterator();
if (te.seekExact(new BytesRef("value")) == false) {
throw new Error();
}
ImpactsEnum ie = te.impacts(PostingsEnum.FREQS);
DEFAULT_IMPACTS_ENUM_CLASS = ie.getClass();
}
} catch (IOException e) {
throw new Error(e);
}

DEFAULT_ACCEPT_DOCS_CLASS = new FixedBitSet(1).asReadOnlyBits().getClass();
}
private static final Class<?> DEFAULT_IMPACTS_ENUM_CLASS =
Lucene101PostingsFormat.getImpactsEnumImpl();
private static final Class<?> DEFAULT_ACCEPT_DOCS_CLASS =
new FixedBitSet(1).asReadOnlyBits().getClass();

static long costWithMinShouldMatch(LongStream costs, int numScorers, int minShouldMatch) {
// the idea here is the following: a boolean query c1,c2,...cn with minShouldMatch=m
Expand Down

0 comments on commit be3e39d

Please sign in to comment.