Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CandidateMatcher public matching functions #13632

Merged
merged 2 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}