mirror of https://github.com/apache/lucene.git
QueryProfilerWeight to extend FilterWeight (#12242)
QueryProfilerWeight should override matches and delegate to the subQueryWeight. Another way to fix this issue is to make it extend ProfileWeight and override only methods that need to have a different behaviour than delegating to the sub weight.
This commit is contained in:
parent
4deb0003c4
commit
b0befef912
|
@ -63,7 +63,7 @@ public class QueryProfilerIndexSearcher extends IndexSearcher {
|
||||||
timer.stop();
|
timer.stop();
|
||||||
profiler.pollLast();
|
profiler.pollLast();
|
||||||
}
|
}
|
||||||
return new QueryProfilerWeight(query, weight, profile);
|
return new QueryProfilerWeight(weight, profile);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -31,7 +31,7 @@ import org.apache.lucene.search.Weight;
|
||||||
class QueryProfilerScorer extends Scorer {
|
class QueryProfilerScorer extends Scorer {
|
||||||
|
|
||||||
private final Scorer scorer;
|
private final Scorer scorer;
|
||||||
private QueryProfilerWeight profileWeight;
|
private final QueryProfilerWeight profileWeight;
|
||||||
|
|
||||||
private final QueryProfilerTimer scoreTimer,
|
private final QueryProfilerTimer scoreTimer,
|
||||||
nextDocTimer,
|
nextDocTimer,
|
||||||
|
|
|
@ -20,8 +20,7 @@ package org.apache.lucene.sandbox.search;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import org.apache.lucene.index.LeafReaderContext;
|
import org.apache.lucene.index.LeafReaderContext;
|
||||||
import org.apache.lucene.search.BulkScorer;
|
import org.apache.lucene.search.BulkScorer;
|
||||||
import org.apache.lucene.search.Explanation;
|
import org.apache.lucene.search.FilterWeight;
|
||||||
import org.apache.lucene.search.Query;
|
|
||||||
import org.apache.lucene.search.Scorer;
|
import org.apache.lucene.search.Scorer;
|
||||||
import org.apache.lucene.search.ScorerSupplier;
|
import org.apache.lucene.search.ScorerSupplier;
|
||||||
import org.apache.lucene.search.Weight;
|
import org.apache.lucene.search.Weight;
|
||||||
|
@ -30,14 +29,12 @@ import org.apache.lucene.search.Weight;
|
||||||
* Weight wrapper that will compute how much time it takes to build the {@link Scorer} and then
|
* Weight wrapper that will compute how much time it takes to build the {@link Scorer} and then
|
||||||
* return a {@link Scorer} that is wrapped in order to compute timings as well.
|
* return a {@link Scorer} that is wrapped in order to compute timings as well.
|
||||||
*/
|
*/
|
||||||
class QueryProfilerWeight extends Weight {
|
class QueryProfilerWeight extends FilterWeight {
|
||||||
|
|
||||||
private final Weight subQueryWeight;
|
|
||||||
private final QueryProfilerBreakdown profile;
|
private final QueryProfilerBreakdown profile;
|
||||||
|
|
||||||
public QueryProfilerWeight(Query query, Weight subQueryWeight, QueryProfilerBreakdown profile) {
|
public QueryProfilerWeight(Weight subQueryWeight, QueryProfilerBreakdown profile) {
|
||||||
super(query);
|
super(subQueryWeight);
|
||||||
this.subQueryWeight = subQueryWeight;
|
|
||||||
this.profile = profile;
|
this.profile = profile;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,7 +43,7 @@ class QueryProfilerWeight extends Weight {
|
||||||
QueryProfilerTimer timer = profile.getTimer(QueryProfilerTimingType.COUNT);
|
QueryProfilerTimer timer = profile.getTimer(QueryProfilerTimingType.COUNT);
|
||||||
timer.start();
|
timer.start();
|
||||||
try {
|
try {
|
||||||
return subQueryWeight.count(context);
|
return in.count(context);
|
||||||
} finally {
|
} finally {
|
||||||
timer.stop();
|
timer.stop();
|
||||||
}
|
}
|
||||||
|
@ -67,7 +64,7 @@ class QueryProfilerWeight extends Weight {
|
||||||
timer.start();
|
timer.start();
|
||||||
final ScorerSupplier subQueryScorerSupplier;
|
final ScorerSupplier subQueryScorerSupplier;
|
||||||
try {
|
try {
|
||||||
subQueryScorerSupplier = subQueryWeight.scorerSupplier(context);
|
subQueryScorerSupplier = in.scorerSupplier(context);
|
||||||
} finally {
|
} finally {
|
||||||
timer.stop();
|
timer.stop();
|
||||||
}
|
}
|
||||||
|
@ -103,7 +100,7 @@ class QueryProfilerWeight extends Weight {
|
||||||
@Override
|
@Override
|
||||||
public BulkScorer bulkScorer(LeafReaderContext context) throws IOException {
|
public BulkScorer bulkScorer(LeafReaderContext context) throws IOException {
|
||||||
// We use the default bulk scorer instead of the specialized one. The reason
|
// We use the default bulk scorer instead of the specialized one. The reason
|
||||||
// is that Lucene's BulkScorers do everything at once: finding matches,
|
// is that BulkScorers do everything at once: finding matches,
|
||||||
// scoring them and calling the collector, so they make it impossible to
|
// scoring them and calling the collector, so they make it impossible to
|
||||||
// see where time is spent, which is the purpose of query profiling.
|
// see where time is spent, which is the purpose of query profiling.
|
||||||
// The default bulk scorer will pull a scorer and iterate over matches,
|
// The default bulk scorer will pull a scorer and iterate over matches,
|
||||||
|
@ -112,11 +109,6 @@ class QueryProfilerWeight extends Weight {
|
||||||
return super.bulkScorer(context);
|
return super.bulkScorer(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Explanation explain(LeafReaderContext context, int doc) throws IOException {
|
|
||||||
return subQueryWeight.explain(context, doc);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isCacheable(LeafReaderContext ctx) {
|
public boolean isCacheable(LeafReaderContext ctx) {
|
||||||
return false;
|
return false;
|
||||||
|
|
|
@ -70,7 +70,7 @@ public class TestQueryProfilerScorer extends LuceneTestCase {
|
||||||
query.createWeight(new IndexSearcher(new MultiReader()), ScoreMode.TOP_SCORES, 1f);
|
query.createWeight(new IndexSearcher(new MultiReader()), ScoreMode.TOP_SCORES, 1f);
|
||||||
FakeScorer fakeScorer = new FakeScorer(weight);
|
FakeScorer fakeScorer = new FakeScorer(weight);
|
||||||
QueryProfilerBreakdown profile = new QueryProfilerBreakdown();
|
QueryProfilerBreakdown profile = new QueryProfilerBreakdown();
|
||||||
QueryProfilerWeight queryProfilerWeight = new QueryProfilerWeight(query, weight, profile);
|
QueryProfilerWeight queryProfilerWeight = new QueryProfilerWeight(weight, profile);
|
||||||
QueryProfilerScorer queryProfilerScorer =
|
QueryProfilerScorer queryProfilerScorer =
|
||||||
new QueryProfilerScorer(queryProfilerWeight, fakeScorer, profile);
|
new QueryProfilerScorer(queryProfilerWeight, fakeScorer, profile);
|
||||||
queryProfilerScorer.setMinCompetitiveScore(0.42f);
|
queryProfilerScorer.setMinCompetitiveScore(0.42f);
|
||||||
|
@ -83,7 +83,7 @@ public class TestQueryProfilerScorer extends LuceneTestCase {
|
||||||
query.createWeight(new IndexSearcher(new MultiReader()), ScoreMode.TOP_SCORES, 1f);
|
query.createWeight(new IndexSearcher(new MultiReader()), ScoreMode.TOP_SCORES, 1f);
|
||||||
FakeScorer fakeScorer = new FakeScorer(weight);
|
FakeScorer fakeScorer = new FakeScorer(weight);
|
||||||
QueryProfilerBreakdown profile = new QueryProfilerBreakdown();
|
QueryProfilerBreakdown profile = new QueryProfilerBreakdown();
|
||||||
QueryProfilerWeight queryProfilerWeight = new QueryProfilerWeight(query, weight, profile);
|
QueryProfilerWeight queryProfilerWeight = new QueryProfilerWeight(weight, profile);
|
||||||
QueryProfilerScorer queryProfilerScorer =
|
QueryProfilerScorer queryProfilerScorer =
|
||||||
new QueryProfilerScorer(queryProfilerWeight, fakeScorer, profile);
|
new QueryProfilerScorer(queryProfilerWeight, fakeScorer, profile);
|
||||||
queryProfilerScorer.setMinCompetitiveScore(0.42f);
|
queryProfilerScorer.setMinCompetitiveScore(0.42f);
|
||||||
|
|
|
@ -0,0 +1,156 @@
|
||||||
|
/*
|
||||||
|
* 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.sandbox.search;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import org.apache.lucene.index.LeafReaderContext;
|
||||||
|
import org.apache.lucene.search.DocIdSetIterator;
|
||||||
|
import org.apache.lucene.search.Explanation;
|
||||||
|
import org.apache.lucene.search.MatchAllDocsQuery;
|
||||||
|
import org.apache.lucene.search.Matches;
|
||||||
|
import org.apache.lucene.search.MatchesIterator;
|
||||||
|
import org.apache.lucene.search.Query;
|
||||||
|
import org.apache.lucene.search.Scorer;
|
||||||
|
import org.apache.lucene.search.Weight;
|
||||||
|
import org.apache.lucene.tests.util.LuceneTestCase;
|
||||||
|
|
||||||
|
public class TestQueryProfilerWeight extends LuceneTestCase {
|
||||||
|
|
||||||
|
private static final class FakeWeight extends Weight {
|
||||||
|
FakeWeight(Query query) {
|
||||||
|
super(query);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Explanation explain(LeafReaderContext context, int doc) {
|
||||||
|
return Explanation.match(1, "fake_description");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Scorer scorer(LeafReaderContext context) {
|
||||||
|
return new Scorer(this) {
|
||||||
|
@Override
|
||||||
|
public DocIdSetIterator iterator() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public float getMaxScore(int upTo) {
|
||||||
|
return 42f;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public float score() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int docID() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isCacheable(LeafReaderContext ctx) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Matches matches(LeafReaderContext context, int doc) {
|
||||||
|
return new Matches() {
|
||||||
|
@Override
|
||||||
|
public MatchesIterator getMatches(String field) {
|
||||||
|
return new MatchesIterator() {
|
||||||
|
@Override
|
||||||
|
public boolean next() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int startPosition() {
|
||||||
|
return 42;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int endPosition() {
|
||||||
|
return 43;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int startOffset() {
|
||||||
|
return 44;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int endOffset() {
|
||||||
|
return 45;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public MatchesIterator getSubMatches() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Query getQuery() {
|
||||||
|
return parentQuery;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Collection<Matches> getSubMatches() {
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Iterator<String> iterator() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testPropagateMatches() throws IOException {
|
||||||
|
Query query = new MatchAllDocsQuery();
|
||||||
|
Weight fakeWeight = new FakeWeight(query);
|
||||||
|
QueryProfilerBreakdown profile = new QueryProfilerBreakdown();
|
||||||
|
QueryProfilerWeight profileWeight = new QueryProfilerWeight(fakeWeight, profile);
|
||||||
|
assertEquals(42, profileWeight.matches(null, 1).getMatches("some_field").startPosition());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testPropagateExplain() throws IOException {
|
||||||
|
Query query = new MatchAllDocsQuery();
|
||||||
|
Weight fakeWeight = new FakeWeight(query);
|
||||||
|
QueryProfilerBreakdown profile = new QueryProfilerBreakdown();
|
||||||
|
QueryProfilerWeight profileWeight = new QueryProfilerWeight(fakeWeight, profile);
|
||||||
|
assertEquals("fake_description", profileWeight.explain(null, 1).getDescription());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testPropagateScorer() throws IOException {
|
||||||
|
Query query = new MatchAllDocsQuery();
|
||||||
|
Weight fakeWeight = new FakeWeight(query);
|
||||||
|
QueryProfilerBreakdown profile = new QueryProfilerBreakdown();
|
||||||
|
QueryProfilerWeight profileWeight = new QueryProfilerWeight(fakeWeight, profile);
|
||||||
|
assertEquals(42f, profileWeight.scorer(null).getMaxScore(DocIdSetIterator.NO_MORE_DOCS), 0f);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue