Skip to content

Commit

Permalink
ProfileScorer should propagate setMinCompetitiveScore. (elastic#40958)
Browse files Browse the repository at this point in the history
Currently enabling profiling disables top-hits optimizations, which is
unfortunate: it would be nice to be able to notice the difference in method
counts and timings depending on whether total hit counts are requested.
  • Loading branch information
jpountz authored and Gurkan Kaymak committed May 27, 2019
1 parent 6b04474 commit c9afd54
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 8 deletions.
24 changes: 18 additions & 6 deletions docs/reference/search/profile.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ This will yield the following result:
"compute_max_score": 0,
"compute_max_score_count": 0,
"shallow_advance": 0,
"shallow_advance_count": 0
"shallow_advance_count": 0,
"set_min_competitive_score": 0,
"set_min_competitive_score_count": 0
},
"children": [
{
Expand All @@ -105,7 +107,9 @@ This will yield the following result:
"compute_max_score": 0,
"compute_max_score_count": 0,
"shallow_advance": 0,
"shallow_advance_count": 0
"shallow_advance_count": 0,
"set_min_competitive_score": 0,
"set_min_competitive_score_count": 0
}
},
{
Expand All @@ -128,7 +132,9 @@ This will yield the following result:
"compute_max_score": 0,
"compute_max_score_count": 0,
"shallow_advance": 0,
"shallow_advance_count": 0
"shallow_advance_count": 0,
"set_min_competitive_score": 0,
"set_min_competitive_score_count": 0
}
}
]
Expand Down Expand Up @@ -311,7 +317,9 @@ The `breakdown` component lists detailed timing statistics about low-level Lucen
"compute_max_score": 0,
"compute_max_score_count": 0,
"shallow_advance": 0,
"shallow_advance_count": 0
"shallow_advance_count": 0,
"set_min_competitive_score": 0,
"set_min_competitive_score_count": 0
}
--------------------------------------------------
// TESTRESPONSE[s/^/{\n"took": $body.took,\n"timed_out": $body.timed_out,\n"_shards": $body._shards,\n"hits": $body.hits,\n"profile": {\n"shards": [ {\n"id": "$body.$_path",\n"searches": [{\n"query": [{\n"type": "BooleanQuery",\n"description": "message:some message:number",\n"time_in_nanos": $body.$_path,/]
Expand Down Expand Up @@ -575,7 +583,9 @@ And the response:
"compute_max_score": 0,
"compute_max_score_count": 0,
"shallow_advance": 0,
"shallow_advance_count": 0
"shallow_advance_count": 0,
"set_min_competitive_score": 0,
"set_min_competitive_score_count": 0
}
},
{
Expand All @@ -598,7 +608,9 @@ And the response:
"compute_max_score": 0,
"compute_max_score_count": 0,
"shallow_advance": 0,
"shallow_advance_count": 0
"shallow_advance_count": 0,
"set_min_competitive_score": 0,
"set_min_competitive_score_count": 0
}
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ final class ProfileScorer extends Scorer {
private final Scorer scorer;
private ProfileWeight profileWeight;

private final Timer scoreTimer, nextDocTimer, advanceTimer, matchTimer, shallowAdvanceTimer, computeMaxScoreTimer;
private final Timer scoreTimer, nextDocTimer, advanceTimer, matchTimer, shallowAdvanceTimer, computeMaxScoreTimer,
setMinCompetitiveScoreTimer;

ProfileScorer(ProfileWeight w, Scorer scorer, QueryProfileBreakdown profile) throws IOException {
super(w);
Expand All @@ -49,6 +50,7 @@ final class ProfileScorer extends Scorer {
matchTimer = profile.getTimer(QueryTimingType.MATCH);
shallowAdvanceTimer = profile.getTimer(QueryTimingType.SHALLOW_ADVANCE);
computeMaxScoreTimer = profile.getTimer(QueryTimingType.COMPUTE_MAX_SCORE);
setMinCompetitiveScoreTimer = profile.getTimer(QueryTimingType.SET_MIN_COMPETITIVE_SCORE);
}

@Override
Expand Down Expand Up @@ -189,4 +191,14 @@ public float getMaxScore(int upTo) throws IOException {
computeMaxScoreTimer.stop();
}
}

@Override
public void setMinCompetitiveScore(float minScore) throws IOException {
setMinCompetitiveScoreTimer.start();
try {
scorer.setMinCompetitiveScore(minScore);
} finally {
setMinCompetitiveScoreTimer.stop();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public enum QueryTimingType {
MATCH,
SCORE,
SHALLOW_ADVANCE,
COMPUTE_MAX_SCORE;
COMPUTE_MAX_SCORE,
SET_MIN_COMPETITIVE_SCORE;

@Override
public String toString() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch 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.elasticsearch.search.profile.query;

import org.apache.lucene.index.MultiReader;
import org.apache.lucene.search.DocIdSetIterator;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreMode;
import org.apache.lucene.search.Scorer;
import org.apache.lucene.search.Weight;
import org.elasticsearch.test.ESTestCase;

import java.io.IOException;

public class ProfileScorerTests extends ESTestCase {

private static class FakeScorer extends Scorer {

public float maxScore, minCompetitiveScore;

protected FakeScorer(Weight weight) {
super(weight);
}

@Override
public DocIdSetIterator iterator() {
throw new UnsupportedOperationException();
}

@Override
public float getMaxScore(int upTo) throws IOException {
return maxScore;
}

@Override
public float score() throws IOException {
return 1f;
}

@Override
public int docID() {
throw new UnsupportedOperationException();
}

@Override
public void setMinCompetitiveScore(float minScore) {
this.minCompetitiveScore = minScore;
}
}

public void testPropagateMinCompetitiveScore() throws IOException {
Query query = new MatchAllDocsQuery();
Weight weight = query.createWeight(new IndexSearcher(new MultiReader()), ScoreMode.TOP_SCORES, 1f);
FakeScorer fakeScorer = new FakeScorer(weight);
QueryProfileBreakdown profile = new QueryProfileBreakdown();
ProfileWeight profileWeight = new ProfileWeight(query, weight, profile);
ProfileScorer profileScorer = new ProfileScorer(profileWeight, fakeScorer, profile);
profileScorer.setMinCompetitiveScore(0.42f);
assertEquals(0.42f, fakeScorer.minCompetitiveScore, 0f);
}

public void testPropagateMaxScore() throws IOException {
Query query = new MatchAllDocsQuery();
Weight weight = query.createWeight(new IndexSearcher(new MultiReader()), ScoreMode.TOP_SCORES, 1f);
FakeScorer fakeScorer = new FakeScorer(weight);
QueryProfileBreakdown profile = new QueryProfileBreakdown();
ProfileWeight profileWeight = new ProfileWeight(query, weight, profile);
ProfileScorer profileScorer = new ProfileScorer(profileWeight, fakeScorer, profile);
profileScorer.setMinCompetitiveScore(0.42f);
fakeScorer.maxScore = 42f;
assertEquals(42f, profileScorer.getMaxScore(DocIdSetIterator.NO_MORE_DOCS), 0f);
}
}

0 comments on commit c9afd54

Please sign in to comment.