Skip to content

Commit f76ebaa

Browse files
committed
Rank feature - unknown field linear
Signed-off-by: Yevhen Tienkaiev <[email protected]>
1 parent 23d6045 commit f76ebaa

File tree

4 files changed

+69
-1
lines changed

4 files changed

+69
-1
lines changed

client/rest-high-level/src/test/java/org/opensearch/client/documentation/QueryDSLDocumentationTests.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,4 +493,10 @@ public void testRankFeatureSigmoid() {
493493
0.6f
494494
);
495495
}
496+
497+
public void testRankFeatureLinear() {
498+
RankFeatureQueryBuilders.linear(
499+
"pagerank"
500+
);
501+
}
496502
}

modules/mapper-extras/src/main/java/org/opensearch/index/query/RankFeatureQueryBuilder.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.opensearch.common.io.stream.StreamInput;
4040
import org.opensearch.common.io.stream.StreamOutput;
4141
import org.opensearch.common.xcontent.ConstructingObjectParser;
42+
import org.opensearch.common.xcontent.ObjectParser;
4243
import org.opensearch.common.xcontent.XContentBuilder;
4344
import org.opensearch.index.mapper.RankFeatureFieldMapper.RankFeatureFieldType;
4445
import org.opensearch.index.mapper.RankFeatureMetaFieldMapper;
@@ -257,6 +258,51 @@ Query toQuery(String field, String feature, boolean positiveScoreImpact) throws
257258
return FeatureField.newSigmoidQuery(field, feature, DEFAULT_BOOST, pivot, exp);
258259
}
259260
}
261+
262+
/**
263+
* A scoring function that scores documents as simply {@code S}
264+
* where S is the indexed value of the static feature.
265+
*/
266+
public static class Linear extends ScoreFunction {
267+
268+
private static final ObjectParser<Linear, Void> PARSER = new ObjectParser<>("linear", Linear::new);
269+
270+
public Linear() {
271+
}
272+
273+
private Linear(StreamInput in) {
274+
this();
275+
}
276+
277+
@Override
278+
public boolean equals(Object obj) {
279+
if (obj == null || getClass() != obj.getClass()) {
280+
return false;
281+
}
282+
return true;
283+
}
284+
285+
@Override
286+
public int hashCode() {
287+
return getClass().hashCode();
288+
}
289+
290+
@Override
291+
void writeTo(StreamOutput out) throws IOException {
292+
out.writeByte((byte) 3);
293+
}
294+
295+
@Override
296+
void doXContent(XContentBuilder builder) throws IOException {
297+
builder.startObject("linear");
298+
builder.endObject();
299+
}
300+
301+
@Override
302+
Query toQuery(String field, String feature, boolean positiveScoreImpact) throws IOException {
303+
return FeatureField.newLinearQuery(field, feature, DEFAULT_BOOST);
304+
}
305+
}
260306
}
261307

262308
private static ScoreFunction readScoreFunction(StreamInput in) throws IOException {
@@ -268,6 +314,8 @@ private static ScoreFunction readScoreFunction(StreamInput in) throws IOExceptio
268314
return new ScoreFunction.Saturation(in);
269315
case 2:
270316
return new ScoreFunction.Sigmoid(in);
317+
case 3:
318+
return new ScoreFunction.Linear(in);
271319
default:
272320
throw new IOException("Illegal score function id: " + b);
273321
}
@@ -305,6 +353,8 @@ private static ScoreFunction readScoreFunction(StreamInput in) throws IOExceptio
305353
ScoreFunction.Saturation.PARSER, new ParseField("saturation"));
306354
PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(),
307355
ScoreFunction.Sigmoid.PARSER, new ParseField("sigmoid"));
356+
PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(),
357+
ScoreFunction.Linear.PARSER, new ParseField("linear"));
308358
}
309359

310360
public static final String NAME = "rank_feature";

modules/mapper-extras/src/main/java/org/opensearch/index/query/RankFeatureQueryBuilders.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,13 @@ public static RankFeatureQueryBuilder sigmoid(String fieldName, float pivot, flo
7777
return new RankFeatureQueryBuilder(fieldName, new RankFeatureQueryBuilder.ScoreFunction.Sigmoid(pivot, exp));
7878
}
7979

80+
/**
81+
* Return a new {@link RankFeatureQueryBuilder} that will score documents as
82+
* {@code S)} where S is the indexed value of the static feature.
83+
* @param fieldName field that stores features
84+
*/
85+
public static RankFeatureQueryBuilder linear(String fieldName) {
86+
return new RankFeatureQueryBuilder(fieldName, new RankFeatureQueryBuilder.ScoreFunction.Linear());
87+
}
88+
8089
}

modules/mapper-extras/src/test/java/org/opensearch/index/query/RankFeatureQueryBuilderTests.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ protected Collection<Class<? extends Plugin>> getPlugins() {
7373
protected RankFeatureQueryBuilder doCreateTestQueryBuilder() {
7474
ScoreFunction function;
7575
boolean mayUseNegativeField = true;
76-
switch (random().nextInt(3)) {
76+
switch (random().nextInt(4)) {
7777
case 0:
7878
mayUseNegativeField = false;
7979
function = new ScoreFunction.Log(1 + randomFloat());
@@ -88,6 +88,9 @@ protected RankFeatureQueryBuilder doCreateTestQueryBuilder() {
8888
case 2:
8989
function = new ScoreFunction.Sigmoid(randomFloat(), randomFloat());
9090
break;
91+
case 3:
92+
function = new ScoreFunction.Linear();
93+
break;
9194
default:
9295
throw new AssertionError();
9396
}

0 commit comments

Comments
 (0)