Skip to content

Commit 615f456

Browse files
committed
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.
1 parent 7e15a39 commit 615f456

File tree

5 files changed

+167
-19
lines changed

5 files changed

+167
-19
lines changed

lucene/sandbox/src/java/org/apache/lucene/sandbox/search/QueryProfilerIndexSearcher.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public Weight createWeight(Query query, ScoreMode scoreMode, float boost) throws
6363
timer.stop();
6464
profiler.pollLast();
6565
}
66-
return new QueryProfilerWeight(query, weight, profile);
66+
return new QueryProfilerWeight(weight, profile);
6767
}
6868

6969
/**

lucene/sandbox/src/java/org/apache/lucene/sandbox/search/QueryProfilerScorer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
class QueryProfilerScorer extends Scorer {
3232

3333
private final Scorer scorer;
34-
private QueryProfilerWeight profileWeight;
34+
private final QueryProfilerWeight profileWeight;
3535

3636
private final QueryProfilerTimer scoreTimer,
3737
nextDocTimer,

lucene/sandbox/src/java/org/apache/lucene/sandbox/search/QueryProfilerWeight.java

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020
import java.io.IOException;
2121
import org.apache.lucene.index.LeafReaderContext;
2222
import org.apache.lucene.search.BulkScorer;
23-
import org.apache.lucene.search.Explanation;
24-
import org.apache.lucene.search.Query;
23+
import org.apache.lucene.search.FilterWeight;
2524
import org.apache.lucene.search.Scorer;
2625
import org.apache.lucene.search.ScorerSupplier;
2726
import org.apache.lucene.search.Weight;
@@ -30,14 +29,12 @@
3029
* Weight wrapper that will compute how much time it takes to build the {@link Scorer} and then
3130
* return a {@link Scorer} that is wrapped in order to compute timings as well.
3231
*/
33-
class QueryProfilerWeight extends Weight {
32+
class QueryProfilerWeight extends FilterWeight {
3433

35-
private final Weight subQueryWeight;
3634
private final QueryProfilerBreakdown profile;
3735

38-
public QueryProfilerWeight(Query query, Weight subQueryWeight, QueryProfilerBreakdown profile) {
39-
super(query);
40-
this.subQueryWeight = subQueryWeight;
36+
public QueryProfilerWeight(Weight subQueryWeight, QueryProfilerBreakdown profile) {
37+
super(subQueryWeight);
4138
this.profile = profile;
4239
}
4340

@@ -46,7 +43,7 @@ public int count(LeafReaderContext context) throws IOException {
4643
QueryProfilerTimer timer = profile.getTimer(QueryProfilerTimingType.COUNT);
4744
timer.start();
4845
try {
49-
return subQueryWeight.count(context);
46+
return in.count(context);
5047
} finally {
5148
timer.stop();
5249
}
@@ -67,7 +64,7 @@ public ScorerSupplier scorerSupplier(LeafReaderContext context) throws IOExcepti
6764
timer.start();
6865
final ScorerSupplier subQueryScorerSupplier;
6966
try {
70-
subQueryScorerSupplier = subQueryWeight.scorerSupplier(context);
67+
subQueryScorerSupplier = in.scorerSupplier(context);
7168
} finally {
7269
timer.stop();
7370
}
@@ -103,7 +100,7 @@ public long cost() {
103100
@Override
104101
public BulkScorer bulkScorer(LeafReaderContext context) throws IOException {
105102
// We use the default bulk scorer instead of the specialized one. The reason
106-
// is that Lucene's BulkScorers do everything at once: finding matches,
103+
// is that BulkScorers do everything at once: finding matches,
107104
// scoring them and calling the collector, so they make it impossible to
108105
// see where time is spent, which is the purpose of query profiling.
109106
// The default bulk scorer will pull a scorer and iterate over matches,
@@ -112,11 +109,6 @@ public BulkScorer bulkScorer(LeafReaderContext context) throws IOException {
112109
return super.bulkScorer(context);
113110
}
114111

115-
@Override
116-
public Explanation explain(LeafReaderContext context, int doc) throws IOException {
117-
return subQueryWeight.explain(context, doc);
118-
}
119-
120112
@Override
121113
public boolean isCacheable(LeafReaderContext ctx) {
122114
return false;

lucene/sandbox/src/test/org/apache/lucene/sandbox/search/TestQueryProfilerScorer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public void testPropagateMinCompetitiveScore() throws IOException {
7070
query.createWeight(new IndexSearcher(new MultiReader()), ScoreMode.TOP_SCORES, 1f);
7171
FakeScorer fakeScorer = new FakeScorer(weight);
7272
QueryProfilerBreakdown profile = new QueryProfilerBreakdown();
73-
QueryProfilerWeight queryProfilerWeight = new QueryProfilerWeight(query, weight, profile);
73+
QueryProfilerWeight queryProfilerWeight = new QueryProfilerWeight(weight, profile);
7474
QueryProfilerScorer queryProfilerScorer =
7575
new QueryProfilerScorer(queryProfilerWeight, fakeScorer, profile);
7676
queryProfilerScorer.setMinCompetitiveScore(0.42f);
@@ -83,7 +83,7 @@ public void testPropagateMaxScore() throws IOException {
8383
query.createWeight(new IndexSearcher(new MultiReader()), ScoreMode.TOP_SCORES, 1f);
8484
FakeScorer fakeScorer = new FakeScorer(weight);
8585
QueryProfilerBreakdown profile = new QueryProfilerBreakdown();
86-
QueryProfilerWeight queryProfilerWeight = new QueryProfilerWeight(query, weight, profile);
86+
QueryProfilerWeight queryProfilerWeight = new QueryProfilerWeight(weight, profile);
8787
QueryProfilerScorer queryProfilerScorer =
8888
new QueryProfilerScorer(queryProfilerWeight, fakeScorer, profile);
8989
queryProfilerScorer.setMinCompetitiveScore(0.42f);
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.lucene.sandbox.search;
19+
20+
import java.io.IOException;
21+
import java.util.Collection;
22+
import java.util.Collections;
23+
import java.util.Iterator;
24+
import org.apache.lucene.index.LeafReaderContext;
25+
import org.apache.lucene.search.DocIdSetIterator;
26+
import org.apache.lucene.search.Explanation;
27+
import org.apache.lucene.search.MatchAllDocsQuery;
28+
import org.apache.lucene.search.Matches;
29+
import org.apache.lucene.search.MatchesIterator;
30+
import org.apache.lucene.search.Query;
31+
import org.apache.lucene.search.Scorer;
32+
import org.apache.lucene.search.Weight;
33+
import org.apache.lucene.tests.util.LuceneTestCase;
34+
35+
public class TestQueryProfilerWeight extends LuceneTestCase {
36+
37+
private static final class FakeWeight extends Weight {
38+
FakeWeight(Query query) {
39+
super(query);
40+
}
41+
42+
@Override
43+
public Explanation explain(LeafReaderContext context, int doc) {
44+
return Explanation.match(1, "fake_description");
45+
}
46+
47+
@Override
48+
public Scorer scorer(LeafReaderContext context) {
49+
return new Scorer(this) {
50+
@Override
51+
public DocIdSetIterator iterator() {
52+
return null;
53+
}
54+
55+
@Override
56+
public float getMaxScore(int upTo) {
57+
return 42f;
58+
}
59+
60+
@Override
61+
public float score() {
62+
return 0;
63+
}
64+
65+
@Override
66+
public int docID() {
67+
return 0;
68+
}
69+
};
70+
}
71+
72+
@Override
73+
public boolean isCacheable(LeafReaderContext ctx) {
74+
return false;
75+
}
76+
77+
@Override
78+
public Matches matches(LeafReaderContext context, int doc) {
79+
return new Matches() {
80+
@Override
81+
public MatchesIterator getMatches(String field) {
82+
return new MatchesIterator() {
83+
@Override
84+
public boolean next() {
85+
return false;
86+
}
87+
88+
@Override
89+
public int startPosition() {
90+
return 42;
91+
}
92+
93+
@Override
94+
public int endPosition() {
95+
return 43;
96+
}
97+
98+
@Override
99+
public int startOffset() {
100+
return 44;
101+
}
102+
103+
@Override
104+
public int endOffset() {
105+
return 45;
106+
}
107+
108+
@Override
109+
public MatchesIterator getSubMatches() {
110+
return null;
111+
}
112+
113+
@Override
114+
public Query getQuery() {
115+
return parentQuery;
116+
}
117+
};
118+
}
119+
120+
@Override
121+
public Collection<Matches> getSubMatches() {
122+
return Collections.emptyList();
123+
}
124+
125+
@Override
126+
public Iterator<String> iterator() {
127+
return null;
128+
}
129+
};
130+
}
131+
}
132+
133+
public void testPropagateMatches() throws IOException {
134+
Query query = new MatchAllDocsQuery();
135+
Weight fakeWeight = new FakeWeight(query);
136+
QueryProfilerBreakdown profile = new QueryProfilerBreakdown();
137+
QueryProfilerWeight profileWeight = new QueryProfilerWeight(fakeWeight, profile);
138+
assertEquals(42, profileWeight.matches(null, 1).getMatches("some_field").startPosition());
139+
}
140+
141+
public void testPropagateExplain() throws IOException {
142+
Query query = new MatchAllDocsQuery();
143+
Weight fakeWeight = new FakeWeight(query);
144+
QueryProfilerBreakdown profile = new QueryProfilerBreakdown();
145+
QueryProfilerWeight profileWeight = new QueryProfilerWeight(fakeWeight, profile);
146+
assertEquals("fake_description", profileWeight.explain(null, 1).getDescription());
147+
}
148+
149+
public void testPropagateScorer() throws IOException {
150+
Query query = new MatchAllDocsQuery();
151+
Weight fakeWeight = new FakeWeight(query);
152+
QueryProfilerBreakdown profile = new QueryProfilerBreakdown();
153+
QueryProfilerWeight profileWeight = new QueryProfilerWeight(fakeWeight, profile);
154+
assertEquals(42f, profileWeight.scorer(null).getMaxScore(DocIdSetIterator.NO_MORE_DOCS), 0f);
155+
}
156+
}

0 commit comments

Comments
 (0)