Skip to content

Commit 6cb97a5

Browse files
authored
Allow unsigned long field to use decay functions (#96394)
This change adds support for unsigned long fields (and any fields that can provide numeric data) to use decay functions as described by (#89603). This allows mapped fields developed as plugins to also have access to this set of functions provided they return IndexNumericFieldData from getForField. Closes #89603.
1 parent 494847b commit 6cb97a5

File tree

3 files changed

+46
-11
lines changed

3 files changed

+46
-11
lines changed

docs/changelog/96394.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pr: 96394
2+
summary: Allow unsigned long field to use decay functions
3+
area: Mapping
4+
type: enhancement
5+
issues:
6+
- 89603

server/src/main/java/org/elasticsearch/index/query/functionscore/DecayFunctionBuilder.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.elasticsearch.common.xcontent.XContentHelper;
2727
import org.elasticsearch.core.TimeValue;
2828
import org.elasticsearch.index.fielddata.FieldData;
29+
import org.elasticsearch.index.fielddata.IndexFieldData;
2930
import org.elasticsearch.index.fielddata.IndexGeoPointFieldData;
3031
import org.elasticsearch.index.fielddata.IndexNumericFieldData;
3132
import org.elasticsearch.index.fielddata.MultiGeoPointValues;
@@ -35,7 +36,6 @@
3536
import org.elasticsearch.index.mapper.DateFieldMapper;
3637
import org.elasticsearch.index.mapper.GeoPointFieldMapper.GeoPointFieldType;
3738
import org.elasticsearch.index.mapper.MappedFieldType;
38-
import org.elasticsearch.index.mapper.NumberFieldMapper;
3939
import org.elasticsearch.index.query.SearchExecutionContext;
4040
import org.elasticsearch.search.MultiValueMode;
4141
import org.elasticsearch.xcontent.NamedXContentRegistry;
@@ -217,15 +217,8 @@ private AbstractDistanceScoreFunction parseVariable(
217217
return parseDateVariable(parser, context, fieldType, mode);
218218
} else if (fieldType instanceof GeoPointFieldType) {
219219
return parseGeoVariable(parser, context, fieldType, mode);
220-
} else if (fieldType instanceof NumberFieldMapper.NumberFieldType) {
221-
return parseNumberVariable(parser, context, fieldType, mode);
222220
} else {
223-
throw new ParsingException(
224-
parser.getTokenLocation(),
225-
"field [{}] is of type [{}], but only numeric types are supported.",
226-
fieldName,
227-
fieldType
228-
);
221+
return parseNumberVariable(parser, context, fieldType, mode);
229222
}
230223
}
231224

@@ -267,8 +260,15 @@ private AbstractDistanceScoreFunction parseNumberVariable(
267260
DecayFunctionBuilder.ORIGIN
268261
);
269262
}
270-
IndexNumericFieldData numericFieldData = context.getForField(fieldType, MappedFieldType.FielddataOperation.SEARCH);
271-
return new NumericFieldDataScoreFunction(origin, scale, decay, offset, getDecayFunction(), numericFieldData, mode);
263+
264+
IndexFieldData<?> indexFieldData = context.getForField(fieldType, MappedFieldType.FielddataOperation.SEARCH);
265+
if (indexFieldData instanceof IndexNumericFieldData numericFieldData) {
266+
return new NumericFieldDataScoreFunction(origin, scale, decay, offset, getDecayFunction(), numericFieldData, mode);
267+
} else {
268+
throw new IllegalArgumentException(
269+
"field [" + fieldName + "] is of type [" + fieldType + "], but only numeric types are supported."
270+
);
271+
}
272272
}
273273

274274
private AbstractDistanceScoreFunction parseGeoVariable(

x-pack/plugin/mapper-unsigned-long/src/yamlRestTest/resources/rest-api-spec/test/10_basic.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,3 +376,32 @@ setup:
376376
}]
377377

378378
- length: { aggregations.test.buckets: 0 }
379+
380+
---
381+
"Decay":
382+
- skip:
383+
features: close_to
384+
version: " - 8.8.99"
385+
reason: "decay functions not supported for unsigned_long"
386+
387+
- do:
388+
search:
389+
index: test1
390+
body:
391+
size: 10
392+
query:
393+
function_score:
394+
functions: [{
395+
"linear": {
396+
"ul": {
397+
"scale": 18000000000000000000.0,
398+
"origin": 12000000000000000000.0
399+
}
400+
}
401+
}]
402+
403+
- close_to: { hits.hits.0._score: { value: 0.9228715, error: 0.001 } }
404+
- close_to: { hits.hits.1._score: { value: 0.9228715, error: 0.001 } }
405+
- close_to: { hits.hits.2._score: { value: 0.8209238, error: 0.001 } }
406+
- close_to: { hits.hits.3._score: { value: 0.8209238, error: 0.001 } }
407+
- close_to: { hits.hits.4._score: { value: 0.6666667, error: 0.001 } }

0 commit comments

Comments
 (0)