Make CandidateMatcher functions public (#13632)

A number of functions in CandidateMatcher are protected or package-protected,
meaning that client code can't use them, which makes it difficult to build custom 
wrapper matchers.  This commit makes these functions public
This commit is contained in:
bjacobowitz 2024-08-07 10:28:06 -04:00 committed by GitHub
parent 9e831ee809
commit 926d8f4ce6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 79 additions and 12 deletions

View File

@ -110,6 +110,8 @@ API Changes
* GITHUB#13499: Remove deprecated TopScoreDocCollector + TopFieldCollector methods (#create, #createSharedManager) (Jakub Slowinski)
* GITHUB#13632: CandidateMatcher public matching functions (Bryan Jacobowitz)
New Features
---------------------

View File

@ -64,7 +64,7 @@ public abstract class CandidateMatcher<T extends QueryMatch> {
* @param metadata the query metadata
* @throws IOException on IO errors
*/
protected abstract void matchQuery(String queryId, Query matchQuery, Map<String, String> metadata)
public abstract void matchQuery(String queryId, Query matchQuery, Map<String, String> metadata)
throws IOException;
/**
@ -95,14 +95,14 @@ public abstract class CandidateMatcher<T extends QueryMatch> {
public abstract T resolve(T match1, T match2);
/** Called by the Monitor if running a query throws an Exception */
void reportError(String queryId, Exception e) {
public void reportError(String queryId, Exception e) {
this.errors.put(queryId, e);
}
/**
* @return the matches from this matcher
*/
final MultiMatchingQueries<T> finish(long buildTime, int queryCount) {
public final MultiMatchingQueries<T> finish(long buildTime, int queryCount) {
doFinish();
this.searchTime =
TimeUnit.MILLISECONDS.convert(System.nanoTime() - searchTime, TimeUnit.NANOSECONDS);

View File

@ -35,7 +35,7 @@ abstract class CollectingMatcher<T extends QueryMatch> extends CandidateMatcher<
}
@Override
protected void matchQuery(final String queryId, Query matchQuery, Map<String, String> metadata)
public void matchQuery(final String queryId, Query matchQuery, Map<String, String> metadata)
throws IOException {
searcher.search(matchQuery, new MatchCollector(queryId, scoreMode));
}

View File

@ -31,8 +31,8 @@ public class ExplainingMatch extends QueryMatch {
searcher ->
new CandidateMatcher<ExplainingMatch>(searcher) {
@Override
protected void matchQuery(
String queryId, Query matchQuery, Map<String, String> metadata) throws IOException {
public void matchQuery(String queryId, Query matchQuery, Map<String, String> metadata)
throws IOException {
int maxDocs = searcher.getIndexReader().maxDoc();
for (int i = 0; i < maxDocs; i++) {
Explanation explanation = searcher.explain(matchQuery, i);

View File

@ -46,8 +46,8 @@ public class HighlightsMatch extends QueryMatch {
new CandidateMatcher<HighlightsMatch>(searcher) {
@Override
protected void matchQuery(
String queryId, Query matchQuery, Map<String, String> metadata) throws IOException {
public void matchQuery(String queryId, Query matchQuery, Map<String, String> metadata)
throws IOException {
Weight w =
searcher.createWeight(
searcher.rewrite(matchQuery), ScoreMode.COMPLETE_NO_SCORES, 1);

View File

@ -74,7 +74,7 @@ public class ParallelMatcher<T extends QueryMatch> extends CandidateMatcher<T> {
}
@Override
protected void matchQuery(String queryId, Query matchQuery, Map<String, String> metadata)
public void matchQuery(String queryId, Query matchQuery, Map<String, String> metadata)
throws IOException {
try {
queue.put(new MatcherTask(queryId, matchQuery, metadata));

View File

@ -79,7 +79,7 @@ public class PartitionMatcher<T extends QueryMatch> extends CandidateMatcher<T>
}
@Override
protected void matchQuery(String queryId, Query matchQuery, Map<String, String> metadata) {
public void matchQuery(String queryId, Query matchQuery, Map<String, String> metadata) {
tasks.add(new MatchTask(queryId, matchQuery, metadata));
}

View File

@ -39,7 +39,7 @@ public interface QueryTimeListener {
CandidateMatcher<T> matcher = factory.createMatcher(searcher);
return new CandidateMatcher<T>(searcher) {
@Override
protected void matchQuery(String queryId, Query matchQuery, Map<String, String> metadata)
public void matchQuery(String queryId, Query matchQuery, Map<String, String> metadata)
throws IOException {
long t = System.nanoTime();
matcher.matchQuery(queryId, matchQuery, metadata);

View File

@ -178,7 +178,7 @@ public class TestMonitor extends MonitorTestBase {
docs ->
new CandidateMatcher<QueryMatch>(docs) {
@Override
protected void matchQuery(
public void matchQuery(
String queryId, Query matchQuery, Map<String, String> metadata) {
assertEquals("value", metadata.get("key"));
}

View File

@ -0,0 +1,65 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.lucene.monitor.outsidepackage;
import java.io.IOException;
import java.util.Collections;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.memory.MemoryIndex;
import org.apache.lucene.monitor.CandidateMatcher;
import org.apache.lucene.monitor.QueryMatch;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.TermQuery;
import org.junit.Test;
public class TestCandidateMatcherVisibility {
private CandidateMatcher<QueryMatch> newCandidateMatcher() {
// Index and searcher for use in creating a matcher
MemoryIndex index = new MemoryIndex();
final IndexSearcher searcher = index.createSearcher();
return QueryMatch.SIMPLE_MATCHER.createMatcher(searcher);
}
@Test
public void testMatchQueryVisibleOutsidePackage() throws IOException {
CandidateMatcher<QueryMatch> matcher = newCandidateMatcher();
// This should compile from outside org.apache.lucene.monitor package
// (subpackage org.apache.lucene.monitor.outsidepackage cannot access package-private content
// from org.apache.lucene.monitor)
matcher.matchQuery("test", new TermQuery(new Term("test_field")), Collections.emptyMap());
}
@Test
public void testReportErrorVisibleOutsidePackage() {
CandidateMatcher<QueryMatch> matcher = newCandidateMatcher();
// This should compile from outside org.apache.lucene.monitor package
// (subpackage org.apache.lucene.monitor.outsidepackage cannot access package-private content
// from org.apache.lucene.monitor)
matcher.reportError("test", new RuntimeException("test exception"));
}
@Test
public void testFinishVisibleOutsidePackage() {
CandidateMatcher<QueryMatch> matcher = newCandidateMatcher();
// This should compile from outside org.apache.lucene.monitor package
// (subpackage org.apache.lucene.monitor.outsidepackage cannot access package-private content
// from org.apache.lucene.monitor)
matcher.finish(0, 0);
}
}