EQL: Replace EqlSearchResponse.Hits parser with ObjectParser (#50925)

Replaces the existing hand-build Hits parser with a
ConstructingObjectParser version.

Relates to #49581
This commit is contained in:
Igor Motov 2020-01-13 07:53:36 -10:00 committed by Aleksandr Maus
parent 88cc30c0d8
commit c184411456
1 changed files with 23 additions and 49 deletions

View File

@ -23,13 +23,10 @@ import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits; import org.elasticsearch.search.SearchHits;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken;
/** /**
* Response to perform an eql search * Response to perform an eql search
@ -414,53 +411,30 @@ public class EqlSearchResponse extends ActionResponse implements ToXContentObjec
} }
} }
public static Hits fromXContent(XContentParser parser) throws IOException { private static final ConstructingObjectParser<EqlSearchResponse.Hits, Void> PARSER =
if (parser.currentToken() != XContentParser.Token.START_OBJECT) { new ConstructingObjectParser<>("eql/search_response_count", true,
parser.nextToken(); args -> {
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser::getTokenLocation); int i = 0;
} @SuppressWarnings("unchecked") List<SearchHit> searchHits = (List<SearchHit>) args[i++];
XContentParser.Token token = parser.currentToken(); @SuppressWarnings("unchecked") List<Sequence> sequences = (List<Sequence>) args[i++];
String currentFieldName = null; @SuppressWarnings("unchecked") List<Count> counts = (List<Count>) args[i++];
TotalHits totalHits = null; TotalHits totalHits = (TotalHits) args[i];
ArrayList<SearchHit> searchHits = null;
ArrayList<Sequence> sequences = null;
ArrayList<Count> counts = null;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else if (token.isValue()) {
if (Fields.TOTAL.equals(currentFieldName)) {
totalHits = new TotalHits(parser.longValue(), TotalHits.Relation.EQUAL_TO);
}
} else if (token == XContentParser.Token.START_ARRAY) {
if (Fields.EVENTS.equals(currentFieldName)) {
searchHits = new ArrayList<>();
while (parser.nextToken() != XContentParser.Token.END_ARRAY) {
searchHits.add(SearchHit.fromXContent(parser));
}
} else if (Fields.SEQUENCES.equals(currentFieldName)) {
sequences = new ArrayList<>();
while (parser.nextToken() != XContentParser.Token.END_ARRAY) {
sequences.add(Sequence.fromXContent(parser));
}
} else if (Fields.COUNTS.equals(currentFieldName)) {
counts = new ArrayList<>();
while (parser.nextToken() != XContentParser.Token.END_ARRAY) {
counts.add(Count.fromXContent(parser));
}
} else {
parser.skipChildren();
}
} else if (token == XContentParser.Token.START_OBJECT) {
if (SearchHits.Fields.TOTAL.equals(currentFieldName)) {
totalHits = SearchHits.parseTotalHitsFragment(parser);
} else {
parser.skipChildren();
}
}
}
return new EqlSearchResponse.Hits(searchHits, sequences, counts, totalHits); return new EqlSearchResponse.Hits(searchHits, sequences, counts, totalHits);
});
static {
PARSER.declareObjectArray(ConstructingObjectParser.optionalConstructorArg(), (p, c) -> SearchHit.fromXContent(p),
new ParseField(Fields.EVENTS));
PARSER.declareObjectArray(ConstructingObjectParser.optionalConstructorArg(), Sequence.PARSER,
new ParseField(Fields.SEQUENCES));
PARSER.declareObjectArray(ConstructingObjectParser.optionalConstructorArg(), Count.PARSER,
new ParseField(Fields.COUNTS));
PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), (p, c) -> SearchHits.parseTotalHitsFragment(p),
new ParseField(Fields.TOTAL));
}
public static Hits fromXContent(XContentParser parser) throws IOException {
return PARSER.parse(parser, null);
} }
@Override @Override