Skip to content

Move test-only search response x-content-parsing code to test codebase #105308

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

Merged
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

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ private static MultiSearchTemplateResponse createTestInstanceWithFailures() {
@Override
protected MultiSearchTemplateResponse doParseInstance(XContentParser parser) {
// The MultiSearchTemplateResponse is identical to the multi search response so we reuse the parsing logic in multi search response
MultiSearchResponse mSearchResponse = MultiSearchResponse.fromXContext(parser);
MultiSearchResponse mSearchResponse = SearchResponseUtils.parseMultiSearchResponse(parser);
try {
org.elasticsearch.action.search.MultiSearchResponse.Item[] responses = mSearchResponse.getResponses();
MultiSearchTemplateResponse.Item[] templateResponses = new MultiSearchTemplateResponse.Item[responses.length];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ protected SearchTemplateResponse doParseInstance(XContentParser parser) throws I
contentType
)
) {
searchTemplateResponse.setResponse(SearchResponse.fromXContent(searchResponseParser));
searchTemplateResponse.setResponse(SearchResponseUtils.parseSearchResponse(searchResponseParser));
}
}
return searchTemplateResponse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,10 @@
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.xcontent.XContentParserUtils;
import org.elasticsearch.index.rankeval.RatedDocument.DocumentKey;
import org.elasticsearch.xcontent.ObjectParser;
import org.elasticsearch.xcontent.ParseField;
import org.elasticsearch.xcontent.ToXContentFragment;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.XContentParser;

import java.io.IOException;
import java.util.ArrayList;
Expand All @@ -35,24 +32,23 @@ public class EvalQueryQuality implements ToXContentFragment, Writeable {
private final List<RatedSearchHit> ratedHits;

public EvalQueryQuality(String id, double metricScore) {
this.queryId = id;
this.metricScore = metricScore;
this.ratedHits = new ArrayList<>();
this(id, metricScore, new ArrayList<>(), null);
}

public EvalQueryQuality(StreamInput in) throws IOException {
this.queryId = in.readString();
this.metricScore = in.readDouble();
this.ratedHits = in.readCollectionAsList(RatedSearchHit::new);
this.optionalMetricDetails = in.readOptionalNamedWriteable(MetricDetail.class);
this(
in.readString(),
in.readDouble(),
in.readCollectionAsList(RatedSearchHit::new),
in.readOptionalNamedWriteable(MetricDetail.class)
);
}

// only used for parsing internally
private EvalQueryQuality(String queryId, ParsedEvalQueryQuality builder) {
EvalQueryQuality(String queryId, double evaluationResult, List<RatedSearchHit> ratedHits, MetricDetail optionalMetricDetails) {
this.queryId = queryId;
this.metricScore = builder.evaluationResult;
this.optionalMetricDetails = builder.optionalMetricDetails;
this.ratedHits = builder.ratedHits;
this.metricScore = evaluationResult;
this.optionalMetricDetails = optionalMetricDetails;
this.ratedHits = ratedHits;
}

@Override
Expand Down Expand Up @@ -113,37 +109,8 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws

static final ParseField METRIC_SCORE_FIELD = new ParseField("metric_score");
private static final ParseField UNRATED_DOCS_FIELD = new ParseField("unrated_docs");
private static final ParseField HITS_FIELD = new ParseField("hits");
private static final ParseField METRIC_DETAILS_FIELD = new ParseField("metric_details");
private static final ObjectParser<ParsedEvalQueryQuality, Void> PARSER = new ObjectParser<>(
"eval_query_quality",
true,
ParsedEvalQueryQuality::new
);

private static class ParsedEvalQueryQuality {
double evaluationResult;
MetricDetail optionalMetricDetails;
List<RatedSearchHit> ratedHits = new ArrayList<>();
}

static {
PARSER.declareDouble((obj, value) -> obj.evaluationResult = value, METRIC_SCORE_FIELD);
PARSER.declareObject((obj, value) -> obj.optionalMetricDetails = value, (p, c) -> parseMetricDetail(p), METRIC_DETAILS_FIELD);
PARSER.declareObjectArray((obj, list) -> obj.ratedHits = list, (p, c) -> RatedSearchHit.parse(p), HITS_FIELD);
}

private static MetricDetail parseMetricDetail(XContentParser parser) throws IOException {
XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser);
XContentParserUtils.ensureExpectedToken(XContentParser.Token.FIELD_NAME, parser.nextToken(), parser);
MetricDetail metricDetail = parser.namedObject(MetricDetail.class, parser.currentName(), null);
XContentParserUtils.ensureExpectedToken(XContentParser.Token.END_OBJECT, parser.nextToken(), parser);
return metricDetail;
}

public static EvalQueryQuality fromXContent(XContentParser parser, String queryId) throws IOException {
return new EvalQueryQuality(queryId, PARSER.apply(parser, null));
}
static final ParseField HITS_FIELD = new ParseField("hits");
static final ParseField METRIC_DETAILS_FIELD = new ParseField("metric_details");

@Override
public final boolean equals(Object obj) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,9 @@
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.xcontent.ConstructingObjectParser;
import org.elasticsearch.xcontent.ObjectParser.ValueType;
import org.elasticsearch.xcontent.ParseField;
import org.elasticsearch.xcontent.ToXContent;
import org.elasticsearch.xcontent.ToXContentObject;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.XContentParser;

import java.io.IOException;
import java.util.Objects;
Expand Down Expand Up @@ -67,28 +63,6 @@ public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params par
return builder;
}

private static final ParseField HIT_FIELD = new ParseField("hit");
private static final ParseField RATING_FIELD = new ParseField("rating");
private static final ConstructingObjectParser<RatedSearchHit, Void> PARSER = new ConstructingObjectParser<>(
"rated_hit",
true,
a -> new RatedSearchHit((SearchHit) a[0], (OptionalInt) a[1])
);

static {
PARSER.declareObject(ConstructingObjectParser.constructorArg(), (p, c) -> SearchHit.fromXContent(p), HIT_FIELD);
PARSER.declareField(
ConstructingObjectParser.constructorArg(),
(p) -> p.currentToken() == XContentParser.Token.VALUE_NULL ? OptionalInt.empty() : OptionalInt.of(p.intValue()),
RATING_FIELD,
ValueType.INT_OR_NULL
);
}

public static RatedSearchHit parse(XContentParser parser) throws IOException {
return PARSER.apply(parser, null);
}

@Override
public final boolean equals(Object obj) {
if (this == obj) {
Expand Down
Loading