Skip to content

Apache Lucene open-source search software

SOLR-1584: fix SolrQuery.setIncludeScore()

Yonik Seeley0d639dad0383
diff --git a/CHANGES.txt b/CHANGES.txt
index b9b490b51a9..40d715af332 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -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
----------------------
diff --git a/src/solrj/org/apache/solr/client/solrj/SolrQuery.java b/src/solrj/org/apache/solr/client/solrj/SolrQuery.java
index ca9e5ed76da..689c3064775 100644
--- a/src/solrj/org/apache/solr/client/solrj/SolrQuery.java
+++ b/src/solrj/org/apache/solr/client/solrj/SolrQuery.java
@@ -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;
}
diff --git a/src/test/org/apache/solr/client/solrj/SolrQueryTest.java b/src/test/org/apache/solr/client/solrj/SolrQueryTest.java
index f5f764aa9c0..3cda2151f8f 100644
--- a/src/test/org/apache/solr/client/solrj/SolrQueryTest.java
+++ b/src/test/org/apache/solr/client/solrj/SolrQueryTest.java
@@ -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() {