diff --git a/build-tools-internal/src/integTest/groovy/org/elasticsearch/gradle/internal/precommit/ThirdPartyAuditTaskFuncTest.groovy b/build-tools-internal/src/integTest/groovy/org/elasticsearch/gradle/internal/precommit/ThirdPartyAuditTaskFuncTest.groovy index bb56e93d35cec..b365624b5749a 100644 --- a/build-tools-internal/src/integTest/groovy/org/elasticsearch/gradle/internal/precommit/ThirdPartyAuditTaskFuncTest.groovy +++ b/build-tools-internal/src/integTest/groovy/org/elasticsearch/gradle/internal/precommit/ThirdPartyAuditTaskFuncTest.groovy @@ -97,7 +97,6 @@ class ThirdPartyAuditTaskFuncTest extends AbstractGradleInternalPluginFuncTest { def output = normalized(result.getOutput()) assertOutputContains(output, """\ - Forbidden APIs output: DEBUG: Classpath: [file:./build/precommit/thirdPartyAudit/thirdPartyAudit/] DEBUG: Detected Java 9 or later with module system. ERROR: Forbidden class/interface use: java.io.File [non-public internal runtime class] @@ -135,7 +134,6 @@ class ThirdPartyAuditTaskFuncTest extends AbstractGradleInternalPluginFuncTest { def output = normalized(result.getOutput()) assertOutputContains(output, """\ - Forbidden APIs output: DEBUG: Classpath: [file:./build/precommit/thirdPartyAudit/thirdPartyAudit/] DEBUG: Detected Java 9 or later with module system. DEBUG: Class 'org.apache.logging.log4j.LogManager' cannot be loaded (while looking up details about referenced class 'org.apache.logging.log4j.LogManager'). diff --git a/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/precommit/ThirdPartyAuditTask.java b/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/precommit/ThirdPartyAuditTask.java index 2293fd4342311..8f357497c9008 100644 --- a/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/precommit/ThirdPartyAuditTask.java +++ b/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/precommit/ThirdPartyAuditTask.java @@ -11,6 +11,8 @@ import org.apache.commons.io.output.NullOutputStream; import org.elasticsearch.gradle.OS; +import org.elasticsearch.gradle.VersionProperties; +import org.elasticsearch.gradle.internal.info.BuildParams; import org.gradle.api.DefaultTask; import org.gradle.api.JavaVersion; import org.gradle.api.file.ArchiveOperations; @@ -55,6 +57,8 @@ import javax.inject.Inject; +import static org.gradle.api.JavaVersion.VERSION_20; + @CacheableTask public abstract class ThirdPartyAuditTask extends DefaultTask { @@ -333,6 +337,10 @@ private String runForbiddenAPIsCli() throws IOException { spec.setExecutable(javaHome.get() + "/bin/java"); } spec.classpath(getForbiddenAPIsClasspath(), classpath); + // Enable explicitly for each release as appropriate. Just JDK 20 for now, and just the vector module. + if (isJava20()) { + spec.jvmArgs("--add-modules", "jdk.incubator.vector"); + } spec.jvmArgs("-Xmx1g"); spec.getMainClass().set("de.thetaphi.forbiddenapis.cli.CliMain"); spec.args("-f", getSignatureFile().getAbsolutePath(), "-d", getJarExpandDir(), "--debug", "--allowmissingclasses"); @@ -355,6 +363,18 @@ private String runForbiddenAPIsCli() throws IOException { return forbiddenApisOutput; } + /** Returns true iff the Java version is 20. */ + private boolean isJava20() { + if (BuildParams.getIsRuntimeJavaHomeSet()) { + if (VERSION_20.equals(BuildParams.getRuntimeJavaVersion())) { + return true; + } + } else if ("20".equals(VersionProperties.getBundledJdkMajorVersion())) { + return true; + } + return false; + } + private Set runJdkJarHellCheck() throws IOException { ByteArrayOutputStream standardOut = new ByteArrayOutputStream(); ExecResult execResult = execOperations.javaexec(spec -> { diff --git a/build-tools-internal/version.properties b/build-tools-internal/version.properties index 5ae0dbf66fa20..d9c09013be730 100644 --- a/build-tools-internal/version.properties +++ b/build-tools-internal/version.properties @@ -1,5 +1,5 @@ elasticsearch = 8.9.0 -lucene = 9.6.0 +lucene = 9.7.0-snapshot-24df30cca69 bundled_jdk_vendor = openjdk bundled_jdk = 20.0.1+9@b4887098932d415489976708ad6d1a4b diff --git a/docs/Versions.asciidoc b/docs/Versions.asciidoc index ac47d2a37c182..466dc74d19e8e 100644 --- a/docs/Versions.asciidoc +++ b/docs/Versions.asciidoc @@ -1,8 +1,8 @@ include::{docs-root}/shared/versions/stack/{source_branch}.asciidoc[] -:lucene_version: 9.6.0 -:lucene_version_path: 9_6_0 +:lucene_version: 9.7.0 +:lucene_version_path: 9_7_0 :jdk: 11.0.2 :jdk_major: 11 :build_type: tar diff --git a/docs/changelog/96433.yaml b/docs/changelog/96433.yaml new file mode 100644 index 0000000000000..04e1e9451637b --- /dev/null +++ b/docs/changelog/96433.yaml @@ -0,0 +1,5 @@ +pr: 96433 +summary: Upgrade Lucene to a 9.7.0 snapshot +area: Search +type: upgrade +issues: [] diff --git a/gradle/verification-metadata.xml b/gradle/verification-metadata.xml index 234affb4e49fb..867b636bc8059 100644 --- a/gradle/verification-metadata.xml +++ b/gradle/verification-metadata.xml @@ -2483,124 +2483,124 @@ - - - + + + - - - + + + - - - + + + - - - + + + - - - + + + - - - + + + - - - + + + - - - + + + - - - + + + - - - + + + - - - + + + - - - + + + - - - + + + - - - + + + - - - + + + - - - + + + - - - + + + - - - + + + - - - + + + - - - + + + - - - + + + - - - + + + - - - + + + - - - + + + diff --git a/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/extras/SourceConfirmedTextQuery.java b/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/extras/SourceConfirmedTextQuery.java index 86214844d8919..f351cca7f73a2 100644 --- a/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/extras/SourceConfirmedTextQuery.java +++ b/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/extras/SourceConfirmedTextQuery.java @@ -10,7 +10,6 @@ import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.index.FieldInvertState; -import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.LeafReaderContext; import org.apache.lucene.index.Term; import org.apache.lucene.index.TermStates; @@ -189,8 +188,8 @@ public void visit(QueryVisitor visitor) { } @Override - public Query rewrite(IndexReader reader) throws IOException { - Query inRewritten = in.rewrite(reader); + public Query rewrite(IndexSearcher searcher) throws IOException { + Query inRewritten = in.rewrite(searcher); if (inRewritten != in) { return new SourceConfirmedTextQuery(inRewritten, valueFetcherProvider, indexAnalyzer); } else if (in instanceof ConstantScoreQuery) { @@ -203,7 +202,7 @@ public Query rewrite(IndexReader reader) throws IOException { } else if (in instanceof MatchNoDocsQuery) { return in; // e.g. empty phrase query } - return super.rewrite(reader); + return super.rewrite(searcher); } @Override diff --git a/modules/parent-join/src/main/java/org/elasticsearch/join/query/HasChildQueryBuilder.java b/modules/parent-join/src/main/java/org/elasticsearch/join/query/HasChildQueryBuilder.java index 1095ffaba8bc9..9738bbd43c31a 100644 --- a/modules/parent-join/src/main/java/org/elasticsearch/join/query/HasChildQueryBuilder.java +++ b/modules/parent-join/src/main/java/org/elasticsearch/join/query/HasChildQueryBuilder.java @@ -398,11 +398,12 @@ public static final class LateParsingQuery extends Query { } @Override - public Query rewrite(IndexReader reader) throws IOException { - Query rewritten = super.rewrite(reader); + public Query rewrite(IndexSearcher searcher) throws IOException { + Query rewritten = super.rewrite(searcher); if (rewritten != this) { return rewritten; } + IndexReader reader = searcher.getIndexReader(); if (reader instanceof DirectoryReader) { IndexSearcher indexSearcher = new IndexSearcher(reader); indexSearcher.setQueryCache(null); @@ -428,7 +429,7 @@ public Query rewrite(IndexReader reader) throws IOException { return new MatchNoDocsQuery("Can't load against an empty reader"); } throw new IllegalStateException( - "can't load global ordinals for reader of type: " + reader.getClass() + " must be a DirectoryReader" + "can't load global ordinals for reader of type: " + searcher.getClass() + " must be a DirectoryReader" ); } } diff --git a/modules/percolator/src/main/java/org/elasticsearch/percolator/PercolateQuery.java b/modules/percolator/src/main/java/org/elasticsearch/percolator/PercolateQuery.java index e85c7b88f4a80..11c726481d0b3 100644 --- a/modules/percolator/src/main/java/org/elasticsearch/percolator/PercolateQuery.java +++ b/modules/percolator/src/main/java/org/elasticsearch/percolator/PercolateQuery.java @@ -8,7 +8,6 @@ package org.elasticsearch.percolator; -import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.LeafReaderContext; import org.apache.lucene.search.BooleanClause.Occur; import org.apache.lucene.search.BooleanQuery; @@ -65,8 +64,8 @@ final class PercolateQuery extends Query implements Accountable { } @Override - public Query rewrite(IndexReader reader) throws IOException { - Query rewritten = candidateMatchesQuery.rewrite(reader); + public Query rewrite(IndexSearcher searcher) throws IOException { + Query rewritten = candidateMatchesQuery.rewrite(searcher); if (rewritten != candidateMatchesQuery) { return new PercolateQuery( name, diff --git a/modules/percolator/src/test/java/org/elasticsearch/percolator/CandidateQueryTests.java b/modules/percolator/src/test/java/org/elasticsearch/percolator/CandidateQueryTests.java index 52422a35a4079..bf20674e64aa5 100644 --- a/modules/percolator/src/test/java/org/elasticsearch/percolator/CandidateQueryTests.java +++ b/modules/percolator/src/test/java/org/elasticsearch/percolator/CandidateQueryTests.java @@ -1225,7 +1225,7 @@ private CustomQuery(Term term) { } @Override - public Query rewrite(IndexReader reader) throws IOException { + public Query rewrite(IndexSearcher searcher) throws IOException { return new TermQuery(term); } diff --git a/server/src/main/java/org/elasticsearch/Version.java b/server/src/main/java/org/elasticsearch/Version.java index 82dd3a3b7780e..83350471a4a83 100644 --- a/server/src/main/java/org/elasticsearch/Version.java +++ b/server/src/main/java/org/elasticsearch/Version.java @@ -140,7 +140,7 @@ public class Version implements Comparable, ToXContentFragment { public static final Version V_8_7_1 = new Version(8_07_01_99, org.apache.lucene.util.Version.LUCENE_9_5_0); public static final Version V_8_8_0 = new Version(8_08_00_99, org.apache.lucene.util.Version.LUCENE_9_6_0); public static final Version V_8_8_1 = new Version(8_08_01_99, org.apache.lucene.util.Version.LUCENE_9_6_0); - public static final Version V_8_9_0 = new Version(8_09_00_99, org.apache.lucene.util.Version.LUCENE_9_6_0); + public static final Version V_8_9_0 = new Version(8_09_00_99, org.apache.lucene.util.Version.LUCENE_9_7_0); public static final Version CURRENT = V_8_9_0; private static final NavigableMap VERSION_IDS; diff --git a/server/src/main/java/org/elasticsearch/common/lucene/search/MoreLikeThisQuery.java b/server/src/main/java/org/elasticsearch/common/lucene/search/MoreLikeThisQuery.java index d16e927efc973..594e414c3a7ed 100644 --- a/server/src/main/java/org/elasticsearch/common/lucene/search/MoreLikeThisQuery.java +++ b/server/src/main/java/org/elasticsearch/common/lucene/search/MoreLikeThisQuery.java @@ -12,12 +12,12 @@ import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.analysis.tokenattributes.CharTermAttribute; import org.apache.lucene.index.Fields; -import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.Term; import org.apache.lucene.index.Terms; import org.apache.lucene.index.TermsEnum; import org.apache.lucene.search.BooleanClause; import org.apache.lucene.search.BooleanQuery; +import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; import org.apache.lucene.search.QueryVisitor; import org.apache.lucene.search.similarities.ClassicSimilarity; @@ -116,12 +116,12 @@ public boolean equals(Object obj) { } @Override - public Query rewrite(IndexReader reader) throws IOException { - Query rewritten = super.rewrite(reader); + public Query rewrite(IndexSearcher searcher) throws IOException { + Query rewritten = super.rewrite(searcher); if (rewritten != this) { return rewritten; } - XMoreLikeThis mlt = new XMoreLikeThis(reader, similarity == null ? new ClassicSimilarity() : similarity); + XMoreLikeThis mlt = new XMoreLikeThis(searcher.getIndexReader(), similarity == null ? new ClassicSimilarity() : similarity); mlt.setFieldNames(moreLikeFields); mlt.setAnalyzer(analyzer); diff --git a/server/src/main/java/org/elasticsearch/common/lucene/search/MultiPhrasePrefixQuery.java b/server/src/main/java/org/elasticsearch/common/lucene/search/MultiPhrasePrefixQuery.java index 376ed6b2626b5..545fbf6cb7b59 100644 --- a/server/src/main/java/org/elasticsearch/common/lucene/search/MultiPhrasePrefixQuery.java +++ b/server/src/main/java/org/elasticsearch/common/lucene/search/MultiPhrasePrefixQuery.java @@ -14,6 +14,7 @@ import org.apache.lucene.index.Terms; import org.apache.lucene.index.TermsEnum; import org.apache.lucene.search.BooleanClause; +import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.MatchNoDocsQuery; import org.apache.lucene.search.MultiPhraseQuery; import org.apache.lucene.search.Query; @@ -129,8 +130,8 @@ public int[] getPositions() { } @Override - public Query rewrite(IndexReader reader) throws IOException { - Query rewritten = super.rewrite(reader); + public Query rewrite(IndexSearcher searcher) throws IOException { + Query rewritten = super.rewrite(searcher); if (rewritten != this) { return rewritten; } @@ -147,7 +148,7 @@ public Query rewrite(IndexReader reader) throws IOException { int position = positions.get(sizeMinus1); Set terms = new HashSet<>(); for (Term term : suffixTerms) { - getPrefixTerms(terms, term, reader); + getPrefixTerms(terms, term, searcher.getIndexReader()); if (terms.size() > maxExpansions) { break; } diff --git a/server/src/main/java/org/elasticsearch/common/lucene/search/function/FunctionScoreQuery.java b/server/src/main/java/org/elasticsearch/common/lucene/search/function/FunctionScoreQuery.java index 9b913aebb9594..aed11297d4285 100644 --- a/server/src/main/java/org/elasticsearch/common/lucene/search/function/FunctionScoreQuery.java +++ b/server/src/main/java/org/elasticsearch/common/lucene/search/function/FunctionScoreQuery.java @@ -77,7 +77,7 @@ protected int doHashCode() { @Override protected ScoreFunction rewrite(IndexReader reader) throws IOException { - Query newFilter = filter.rewrite(reader); + Query newFilter = filter.rewrite(new IndexSearcher(reader)); if (newFilter == filter) { return this; } @@ -201,16 +201,16 @@ public void visit(QueryVisitor visitor) { } @Override - public Query rewrite(IndexReader reader) throws IOException { - Query rewritten = super.rewrite(reader); + public Query rewrite(IndexSearcher searcher) throws IOException { + Query rewritten = super.rewrite(searcher); if (rewritten != this) { return rewritten; } - Query newQ = subQuery.rewrite(reader); + Query newQ = subQuery.rewrite(searcher); ScoreFunction[] newFunctions = new ScoreFunction[functions.length]; boolean needsRewrite = (newQ != subQuery); for (int i = 0; i < functions.length; i++) { - newFunctions[i] = functions[i].rewrite(reader); + newFunctions[i] = functions[i].rewrite(searcher.getIndexReader()); needsRewrite |= (newFunctions[i] != functions[i]); } if (needsRewrite) { diff --git a/server/src/main/java/org/elasticsearch/common/lucene/search/function/ScriptScoreQuery.java b/server/src/main/java/org/elasticsearch/common/lucene/search/function/ScriptScoreQuery.java index ee35a9a25af1a..77154eda0a649 100644 --- a/server/src/main/java/org/elasticsearch/common/lucene/search/function/ScriptScoreQuery.java +++ b/server/src/main/java/org/elasticsearch/common/lucene/search/function/ScriptScoreQuery.java @@ -8,7 +8,6 @@ package org.elasticsearch.common.lucene.search.function; -import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.LeafReaderContext; import org.apache.lucene.search.BooleanClause; import org.apache.lucene.search.BulkScorer; @@ -69,12 +68,12 @@ public ScriptScoreQuery( } @Override - public Query rewrite(IndexReader reader) throws IOException { - Query newQ = subQuery.rewrite(reader); + public Query rewrite(IndexSearcher searcher) throws IOException { + Query newQ = subQuery.rewrite(searcher); if (newQ != subQuery) { return new ScriptScoreQuery(newQ, script, scriptBuilder, lookup, minScore, indexName, shardId, indexVersion); } - return super.rewrite(reader); + return super.rewrite(searcher); } @Override diff --git a/server/src/main/java/org/elasticsearch/index/query/DateRangeIncludingNowQuery.java b/server/src/main/java/org/elasticsearch/index/query/DateRangeIncludingNowQuery.java index 3c1bbefcacca7..449275fecc3c9 100644 --- a/server/src/main/java/org/elasticsearch/index/query/DateRangeIncludingNowQuery.java +++ b/server/src/main/java/org/elasticsearch/index/query/DateRangeIncludingNowQuery.java @@ -8,8 +8,8 @@ package org.elasticsearch.index.query; -import org.apache.lucene.index.IndexReader; import org.apache.lucene.search.BooleanClause; +import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; import org.apache.lucene.search.QueryVisitor; @@ -34,7 +34,7 @@ public Query getQuery() { } @Override - public Query rewrite(IndexReader reader) throws IOException { + public Query rewrite(IndexSearcher searcher) throws IOException { return in; } diff --git a/server/src/main/java/org/elasticsearch/index/search/ESToParentBlockJoinQuery.java b/server/src/main/java/org/elasticsearch/index/search/ESToParentBlockJoinQuery.java index 67ec03b291548..f798ba6270271 100644 --- a/server/src/main/java/org/elasticsearch/index/search/ESToParentBlockJoinQuery.java +++ b/server/src/main/java/org/elasticsearch/index/search/ESToParentBlockJoinQuery.java @@ -8,7 +8,6 @@ package org.elasticsearch.index.search; -import org.apache.lucene.index.IndexReader; import org.apache.lucene.search.BooleanClause; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; @@ -54,8 +53,8 @@ public ScoreMode getScoreMode() { } @Override - public Query rewrite(IndexReader reader) throws IOException { - Query innerRewrite = query.rewrite(reader); + public Query rewrite(IndexSearcher searcher) throws IOException { + Query innerRewrite = query.rewrite(searcher); if (innerRewrite != query) { // Right now ToParentBlockJoinQuery always rewrites to a ToParentBlockJoinQuery // so the else block will never be used. It is useful in the case that @@ -69,7 +68,7 @@ public Query rewrite(IndexReader reader) throws IOException { return innerRewrite; } } - return super.rewrite(reader); + return super.rewrite(searcher); } @Override diff --git a/server/src/main/java/org/elasticsearch/lucene/queries/BlendedTermQuery.java b/server/src/main/java/org/elasticsearch/lucene/queries/BlendedTermQuery.java index 20cc5b411b63b..a49f02acf4c4d 100644 --- a/server/src/main/java/org/elasticsearch/lucene/queries/BlendedTermQuery.java +++ b/server/src/main/java/org/elasticsearch/lucene/queries/BlendedTermQuery.java @@ -16,6 +16,7 @@ import org.apache.lucene.search.BooleanClause; import org.apache.lucene.search.BoostQuery; import org.apache.lucene.search.DisjunctionMaxQuery; +import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; import org.apache.lucene.search.QueryVisitor; import org.apache.lucene.search.TermQuery; @@ -67,11 +68,12 @@ public BlendedTermQuery(Term[] terms, float[] boosts) { } @Override - public Query rewrite(IndexReader reader) throws IOException { - Query rewritten = super.rewrite(reader); + public Query rewrite(IndexSearcher searcher) throws IOException { + Query rewritten = super.rewrite(searcher); if (rewritten != this) { return rewritten; } + IndexReader reader = searcher.getIndexReader(); IndexReaderContext context = reader.getContext(); TermStates[] ctx = new TermStates[terms.length]; int[] docFreqs = new int[ctx.length]; diff --git a/server/src/main/java/org/elasticsearch/lucene/queries/MinDocQuery.java b/server/src/main/java/org/elasticsearch/lucene/queries/MinDocQuery.java index b65762374ebca..0e2f05cc12e4f 100644 --- a/server/src/main/java/org/elasticsearch/lucene/queries/MinDocQuery.java +++ b/server/src/main/java/org/elasticsearch/lucene/queries/MinDocQuery.java @@ -59,7 +59,8 @@ public boolean equals(Object obj) { } @Override - public Query rewrite(IndexReader reader) throws IOException { + public Query rewrite(IndexSearcher searcher) throws IOException { + IndexReader reader = searcher.getIndexReader(); if (Objects.equals(reader.getContext().id(), readerId) == false) { return new MinDocQuery(minDoc, reader.getContext().id()); } diff --git a/server/src/main/java/org/elasticsearch/lucene/search/vectorhighlight/CustomFieldQuery.java b/server/src/main/java/org/elasticsearch/lucene/search/vectorhighlight/CustomFieldQuery.java index a15cf181fd584..7e1478f28dddc 100644 --- a/server/src/main/java/org/elasticsearch/lucene/search/vectorhighlight/CustomFieldQuery.java +++ b/server/src/main/java/org/elasticsearch/lucene/search/vectorhighlight/CustomFieldQuery.java @@ -14,6 +14,7 @@ import org.apache.lucene.sandbox.search.CombinedFieldQuery; import org.apache.lucene.search.BoostQuery; import org.apache.lucene.search.ConstantScoreQuery; +import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.MultiPhraseQuery; import org.apache.lucene.search.PhraseQuery; import org.apache.lucene.search.Query; @@ -45,43 +46,43 @@ public CustomFieldQuery(Query query, IndexReader reader, boolean phraseHighlight } @Override - protected void flatten(Query sourceQuery, IndexReader reader, Collection flatQueries, float boost) throws IOException { + protected void flatten(Query sourceQuery, IndexSearcher searcher, Collection flatQueries, float boost) throws IOException { if (sourceQuery instanceof BoostQuery bq) { sourceQuery = bq.getQuery(); boost *= bq.getBoost(); - flatten(sourceQuery, reader, flatQueries, boost); + flatten(sourceQuery, searcher, flatQueries, boost); } else if (sourceQuery instanceof SpanTermQuery) { - super.flatten(new TermQuery(((SpanTermQuery) sourceQuery).getTerm()), reader, flatQueries, boost); + super.flatten(new TermQuery(((SpanTermQuery) sourceQuery).getTerm()), searcher, flatQueries, boost); } else if (sourceQuery instanceof ConstantScoreQuery) { - flatten(((ConstantScoreQuery) sourceQuery).getQuery(), reader, flatQueries, boost); + flatten(((ConstantScoreQuery) sourceQuery).getQuery(), searcher, flatQueries, boost); } else if (sourceQuery instanceof FunctionScoreQuery) { - flatten(((FunctionScoreQuery) sourceQuery).getSubQuery(), reader, flatQueries, boost); + flatten(((FunctionScoreQuery) sourceQuery).getSubQuery(), searcher, flatQueries, boost); } else if (sourceQuery instanceof MultiPhrasePrefixQuery) { - flatten(sourceQuery.rewrite(reader), reader, flatQueries, boost); + flatten(sourceQuery.rewrite(searcher), searcher, flatQueries, boost); } else if (sourceQuery instanceof MultiPhraseQuery q) { - convertMultiPhraseQuery(0, new int[q.getTermArrays().length], q, q.getTermArrays(), q.getPositions(), reader, flatQueries); + convertMultiPhraseQuery(0, new int[q.getTermArrays().length], q, q.getTermArrays(), q.getPositions(), searcher, flatQueries); } else if (sourceQuery instanceof BlendedTermQuery blendedTermQuery) { - flatten(blendedTermQuery.rewrite(reader), reader, flatQueries, boost); + flatten(blendedTermQuery.rewrite(searcher), searcher, flatQueries, boost); } else if (sourceQuery instanceof org.apache.lucene.queries.function.FunctionScoreQuery funcScoreQuery) { // flatten query with query boost - flatten(funcScoreQuery.getWrappedQuery(), reader, flatQueries, boost); + flatten(funcScoreQuery.getWrappedQuery(), searcher, flatQueries, boost); } else if (sourceQuery instanceof SynonymQuery synQuery) { // SynonymQuery should be handled by the parent class directly. // This statement should be removed when https://issues.apache.org/jira/browse/LUCENE-7484 is merged. for (Term term : synQuery.getTerms()) { - flatten(new TermQuery(term), reader, flatQueries, boost); + flatten(new TermQuery(term), searcher, flatQueries, boost); } } else if (sourceQuery instanceof CombinedFieldQuery combinedFieldQuery) { for (Term term : combinedFieldQuery.getTerms()) { - flatten(new TermQuery(term), reader, flatQueries, boost); + flatten(new TermQuery(term), searcher, flatQueries, boost); } } else if (sourceQuery instanceof ESToParentBlockJoinQuery) { Query childQuery = ((ESToParentBlockJoinQuery) sourceQuery).getChildQuery(); if (childQuery != null) { - flatten(childQuery, reader, flatQueries, boost); + flatten(childQuery, searcher, flatQueries, boost); } } else { - super.flatten(sourceQuery, reader, flatQueries, boost); + super.flatten(sourceQuery, searcher, flatQueries, boost); } } @@ -91,7 +92,7 @@ private void convertMultiPhraseQuery( MultiPhraseQuery orig, Term[][] terms, int[] pos, - IndexReader reader, + IndexSearcher searcher, Collection flatQueries ) throws IOException { if (currentPos == 0) { @@ -103,7 +104,7 @@ private void convertMultiPhraseQuery( if (numTerms > 16) { for (Term[] currentPosTerm : terms) { for (Term term : currentPosTerm) { - super.flatten(new TermQuery(term), reader, flatQueries, 1F); + super.flatten(new TermQuery(term), searcher, flatQueries, 1F); } } return; @@ -120,12 +121,12 @@ private void convertMultiPhraseQuery( queryBuilder.add(terms[i][termsIdx[i]], pos[i]); } Query query = queryBuilder.build(); - this.flatten(query, reader, flatQueries, 1F); + this.flatten(query, searcher, flatQueries, 1F); } else { Term[] t = terms[currentPos]; for (int i = 0; i < t.length; i++) { termsIdx[currentPos] = i; - convertMultiPhraseQuery(currentPos + 1, termsIdx, orig, terms, pos, reader, flatQueries); + convertMultiPhraseQuery(currentPos + 1, termsIdx, orig, terms, pos, searcher, flatQueries); } } } diff --git a/server/src/main/java/org/elasticsearch/search/suggest/completion/CompletionSuggester.java b/server/src/main/java/org/elasticsearch/search/suggest/completion/CompletionSuggester.java index c2a233645a596..c967d7683f0b2 100644 --- a/server/src/main/java/org/elasticsearch/search/suggest/completion/CompletionSuggester.java +++ b/server/src/main/java/org/elasticsearch/search/suggest/completion/CompletionSuggester.java @@ -74,7 +74,7 @@ protected Suggest.Suggestion query.rewrite(reader)); + IndexSearcher searcher = new IndexSearcher(reader); + Query query = queryBuilder.toQuery(createSearchExecutionContext(searcher)); + RuntimeException exc = expectThrows(RuntimeException.class, () -> query.rewrite(searcher)); assertThat(exc.getMessage(), containsString("maxClauseCount")); } finally { BooleanQuery.setMaxClauseCount(origBoolMaxClauseCount); diff --git a/server/src/test/java/org/elasticsearch/index/search/ESToParentBlockJoinQueryTests.java b/server/src/test/java/org/elasticsearch/index/search/ESToParentBlockJoinQueryTests.java index 3868e87efa373..b7349f1033a5e 100644 --- a/server/src/test/java/org/elasticsearch/index/search/ESToParentBlockJoinQueryTests.java +++ b/server/src/test/java/org/elasticsearch/index/search/ESToParentBlockJoinQueryTests.java @@ -10,6 +10,7 @@ import org.apache.lucene.index.MultiReader; import org.apache.lucene.index.Term; +import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.PhraseQuery; import org.apache.lucene.search.Query; import org.apache.lucene.search.TermQuery; @@ -88,7 +89,7 @@ public void testRewrite() throws IOException { ScoreMode.Avg, "nested" ); - Query rewritten = q.rewrite(new MultiReader()); + Query rewritten = q.rewrite(new IndexSearcher(new MultiReader())); assertEquals(expected, rewritten); } } diff --git a/server/src/test/java/org/elasticsearch/lucene/queries/MinDocQueryTests.java b/server/src/test/java/org/elasticsearch/lucene/queries/MinDocQueryTests.java index f1e03c42e0487..3fa292acc50e8 100644 --- a/server/src/test/java/org/elasticsearch/lucene/queries/MinDocQueryTests.java +++ b/server/src/test/java/org/elasticsearch/lucene/queries/MinDocQueryTests.java @@ -37,10 +37,11 @@ public void testBasics() { public void testRewrite() throws Exception { IndexReader reader = new MultiReader(); + IndexSearcher searcher = new IndexSearcher(reader); MinDocQuery query = new MinDocQuery(42); - Query rewritten = query.rewrite(reader); + Query rewritten = query.rewrite(searcher); QueryUtils.checkUnequal(query, rewritten); - Query rewritten2 = rewritten.rewrite(reader); + Query rewritten2 = rewritten.rewrite(searcher); assertSame(rewritten, rewritten2); } diff --git a/server/src/test/java/org/elasticsearch/lucene/queries/SpanMatchNoDocsQueryTests.java b/server/src/test/java/org/elasticsearch/lucene/queries/SpanMatchNoDocsQueryTests.java index 6fc5e841e4e02..1f9bf525b44b4 100644 --- a/server/src/test/java/org/elasticsearch/lucene/queries/SpanMatchNoDocsQueryTests.java +++ b/server/src/test/java/org/elasticsearch/lucene/queries/SpanMatchNoDocsQueryTests.java @@ -33,7 +33,7 @@ public class SpanMatchNoDocsQueryTests extends ESTestCase { public void testSimple() throws Exception { SpanMatchNoDocsQuery query = new SpanMatchNoDocsQuery("field", "a good reason"); assertEquals(query.toString(), "SpanMatchNoDocsQuery(\"a good reason\")"); - Query rewrite = query.rewrite(null); + Query rewrite = query.rewrite((IndexSearcher) null); assertTrue(rewrite instanceof SpanMatchNoDocsQuery); assertEquals(rewrite.toString(), "SpanMatchNoDocsQuery(\"a good reason\")"); } @@ -68,7 +68,7 @@ public void testQuery() throws Exception { assertEquals(searcher.count(orQuery), 1); hits = searcher.search(orQuery, 1000).scoreDocs; assertEquals(1, hits.length); - Query rewrite = orQuery.rewrite(ir); + Query rewrite = orQuery.rewrite(searcher); assertEquals(rewrite, orQuery); SpanNearQuery nearQuery = new SpanNearQuery( @@ -79,7 +79,7 @@ public void testQuery() throws Exception { assertEquals(searcher.count(nearQuery), 0); hits = searcher.search(nearQuery, 1000).scoreDocs; assertEquals(0, hits.length); - rewrite = nearQuery.rewrite(ir); + rewrite = nearQuery.rewrite(searcher); assertEquals(rewrite, nearQuery); iw.close(); diff --git a/server/src/test/java/org/elasticsearch/search/internal/ContextIndexSearcherTests.java b/server/src/test/java/org/elasticsearch/search/internal/ContextIndexSearcherTests.java index 137f0c5447611..c0758531988e3 100644 --- a/server/src/test/java/org/elasticsearch/search/internal/ContextIndexSearcherTests.java +++ b/server/src/test/java/org/elasticsearch/search/internal/ContextIndexSearcherTests.java @@ -17,7 +17,6 @@ import org.apache.lucene.index.DirectoryReader; import org.apache.lucene.index.FilterDirectoryReader; import org.apache.lucene.index.FilterLeafReader; -import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.index.LeafReader; @@ -576,12 +575,12 @@ public String toString(String field) { } @Override - public Query rewrite(IndexReader reader) throws IOException { - Query queryRewritten = query.rewrite(reader); + public Query rewrite(IndexSearcher searcher) throws IOException { + Query queryRewritten = query.rewrite(searcher); if (query != queryRewritten) { return new CreateScorerOnceQuery(queryRewritten); } - return super.rewrite(reader); + return super.rewrite(searcher); } @Override diff --git a/x-pack/plugin/search-business-rules/src/main/java/org/elasticsearch/xpack/searchbusinessrules/CappedScoreQuery.java b/x-pack/plugin/search-business-rules/src/main/java/org/elasticsearch/xpack/searchbusinessrules/CappedScoreQuery.java index 614172a7edd73..d9e65c385c610 100644 --- a/x-pack/plugin/search-business-rules/src/main/java/org/elasticsearch/xpack/searchbusinessrules/CappedScoreQuery.java +++ b/x-pack/plugin/search-business-rules/src/main/java/org/elasticsearch/xpack/searchbusinessrules/CappedScoreQuery.java @@ -6,7 +6,6 @@ */ package org.elasticsearch.xpack.searchbusinessrules; -import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.LeafReaderContext; import org.apache.lucene.search.BooleanClause; import org.apache.lucene.search.BoostQuery; @@ -56,8 +55,8 @@ public void visit(QueryVisitor visitor) { } @Override - public Query rewrite(IndexReader reader) throws IOException { - Query rewritten = query.rewrite(reader); + public Query rewrite(IndexSearcher searcher) throws IOException { + Query rewritten = query.rewrite(searcher); if (rewritten != query) { return new CappedScoreQuery(rewritten, maxScore); @@ -71,7 +70,7 @@ public Query rewrite(IndexReader reader) throws IOException { return new CappedScoreQuery(((BoostQuery) rewritten).getQuery(), maxScore); } - return super.rewrite(reader); + return super.rewrite(searcher); } /** diff --git a/x-pack/plugin/wildcard/src/main/java/org/elasticsearch/xpack/wildcard/mapper/BinaryDvConfirmedAutomatonQuery.java b/x-pack/plugin/wildcard/src/main/java/org/elasticsearch/xpack/wildcard/mapper/BinaryDvConfirmedAutomatonQuery.java index f2836e10e4ed8..608e5f1972373 100644 --- a/x-pack/plugin/wildcard/src/main/java/org/elasticsearch/xpack/wildcard/mapper/BinaryDvConfirmedAutomatonQuery.java +++ b/x-pack/plugin/wildcard/src/main/java/org/elasticsearch/xpack/wildcard/mapper/BinaryDvConfirmedAutomatonQuery.java @@ -9,7 +9,6 @@ import org.apache.lucene.index.BinaryDocValues; import org.apache.lucene.index.DocValues; -import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.LeafReaderContext; import org.apache.lucene.search.ConstantScoreScorer; import org.apache.lucene.search.ConstantScoreWeight; @@ -55,8 +54,8 @@ private BinaryDvConfirmedAutomatonQuery(Query approximation, String field, Strin } @Override - public Query rewrite(IndexReader reader) throws IOException { - Query approxRewrite = approxQuery.rewrite(reader); + public Query rewrite(IndexSearcher searcher) throws IOException { + Query approxRewrite = approxQuery.rewrite(searcher); if (approxQuery != approxRewrite) { return new BinaryDvConfirmedAutomatonQuery(approxRewrite, field, matchPattern, bytesMatcher); } diff --git a/x-pack/plugin/wildcard/src/test/java/org/elasticsearch/xpack/wildcard/mapper/WildcardFieldMapperTests.java b/x-pack/plugin/wildcard/src/test/java/org/elasticsearch/xpack/wildcard/mapper/WildcardFieldMapperTests.java index f963c55f6602f..b9133ff3bce73 100644 --- a/x-pack/plugin/wildcard/src/test/java/org/elasticsearch/xpack/wildcard/mapper/WildcardFieldMapperTests.java +++ b/x-pack/plugin/wildcard/src/test/java/org/elasticsearch/xpack/wildcard/mapper/WildcardFieldMapperTests.java @@ -259,7 +259,7 @@ public void testTooBigQueryField() throws IOException { iw.close(); // Test wildcard query - String queryString = randomABString((BooleanQuery.getMaxClauseCount() * 2) + 1); + String queryString = randomABString((IndexSearcher.getMaxClauseCount() * 2) + 1); Query wildcardFieldQuery = wildcardFieldType.fieldType().wildcardQuery(queryString, null, null); TopDocs wildcardFieldTopDocs = searcher.search(wildcardFieldQuery, 10, Sort.INDEXORDER); assertThat(wildcardFieldTopDocs.totalHits.value, equalTo(0L)); @@ -898,7 +898,7 @@ protected Query getSimplifiedApproximationQuery(Query approximationQuery) throws int numRewrites = 0; int maxNumRewrites = 100; for (; numRewrites < maxNumRewrites; numRewrites++) { - Query newApprox = approximationQuery.rewrite(rewriteReader); + Query newApprox = approximationQuery.rewrite(new IndexSearcher(rewriteReader)); if (newApprox == approximationQuery) { break; }