26
26
import org .elasticsearch .common .io .stream .StreamInput ;
27
27
import org .elasticsearch .common .io .stream .StreamOutput ;
28
28
import org .elasticsearch .common .xcontent .ConstructingObjectParser ;
29
+ import org .elasticsearch .common .xcontent .ObjectParser ;
29
30
import org .elasticsearch .common .xcontent .XContentBuilder ;
30
31
import org .elasticsearch .index .mapper .MappedFieldType ;
31
32
import org .elasticsearch .index .mapper .RankFeatureFieldMapper .RankFeatureFieldType ;
@@ -104,7 +105,7 @@ void doXContent(XContentBuilder builder) throws IOException {
104
105
}
105
106
106
107
@ Override
107
- Query toQuery (String field , String feature , boolean positiveScoreImpact ) throws IOException {
108
+ Query toQuery (String field , String feature , boolean positiveScoreImpact ) {
108
109
if (positiveScoreImpact == false ) {
109
110
throw new IllegalArgumentException ("Cannot use the [log] function with a field that has a negative score impact as " +
110
111
"it would trigger negative scores" );
@@ -175,7 +176,7 @@ void doXContent(XContentBuilder builder) throws IOException {
175
176
}
176
177
177
178
@ Override
178
- Query toQuery (String field , String feature , boolean positiveScoreImpact ) throws IOException {
179
+ Query toQuery (String field , String feature , boolean positiveScoreImpact ) {
179
180
if (pivot == null ) {
180
181
return FeatureField .newSaturationQuery (field , feature );
181
182
} else {
@@ -240,10 +241,55 @@ void doXContent(XContentBuilder builder) throws IOException {
240
241
}
241
242
242
243
@ Override
243
- Query toQuery (String field , String feature , boolean positiveScoreImpact ) throws IOException {
244
+ Query toQuery (String field , String feature , boolean positiveScoreImpact ) {
244
245
return FeatureField .newSigmoidQuery (field , feature , DEFAULT_BOOST , pivot , exp );
245
246
}
246
247
}
248
+
249
+ /**
250
+ * A scoring function that scores documents as simply {@code S}
251
+ * where S is the indexed value of the static feature.
252
+ */
253
+ public static class Linear extends ScoreFunction {
254
+
255
+ private static final ObjectParser <Linear , Void > PARSER = new ObjectParser <>("linear" , Linear ::new );
256
+
257
+ public Linear () {
258
+ }
259
+
260
+ private Linear (StreamInput in ) {
261
+ this ();
262
+ }
263
+
264
+ @ Override
265
+ public boolean equals (Object obj ) {
266
+ if (obj == null || getClass () != obj .getClass ()) {
267
+ return false ;
268
+ }
269
+ return true ;
270
+ }
271
+
272
+ @ Override
273
+ public int hashCode () {
274
+ return getClass ().hashCode ();
275
+ }
276
+
277
+ @ Override
278
+ void writeTo (StreamOutput out ) throws IOException {
279
+ out .writeByte ((byte ) 3 );
280
+ }
281
+
282
+ @ Override
283
+ void doXContent (XContentBuilder builder ) throws IOException {
284
+ builder .startObject ("linear" );
285
+ builder .endObject ();
286
+ }
287
+
288
+ @ Override
289
+ Query toQuery (String field , String feature , boolean positiveScoreImpact ) {
290
+ return FeatureField .newLinearQuery (field , feature , DEFAULT_BOOST );
291
+ }
292
+ }
247
293
}
248
294
249
295
private static ScoreFunction readScoreFunction (StreamInput in ) throws IOException {
@@ -255,6 +301,8 @@ private static ScoreFunction readScoreFunction(StreamInput in) throws IOExceptio
255
301
return new ScoreFunction .Saturation (in );
256
302
case 2 :
257
303
return new ScoreFunction .Sigmoid (in );
304
+ case 3 :
305
+ return new ScoreFunction .Linear (in );
258
306
default :
259
307
throw new IOException ("Illegal score function id: " + b );
260
308
}
@@ -268,7 +316,7 @@ private static ScoreFunction readScoreFunction(StreamInput in) throws IOExceptio
268
316
long numNonNulls = Arrays .stream (args , 3 , args .length ).filter (Objects ::nonNull ).count ();
269
317
final RankFeatureQueryBuilder query ;
270
318
if (numNonNulls > 1 ) {
271
- throw new IllegalArgumentException ("Can only specify one of [log], [saturation] and [sigmoid ]" );
319
+ throw new IllegalArgumentException ("Can only specify one of [log], [saturation], [sigmoid] and [linear ]" );
272
320
} else if (numNonNulls == 0 ) {
273
321
query = new RankFeatureQueryBuilder (field , new ScoreFunction .Saturation ());
274
322
} else {
@@ -292,6 +340,8 @@ private static ScoreFunction readScoreFunction(StreamInput in) throws IOExceptio
292
340
ScoreFunction .Saturation .PARSER , new ParseField ("saturation" ));
293
341
PARSER .declareObject (ConstructingObjectParser .optionalConstructorArg (),
294
342
ScoreFunction .Sigmoid .PARSER , new ParseField ("sigmoid" ));
343
+ PARSER .declareObject (ConstructingObjectParser .optionalConstructorArg (),
344
+ ScoreFunction .Linear .PARSER , new ParseField ("linear" ));
295
345
}
296
346
297
347
public static final String NAME = "rank_feature" ;
0 commit comments