SOLR-1584: fix SolrQuery.setIncludeScore()

git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@883315 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yonik Seeley 2009-11-23 12:14:38 +00:00
parent e5843b4e71
commit 0d639dad03
3 changed files with 22 additions and 4 deletions

View File

@ -44,14 +44,14 @@ New Features
* SOLR-1574: Add many new functions from java Math (e.g. sin, cos) (yonik)
* SOLR-1569: Allow functions to take in literal strings by modifying the FunctionQParser and adding LiteralValueSource (gsingers)
Optimizations
----------------------
Bug Fixes
----------------------
* SOLR-1569: Allow functions to take in literal strings by modifying the FunctionQParser and adding LiteralValueSource (gsingers)
* SOLR-1432: Make the new ValueSource.getValues(context,reader) delegate
to the original ValueSource.getValues(reader) so custom sources
will work. (yonik)
@ -70,6 +70,10 @@ Bug Fixes
string, which is treated the same as if no dataDir had been specified,
hence the "data" directory under the solr home will be used. (yonik)
* SOLR-1584: SolrJ - SolrQuery.setIncludeScore() incorrectly added
fl=score to the parameter list instead of appending score to the
existing field list. (yonik)
Other Changes
----------------------

View File

@ -23,6 +23,8 @@ import org.apache.solr.common.params.HighlightParams;
import org.apache.solr.common.params.ModifiableSolrParams;
import org.apache.solr.common.params.StatsParams;
import java.util.regex.Pattern;
/**
* This is an augmented SolrParams with get/set/add fields for common fields used
@ -445,11 +447,16 @@ public class SolrQuery extends ModifiableSolrParams
return fields;
}
private static Pattern scorePattern = Pattern.compile("(^|[, ])score");
public SolrQuery setIncludeScore(boolean includeScore) {
String fields = get(CommonParams.FL,"*");
if (includeScore) {
this.add(CommonParams.FL, "score");
if (!scorePattern.matcher(fields).find()) {
this.set(CommonParams.FL, fields+",score");
}
} else {
this.remove(CommonParams.FL, "score");
this.set(CommonParams.FL, scorePattern.matcher(fields).replaceAll(""));
}
return this;
}

View File

@ -130,6 +130,13 @@ public class SolrQueryTest extends TestCase {
assertEquals( Boolean.TRUE, q.setMissing(Boolean.TRUE.toString()).getBool( FacetParams.FACET_MISSING ) );
assertEquals( Boolean.FALSE, q.setFacetMissing( Boolean.FALSE ).getBool( FacetParams.FACET_MISSING ) );
assertEquals( "true", q.setParam( "xxx", true ).getParams( "xxx" )[0] );
assertEquals( "x,y", q.setFields("x","y").getFields() );
assertEquals( "x,y,score", q.setIncludeScore(true).getFields() );
assertEquals( "x,y,score", q.setIncludeScore(true).getFields() ); // set twice on purpose
assertEquals( "x,y", q.setIncludeScore(false).getFields() );
assertEquals( "x,y", q.setIncludeScore(false).getFields() ); // remove twice on purpose
}
public void testOrder() {