Skip to content

Commit

Permalink
EQL: Replace EqlSearchResponse.Hits parser with ObjectParser (elastic…
Browse files Browse the repository at this point in the history
…#50925)

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

Relates to elastic#49581
  • Loading branch information
imotov authored and aleksmaus committed Jan 27, 2020
1 parent 88cc30c commit c184411
Showing 1 changed file with 23 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,10 @@
import org.elasticsearch.search.SearchHits;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken;


/**
* Response to perform an eql search
Expand Down Expand Up @@ -414,53 +411,30 @@ public void writeTo(StreamOutput out) throws IOException {
}
}

private static final ConstructingObjectParser<EqlSearchResponse.Hits, Void> PARSER =
new ConstructingObjectParser<>("eql/search_response_count", true,
args -> {
int i = 0;
@SuppressWarnings("unchecked") List<SearchHit> searchHits = (List<SearchHit>) args[i++];
@SuppressWarnings("unchecked") List<Sequence> sequences = (List<Sequence>) args[i++];
@SuppressWarnings("unchecked") List<Count> counts = (List<Count>) args[i++];
TotalHits totalHits = (TotalHits) args[i];
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 {
if (parser.currentToken() != XContentParser.Token.START_OBJECT) {
parser.nextToken();
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser::getTokenLocation);
}
XContentParser.Token token = parser.currentToken();
String currentFieldName = null;
TotalHits totalHits = null;
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 PARSER.parse(parser, null);
}

@Override
Expand Down

0 comments on commit c184411

Please sign in to comment.