Skip to content

Commit

Permalink
fixed _score syscolumn NaN response when using ORDER BY clause
Browse files Browse the repository at this point in the history
  • Loading branch information
mikethebeer committed Oct 1, 2015
1 parent c68f6dc commit 740fef1
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ Changes for Crate
Unreleased
==========

- Fixed a bug that causes a NaN response on _score system column in select
statement that uses an ORDER BY clause

- Fix: in some rare cases tables were not updated on every node after table
schema changes

Expand Down
11 changes: 10 additions & 1 deletion sql/src/main/java/io/crate/metadata/ScoreReferenceDetector.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,19 @@ public boolean apply(@Nullable Reference input) {
}
};

public Boolean detect(Symbol symbol) {
public static boolean detect(Symbol symbol) {
return VISITOR.process(symbol, SCORE_PREDICATE);
}

public static boolean detect(Iterable<? extends Symbol> symbols) {
for (Symbol symbol : symbols) {
if(detect(symbol)) {
return true;
}
}
return false;
}

static class Visitor extends SymbolVisitor<Predicate<Reference>, Boolean> {
@Override
public Boolean visitFunction(Function symbol, Predicate<Reference> predicate) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import io.crate.jobs.KeepAliveListener;
import io.crate.lucene.QueryBuilderHelper;
import io.crate.metadata.Functions;
import io.crate.metadata.ScoreReferenceDetector;
import io.crate.operation.*;
import io.crate.operation.reference.doc.lucene.*;
import io.crate.planner.node.dql.CollectPhase;
Expand Down Expand Up @@ -108,6 +109,7 @@ public void required(boolean required) {
private AtomicReader currentReader;
private int rowCount = 0;
private int pageSize;
private boolean doDocScores;

public LuceneDocCollector(CrateSearchContext searchContext,
KeepAliveListener keepAliveListener,
Expand All @@ -122,6 +124,7 @@ public LuceneDocCollector(CrateSearchContext searchContext,
this.ramAccountingContext = ramAccountingContext;
this.limit = collectNode.limit();
this.orderBy = collectNode.orderBy();
this.doDocScores = ScoreReferenceDetector.detect(collectNode.toCollect());
this.downstream = downStreamProjector.registerUpstream(this);
this.inputRow = new InputRow(inputs);
this.collectorExpressions = collectorExpressions;
Expand Down Expand Up @@ -241,7 +244,7 @@ public void kill() {
private void searchWithOrderBy(Query query) throws IOException {
Integer batchSize = limit == null ? pageSize : Math.min(pageSize, limit);
Sort sort = LuceneSortGenerator.generateLuceneSort(collectorContext, orderBy, inputSymbolVisitor);
TopFieldDocs topFieldDocs = searchContext.searcher().search(query, batchSize, sort);
TopFieldDocs topFieldDocs = searchContext.searcher().search(query, null, batchSize, sort, doDocScores, false);
int collected = topFieldDocs.scoreDocs.length;

Collection<ScoreCollectorExpression> scoreExpressions = getScoreExpressions();
Expand All @@ -257,9 +260,9 @@ private void searchWithOrderBy(Query query) throws IOException {
BooleanQuery searchAfterQuery = new BooleanQuery();
searchAfterQuery.add(query, BooleanClause.Occur.MUST);
searchAfterQuery.add(alreadyCollectedQuery, BooleanClause.Occur.MUST_NOT);
topFieldDocs = (TopFieldDocs)searchContext.searcher().searchAfter(lastCollected, searchAfterQuery, batchSize, sort);
topFieldDocs = (TopFieldDocs)searchContext.searcher().searchAfter(lastCollected, searchAfterQuery, null, batchSize, sort, doDocScores, false);
} else {
topFieldDocs = (TopFieldDocs)searchContext.searcher().searchAfter(lastCollected, query, batchSize, sort);
topFieldDocs = (TopFieldDocs)searchContext.searcher().searchAfter(lastCollected, query, null, batchSize, sort, doDocScores, false);
}
collected += topFieldDocs.scoreDocs.length;
lastCollected = collectTopFields(topFieldDocs, scoreExpressions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,7 @@ public void testSelectWhereScore() throws Exception {
execute("select quote, \"_score\" from quotes where match(quote_ft, 'time') " +
"and \"_score\" >= 0.98 order by quote ");
assertEquals(1L, response.rowCount());
assertEquals(false, Float.isNaN((Float) response.rows()[0][1]));
assertThat((Float) response.rows()[0][1], greaterThanOrEqualTo(0.98f));
}

Expand Down

0 comments on commit 740fef1

Please sign in to comment.