Skip to content

Commit

Permalink
Make CandidateMatcher functions public (#13632)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bjacobowitz authored Aug 7, 2024
1 parent 9e831ee commit 926d8f4
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 12 deletions.
2 changes: 2 additions & 0 deletions lucene/CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
---------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public CandidateMatcher(IndexSearcher searcher) {
* @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;

/**
Expand Down Expand Up @@ -95,14 +95,14 @@ protected final void addMatch(T match, int doc) {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ private ParallelMatcher(
}

@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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private PartitionMatcher(
}

@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));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ static <T extends QueryMatch> MatcherFactory<T> timingMatcher(
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public void testMatcherMetadata() throws IOException {
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"));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 926d8f4

Please sign in to comment.