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

DATASOLR-485 Add possibility to have constant score operator #81

Open
wants to merge 1 commit into
base: 3.0.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public abstract class QueryParserBase<QUERYTPYE extends SolrDataQuery> implement
protected static final String DELIMINATOR = ":";
protected static final String NOT = "-";
protected static final String BOOST = "^";
protected static final String CONSTANT_SCORE = "^=";

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

if (!Float.isNaN(criteria.getScore())) {
queryFragment.append(CONSTANT_SCORE).append(criteria.getScore());
}

return queryFragment.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
* @author Christoph Strobl
* @author Philipp Jardas
* @author Francisco Spaeth
* @author Radek Mensik
*/
public class Criteria extends Node {

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

private @Nullable Field field;
private float boost = Float.NaN;
private float score = Float.NaN;

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

Expand Down Expand Up @@ -391,6 +393,14 @@ public Criteria boost(float boost) {
return this;
}

public Criteria constantScore(float score) {
if (score < 0) {
throw new InvalidDataAccessApiUsageException("Score must not be negative.");
}
this.score = score;
return this;
}

/**
* Crates new {@link Predicate} for {@code RANGE [lowerBound TO upperBound]}
*
Expand Down Expand Up @@ -602,6 +612,10 @@ public float getBoost() {
return this.boost;
}

public float getScore() {
return score;
}

/**
* @return unmodifiable set of all {@link Predicate}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

/**
* @author Christoph Strobl
* @author Radek Mensik
* @since 1.2
*/
public class Crotch extends Criteria {
Expand Down Expand Up @@ -56,6 +57,12 @@ public Crotch boost(float boost) {
return this;
}

@Override
public Crotch constantScore(float score) {
mostRecentSibling.constantScore(score);
return this;
}

@Override
public Crotch not() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

/**
* @author Christoph Strobl
* @author Radek Mensik
* @since 1.2
*/
public abstract class Node {
Expand Down Expand Up @@ -175,6 +176,8 @@ protected void setNegating(boolean negating) {

public abstract Node boost(float value);

public abstract Node constantScore(float score);

public abstract Node between(Object lowerBound, Object upperBound);

public abstract Node between(Object lowerBound, Object upperBound, boolean includeLowerBound,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
* @author Francisco Spaeth
* @author Petar Tahchiev
* @author Michael Rocke
* @author Radek Mensik
*/
public class DefaultQueryParserTests {

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

@Test
public void testScoreMultipleValues() {

Criteria criteria = new Criteria("field_1").is("value_1").is("value_2").constantScore(3f);
assertEquals("field_1:(value_1 value_2)^=3.0", queryParser.createQueryStringFromCriteria(criteria));
}

@Test
public void testBoostMultipleCriteriasValues() {
Criteria criteria = new Criteria("field_1").is("value_1").is("value_2").boost(2f).and("field_3").is("value_3");
assertEquals("field_1:(value_1 value_2)^2.0 AND field_3:value_3", queryParser.createQueryStringFromNode(criteria));
}

@Test
public void testScoreMultipleCriteriasValues() {
Criteria criteria = new Criteria("field_1").is("value_1").is("value_2").constantScore(3f).and("field_3").is("value_3");
assertEquals("field_1:(value_1 value_2)^=3.0 AND field_3:value_3", queryParser.createQueryStringFromNode(criteria));
}

@Test
public void testBetween() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
* @author Christoph Strobl
* @author John Dorman
* @author Philipp Jardas
* @author Radek Mensik
*/
public class CriteriaTests {

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

@Test
public void testConstantScore() {
Criteria criteria = new Criteria("field_1").is("value_1").constantScore(4f);
assertPredicate(criteria.getPredicates(), 0, OperationKey.EQUALS, "value_1");
Assert.assertEquals(4f, criteria.getScore(), 0);
}

@Test
public void testBetween() {
Criteria criteria = new Criteria("field_1").between(100, 200);
Expand Down