Index: matcher/multimatch.cc
===================================================================
--- matcher/multimatch.cc	(revision 11739)
+++ matcher/multimatch.cc	(working copy)
@@ -478,6 +478,8 @@
     bool is_heap = false;
 
     while (true) {
+	bool pushback;
+
 	if (rare(recalculate_w_max)) {
 	    if (min_weight > 0.0) {
 		if (rare(getorrecalc_maxweight(pl) < min_weight)) {
@@ -544,11 +546,16 @@
 		    // processing needed.
 		    LOGLINE(MATCH, "Making note of match item which sorts lower than min_item");
 		    ++docs_matched;
+		    if (!calculated_weight) wt = pl->get_weight();
+		    if (wt > greatest_wt) goto new_greatest_weight;
 		    continue;
 		}
 		if (docs_matched >= check_at_least) {
 		    // We've seen enough items - we can drop this one.
 		    LOGLINE(MATCH, "Dropping candidate which sorts lower than min_item");
+		    // FIXME: hmm, match decider might have rejected this...
+		    if (!calculated_weight) wt = pl->get_weight();
+		    if (wt > greatest_wt) goto new_greatest_weight;
 		    continue;
 		}
 		// We can't drop the item, because we need to show it
@@ -590,7 +597,7 @@
 	    new_item.wt = wt;
 	}
 
-	bool pushback = true;
+	pushback = true;
 	documents_considered++;
 
 	// Perform collapsing on key if requested.
@@ -623,6 +630,11 @@
 			if (new_item.wt > oldkey->second.second) {
 			    oldkey->second.second = new_item.wt;
 			}
+			// If we're sorting by relevance primarily, then we
+			// throw away the lower weighted document anyway.
+			if (sort_by != REL && sort_by != REL_VAL) {
+			    if (wt > greatest_wt) goto new_greatest_weight;
+			}
 			continue;
 		    }
 		    // Make a note of the updated collapse count in the
@@ -734,6 +746,7 @@
 
 	// Keep a track of the greatest weight we've seen.
 	if (wt > greatest_wt) {
+new_greatest_weight:
 	    greatest_wt = wt;
 	    if (percent_cutoff) {
 		Xapian::weight w = wt * percent_cutoff_factor;
