Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accommodate changes to XContent classes #125

Merged
merged 1 commit into from
Apr 1, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import org.opensearch.cluster.service.ClusterService;
import org.opensearch.common.io.stream.NamedWriteableRegistry;
import org.opensearch.common.settings.Setting;
import org.opensearch.common.xcontent.NamedXContentRegistry;
import org.opensearch.core.xcontent.NamedXContentRegistry;
import org.opensearch.env.Environment;
import org.opensearch.env.NodeEnvironment;
import org.opensearch.plugins.ActionPlugin;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import org.opensearch.common.io.stream.StreamInput;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.xcontent.XContentParser;
import org.opensearch.core.xcontent.XContentParser;

import java.io.IOException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
*/
package org.opensearch.search.relevance.configuration;

import org.opensearch.common.ParseField;
import org.opensearch.common.ParsingException;
import org.opensearch.common.io.stream.StreamInput;
import org.opensearch.common.io.stream.StreamOutput;
import org.opensearch.common.xcontent.XContentBuilder;
import org.opensearch.common.xcontent.XContentParser;
import org.opensearch.core.ParseField;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.search.SearchExtBuilder;
import org.opensearch.search.relevance.transformer.TransformerType;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
import static org.opensearch.search.relevance.configuration.Constants.ORDER;
import static org.opensearch.search.relevance.configuration.Constants.PROPERTIES;

import org.opensearch.common.ParseField;
import org.opensearch.common.io.stream.Writeable;
import org.opensearch.common.xcontent.ToXContentObject;
import org.opensearch.core.ParseField;
import org.opensearch.core.xcontent.ToXContentObject;

public abstract class TransformerConfiguration implements Writeable, ToXContentObject {
protected static final ParseField TRANSFORMER_ORDER = new ParseField(ORDER);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@
*/
package org.opensearch.search.relevance.transformer.kendraintelligentranking.configuration;

import org.opensearch.common.ParseField;
import org.opensearch.common.ParsingException;
import org.opensearch.common.io.stream.StreamInput;
import org.opensearch.common.io.stream.StreamOutput;
import org.opensearch.common.io.stream.Writeable;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.xcontent.ObjectParser;
import org.opensearch.common.xcontent.ToXContentObject;
import org.opensearch.common.xcontent.XContentBuilder;
import org.opensearch.common.xcontent.XContentParser;
import org.opensearch.core.ParseField;
import org.opensearch.core.xcontent.ObjectParser;
import org.opensearch.core.xcontent.ToXContent;
import org.opensearch.core.xcontent.ToXContentObject;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.search.relevance.configuration.ResultTransformerConfiguration;
import org.opensearch.search.relevance.configuration.TransformerConfiguration;
import org.opensearch.search.relevance.transformer.kendraintelligentranking.KendraIntelligentRanker;
Expand Down Expand Up @@ -76,7 +77,7 @@ public void writeTo(StreamOutput out) throws IOException {
this.properties.writeTo(out);
}

public static ResultTransformerConfiguration parse(XContentParser parser) throws IOException {
public static KendraIntelligentRankingConfiguration parse(XContentParser parser) throws IOException {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re:naming - is this going from less specific naming to more specific intentionally?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.

a) It helps the unit tests, since we can confirm the specific type.
b) There's a best-practice to take the most general type of argument and return the most specific type. I think it's an application of the Liskov substitution principle.

try {
KendraIntelligentRankingConfiguration configuration = PARSER.parse(parser, null);
if (configuration != null && configuration.getOrder() <= 0) {
Expand All @@ -90,7 +91,7 @@ public static ResultTransformerConfiguration parse(XContentParser parser) throws
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException {
builder.startObject();
builder.field(TRANSFORMER_ORDER.getPreferredName(), this.order);
builder.field(TRANSFORMER_PROPERTIES.getPreferredName(), this.properties);
Expand Down Expand Up @@ -154,7 +155,7 @@ public KendraIntelligentRankingProperties(final List<String> bodyFields,

public KendraIntelligentRankingProperties(StreamInput input) throws IOException {
this.bodyFields = input.readStringList();
this.bodyFields = input.readStringList();
this.titleFields = input.readStringList();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this related to the XContent classes change?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not directly.

When merging the cherry-picked commit, I also picked up the unit tests that I had previously added (in KendraIntelligentRankingConfigurationTests).

When I added those tests in main, they also revealed some bugs (like this one). I should go through and make sure that all of the tests I added in main get backported to 2.x, since I fixed a bunch of little bugs along the way.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh... I just added the backport 2.x label to #99.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, but the backport PR fails to build because we need to merge this PR to fix the XContent stuff:

#126

Copy link
Collaborator

@noCharger noCharger Mar 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, but the backport PR fails to build because we need to merge this PR to fix the XContent stuff:

#126

Looks like there are two ways to handle this issue:

  1. In this PR, remove the content of Improve test coverage #99, merge this one and merge [Backport 2.x] Improve test coverage #126
  2. Change the description to include the backport of Improve test coverage #99, then merge this one; the backport PR [Backport 2.x] Improve test coverage #126 is pointless in this case because the code is already include.

I'm fine with either, but 1. appears to overreact, and the order of commits in 2.x is messed up comparing to main branch

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think #126 shouldn't have any conflicts once this one is merged, because any overlapping changes should be identical.

this.docLimit = input.readInt();
}

Expand Down Expand Up @@ -195,7 +196,7 @@ public boolean equals(Object o) {

KendraIntelligentRankingProperties properties = (KendraIntelligentRankingProperties) o;

return (bodyFields == properties.bodyFields) && (titleFields == properties.titleFields) &&
return Objects.equals(bodyFields, properties.bodyFields) && Objects.equals(titleFields, properties.titleFields) &&
(docLimit == properties.docLimit);
}

Expand Down Expand Up @@ -228,4 +229,6 @@ public void setDocLimit(final int docLimit) {
this.docLimit = docLimit;
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import org.opensearch.common.io.stream.StreamInput;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.xcontent.XContentParser;
import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.search.relevance.configuration.ResultTransformerConfiguration;
import org.opensearch.search.relevance.configuration.ResultTransformerConfigurationFactory;
import org.opensearch.search.relevance.transformer.kendraintelligentranking.KendraIntelligentRanker;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
import org.opensearch.common.io.stream.StreamOutput;
import org.opensearch.common.settings.Setting;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.xcontent.XContentBuilder;
import org.opensearch.common.xcontent.XContentParser;
import org.opensearch.common.xcontent.json.JsonXContent;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.search.SearchHit;
import org.opensearch.search.SearchHits;
import org.opensearch.search.builder.SearchSourceBuilder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
import org.opensearch.common.io.stream.StreamInput;
import org.opensearch.common.io.stream.StreamOutput;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.xcontent.XContentBuilder;
import org.opensearch.common.xcontent.XContentHelper;
import org.opensearch.common.xcontent.XContentParser;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.test.OpenSearchTestCase;

import java.io.IOException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
import org.opensearch.common.bytes.BytesReference;
import org.opensearch.common.settings.Setting;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.xcontent.XContentBuilder;
import org.opensearch.common.xcontent.json.JsonXContent;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.index.query.MatchAllQueryBuilder;
import org.opensearch.index.query.MatchQueryBuilder;
import org.opensearch.search.SearchHit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,73 @@
*/
package org.opensearch.search.relevance.transformer.kendraintelligentranking.configuration;

import org.junit.Test;
import org.opensearch.common.bytes.BytesReference;
import org.opensearch.common.io.stream.BytesStreamOutput;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.xcontent.XContentHelper;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.search.relevance.configuration.Constants;
import org.opensearch.test.OpenSearchTestCase;

import java.io.IOException;
import java.util.List;

public class KendraIntelligentRankingConfigurationTests extends OpenSearchTestCase {
public void testParseWithNullParserAndContext() {
try {
KendraIntelligentRankingConfiguration.parse(null);
fail();
} catch (NullPointerException | IOException e) {
}
public void testParseWithNullParserAndContext() throws IOException {
expectThrows(NullPointerException.class, () -> KendraIntelligentRankingConfiguration.parse(null));
}

public void testSerializeToXContentRoundtrip() throws IOException {
KendraIntelligentRankingConfiguration expected = getKendraIntelligentRankingConfiguration();

XContentType xContentType = randomFrom(XContentType.values());
BytesReference serialized = XContentHelper.toXContent(expected, xContentType, true);

XContentParser parser = createParser(xContentType.xContent(), serialized);

KendraIntelligentRankingConfiguration deserialized =
KendraIntelligentRankingConfiguration.parse(parser);
assertEquals(expected, deserialized);
}

public void testSerializeToStreamRoundtrip() throws IOException {
KendraIntelligentRankingConfiguration expected = getKendraIntelligentRankingConfiguration();
BytesStreamOutput bytesStreamOutput = new BytesStreamOutput();
expected.writeTo(bytesStreamOutput);

KendraIntelligentRankingConfiguration deserialized =
new KendraIntelligentRankingConfiguration(bytesStreamOutput.bytes().streamInput());
assertEquals(expected, deserialized);
}

private static KendraIntelligentRankingConfiguration getKendraIntelligentRankingConfiguration() {
int order = randomInt(10) + 1;
int docLimit = randomInt( Integer.MAX_VALUE - 25) + 25;
KendraIntelligentRankingConfiguration.KendraIntelligentRankingProperties properties =
new KendraIntelligentRankingConfiguration.KendraIntelligentRankingProperties(List.of("body1"),
List.of("title1"), docLimit);
return new KendraIntelligentRankingConfiguration(order, properties);
}

public void testReadFromSettings() throws IOException {
int order = randomInt(10);
int docLimit = randomInt(100) + 25;
String bodyField = "body1";
String titleField = "title1";
Settings settings = Settings.builder()
.put(Constants.ORDER, order)
.put("properties.doc_limit", docLimit)
.put("properties.body_field", bodyField)
.putList("properties.title_field", titleField)
.build();

KendraIntelligentRankingConfiguration expected = new KendraIntelligentRankingConfiguration(order,
new KendraIntelligentRankingConfiguration.KendraIntelligentRankingProperties(List.of(bodyField),
List.of(titleField), docLimit));

KendraIntelligentRankingConfiguration actual = new KendraIntelligentRankingConfiguration(settings);

assertEquals(expected, actual);
}
}