-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
5 changed files
with
167 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
156 changes: 156 additions & 0 deletions
156
lucene/sandbox/src/test/org/apache/lucene/sandbox/search/TestQueryProfilerWeight.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |