From f46f6950ec611aefe220c1b21cdaef8b207a0764 Mon Sep 17 00:00:00 2001 From: Yevhen Tienkaiev Date: Thu, 29 Jul 2021 21:28:29 +0300 Subject: [PATCH] Rank feature - unknown field linear (#983) Signed-off-by: Yevhen Tienkaiev --- .../QueryDSLDocumentationTests.java | 6 +++ .../index/query/RankFeatureQueryBuilder.java | 52 ++++++++++++++++++- .../index/query/RankFeatureQueryBuilders.java | 9 ++++ .../query/RankFeatureQueryBuilderTests.java | 5 +- 4 files changed, 70 insertions(+), 2 deletions(-) diff --git a/client/rest-high-level/src/test/java/org/opensearch/client/documentation/QueryDSLDocumentationTests.java b/client/rest-high-level/src/test/java/org/opensearch/client/documentation/QueryDSLDocumentationTests.java index 7b3d21cbaf5..135a0bea730 100644 --- a/client/rest-high-level/src/test/java/org/opensearch/client/documentation/QueryDSLDocumentationTests.java +++ b/client/rest-high-level/src/test/java/org/opensearch/client/documentation/QueryDSLDocumentationTests.java @@ -493,4 +493,10 @@ public class QueryDSLDocumentationTests extends OpenSearchTestCase { 0.6f ); } + + public void testRankFeatureLinear() { + RankFeatureQueryBuilders.linear( + "pagerank" + ); + } } diff --git a/modules/mapper-extras/src/main/java/org/opensearch/index/query/RankFeatureQueryBuilder.java b/modules/mapper-extras/src/main/java/org/opensearch/index/query/RankFeatureQueryBuilder.java index a2301954467..be38eeb0065 100644 --- a/modules/mapper-extras/src/main/java/org/opensearch/index/query/RankFeatureQueryBuilder.java +++ b/modules/mapper-extras/src/main/java/org/opensearch/index/query/RankFeatureQueryBuilder.java @@ -39,6 +39,7 @@ import org.opensearch.common.ParseField; import org.opensearch.common.io.stream.StreamInput; import org.opensearch.common.io.stream.StreamOutput; import org.opensearch.common.xcontent.ConstructingObjectParser; +import org.opensearch.common.xcontent.ObjectParser; import org.opensearch.common.xcontent.XContentBuilder; import org.opensearch.index.mapper.RankFeatureFieldMapper.RankFeatureFieldType; import org.opensearch.index.mapper.RankFeatureMetaFieldMapper; @@ -257,6 +258,51 @@ public final class RankFeatureQueryBuilder extends AbstractQueryBuilder PARSER = new ObjectParser<>("linear", Linear::new); + + public Linear() { + } + + private Linear(StreamInput in) { + this(); + } + + @Override + public boolean equals(Object obj) { + if (obj == null || getClass() != obj.getClass()) { + return false; + } + return true; + } + + @Override + public int hashCode() { + return getClass().hashCode(); + } + + @Override + void writeTo(StreamOutput out) throws IOException { + out.writeByte((byte) 3); + } + + @Override + void doXContent(XContentBuilder builder) throws IOException { + builder.startObject("linear"); + builder.endObject(); + } + + @Override + Query toQuery(String field, String feature, boolean positiveScoreImpact) throws IOException { + return FeatureField.newLinearQuery(field, feature, DEFAULT_BOOST); + } + } } private static ScoreFunction readScoreFunction(StreamInput in) throws IOException { @@ -268,6 +314,8 @@ public final class RankFeatureQueryBuilder extends AbstractQueryBuilder 1) { - throw new IllegalArgumentException("Can only specify one of [log], [saturation] and [sigmoid]"); + throw new IllegalArgumentException("Can only specify one of [log], [saturation], [sigmoid] and [linear]"); } else if (numNonNulls == 0) { query = new RankFeatureQueryBuilder(field, new ScoreFunction.Saturation()); } else { @@ -305,6 +353,8 @@ public final class RankFeatureQueryBuilder extends AbstractQueryBuilder