Skip to content
Merged
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
6 changes: 6 additions & 0 deletions docs/changelog/91195.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pr: 91195
summary: Allow different decay values depending on the score function
area: Search
type: bug
issues:
- 78887
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,7 @@ protected DecayFunctionBuilder(String fieldName, Object origin, Object scale, Ob
if (scale == null) {
throw new IllegalArgumentException("decay function: scale must not be null");
}
if (decay <= 0 || decay >= 1.0) {
throw new IllegalStateException("decay function: decay must be in range 0..1!");
}
validateDecay(decay);
this.fieldName = fieldName;
try {
XContentBuilder builder = XContentFactory.jsonBuilder();
Expand Down Expand Up @@ -112,6 +110,15 @@ protected DecayFunctionBuilder(String fieldName, BytesReference functionBytes) {
this.functionBytes = functionBytes;
}

/**
* Override this function if you have different validation rules per score function
*/
protected void validateDecay(double decay) {
if (decay <= 0 || decay >= 1.0) {
throw new IllegalStateException("decay function: decay must be in range (0..1)!");
}
}

/**
* Read from a stream.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ public LinearDecayFunctionBuilder(StreamInput in) throws IOException {
super(in);
}

@Override
protected void validateDecay(double decay) {
if (decay < 0 || decay >= 1.0) {
throw new IllegalStateException("decay function: decay must be in range [0..1)!");
}
}

@Override
public String getName() {
return NAME;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ public void testIllegalArguments() {
expectThrows(IllegalArgumentException.class, () -> new ExponentialDecayFunctionBuilder("", "", null, "", randomDouble()));
}

public void testDecayValues() {
expectThrows(IllegalStateException.class, () -> new ExponentialDecayFunctionBuilder("", "", "", "", 0.0));
expectThrows(IllegalStateException.class, () -> new ExponentialDecayFunctionBuilder("", "", "", "", 1.0));
expectThrows(IllegalStateException.class, () -> new GaussDecayFunctionBuilder("", "", "", "", 0.0));
expectThrows(IllegalStateException.class, () -> new GaussDecayFunctionBuilder("", "", "", "", 1.0));
expectThrows(IllegalStateException.class, () -> new LinearDecayFunctionBuilder("", "", "", "", 1.0));
expectThrows(IllegalStateException.class, () -> new LinearDecayFunctionBuilder("", "", "", "", -1.0));
// should not throw since the score formula allows it
new LinearDecayFunctionBuilder("", "", "", "", 0.0);
}

public void testRandomScoreFunctionWithSeedNoField() throws Exception {
RandomScoreFunctionBuilder builder = new RandomScoreFunctionBuilder();
builder.seed(42);
Expand Down