Skip to content
This repository was archived by the owner on Sep 19, 2023. It is now read-only.

Commit ace4d0e

Browse files
author
Radek Mensik
committed
DATASOLR-485 Add possibility to have constant score operator
1 parent 82ab8b1 commit ace4d0e

File tree

6 files changed

+51
-0
lines changed

6 files changed

+51
-0
lines changed

src/main/java/org/springframework/data/solr/core/QueryParserBase.java

+5
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ public abstract class QueryParserBase<QUERYTPYE extends SolrDataQuery> implement
7676
protected static final String DELIMINATOR = ":";
7777
protected static final String NOT = "-";
7878
protected static final String BOOST = "^";
79+
protected static final String CONSTANT_SCORE = "^=";
7980

8081
protected final GenericConversionService conversionService = new GenericConversionService();
8182
private final List<PredicateProcessor> critieraEntryProcessors = new ArrayList<>();
@@ -228,6 +229,10 @@ protected String createQueryFragmentForCriteria(Criteria part) {
228229
queryFragment.append(BOOST).append(criteria.getBoost());
229230
}
230231

232+
if (!Float.isNaN(criteria.getScore())) {
233+
queryFragment.append(CONSTANT_SCORE).append(criteria.getScore());
234+
}
235+
231236
return queryFragment.toString();
232237
}
233238

src/main/java/org/springframework/data/solr/core/query/Criteria.java

+14
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
* @author Christoph Strobl
3838
* @author Philipp Jardas
3939
* @author Francisco Spaeth
40+
* @author Radek Mensik
4041
*/
4142
public class Criteria extends Node {
4243

@@ -45,6 +46,7 @@ public class Criteria extends Node {
4546

4647
private @Nullable Field field;
4748
private float boost = Float.NaN;
49+
private float score = Float.NaN;
4850

4951
private Set<Predicate> predicates = new LinkedHashSet<>();
5052

@@ -391,6 +393,14 @@ public Criteria boost(float boost) {
391393
return this;
392394
}
393395

396+
public Criteria constantScore(float score) {
397+
if (score < 0) {
398+
throw new InvalidDataAccessApiUsageException("Score must not be negative.");
399+
}
400+
this.score = score;
401+
return this;
402+
}
403+
394404
/**
395405
* Crates new {@link Predicate} for {@code RANGE [lowerBound TO upperBound]}
396406
*
@@ -602,6 +612,10 @@ public float getBoost() {
602612
return this.boost;
603613
}
604614

615+
public float getScore() {
616+
return score;
617+
}
618+
605619
/**
606620
* @return unmodifiable set of all {@link Predicate}
607621
*/

src/main/java/org/springframework/data/solr/core/query/Crotch.java

+7
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
/**
2929
* @author Christoph Strobl
30+
* @author Radek Mensik
3031
* @since 1.2
3132
*/
3233
public class Crotch extends Criteria {
@@ -56,6 +57,12 @@ public Crotch boost(float boost) {
5657
return this;
5758
}
5859

60+
@Override
61+
public Crotch constantScore(float score) {
62+
mostRecentSibling.constantScore(score);
63+
return this;
64+
}
65+
5966
@Override
6067
public Crotch not() {
6168

src/main/java/org/springframework/data/solr/core/query/Node.java

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
/**
2727
* @author Christoph Strobl
28+
* @author Radek Mensik
2829
* @since 1.2
2930
*/
3031
public abstract class Node {
@@ -175,6 +176,8 @@ protected void setNegating(boolean negating) {
175176

176177
public abstract Node boost(float value);
177178

179+
public abstract Node constantScore(float score);
180+
178181
public abstract Node between(Object lowerBound, Object upperBound);
179182

180183
public abstract Node between(Object lowerBound, Object upperBound, boolean includeLowerBound,

src/test/java/org/springframework/data/solr/core/DefaultQueryParserTests.java

+14
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
* @author Francisco Spaeth
6666
* @author Petar Tahchiev
6767
* @author Michael Rocke
68+
* @author Radek Mensik
6869
*/
6970
public class DefaultQueryParserTests {
7071

@@ -259,12 +260,25 @@ public void testBoostMultipleValues() {
259260
assertEquals("field_1:(value_1 value_2)^2.0", queryParser.createQueryStringFromCriteria(criteria));
260261
}
261262

263+
@Test
264+
public void testScoreMultipleValues() {
265+
266+
Criteria criteria = new Criteria("field_1").is("value_1").is("value_2").constantScore(3f);
267+
assertEquals("field_1:(value_1 value_2)^=3.0", queryParser.createQueryStringFromCriteria(criteria));
268+
}
269+
262270
@Test
263271
public void testBoostMultipleCriteriasValues() {
264272
Criteria criteria = new Criteria("field_1").is("value_1").is("value_2").boost(2f).and("field_3").is("value_3");
265273
assertEquals("field_1:(value_1 value_2)^2.0 AND field_3:value_3", queryParser.createQueryStringFromNode(criteria));
266274
}
267275

276+
@Test
277+
public void testScoreMultipleCriteriasValues() {
278+
Criteria criteria = new Criteria("field_1").is("value_1").is("value_2").constantScore(3f).and("field_3").is("value_3");
279+
assertEquals("field_1:(value_1 value_2)^=3.0 AND field_3:value_3", queryParser.createQueryStringFromNode(criteria));
280+
}
281+
268282
@Test
269283
public void testBetween() {
270284

src/test/java/org/springframework/data/solr/core/query/CriteriaTests.java

+8
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
* @author Christoph Strobl
3939
* @author John Dorman
4040
* @author Philipp Jardas
41+
* @author Radek Mensik
4142
*/
4243
public class CriteriaTests {
4344

@@ -311,6 +312,13 @@ public void testBoost() {
311312
Assert.assertEquals(2f, criteria.getBoost(), 0);
312313
}
313314

315+
@Test
316+
public void testConstantScore() {
317+
Criteria criteria = new Criteria("field_1").is("value_1").constantScore(4f);
318+
assertPredicate(criteria.getPredicates(), 0, OperationKey.EQUALS, "value_1");
319+
Assert.assertEquals(4f, criteria.getScore(), 0);
320+
}
321+
314322
@Test
315323
public void testBetween() {
316324
Criteria criteria = new Criteria("field_1").between(100, 200);

0 commit comments

Comments
 (0)