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:
parent
88cc30c0d8
commit
c184411456
|
@ -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;
|
return new EqlSearchResponse.Hits(searchHits, sequences, counts, totalHits);
|
||||||
ArrayList<Sequence> sequences = null;
|
});
|
||||||
ArrayList<Count> counts = null;
|
|
||||||
|
|
||||||
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
|
static {
|
||||||
if (token == XContentParser.Token.FIELD_NAME) {
|
PARSER.declareObjectArray(ConstructingObjectParser.optionalConstructorArg(), (p, c) -> SearchHit.fromXContent(p),
|
||||||
currentFieldName = parser.currentName();
|
new ParseField(Fields.EVENTS));
|
||||||
} else if (token.isValue()) {
|
PARSER.declareObjectArray(ConstructingObjectParser.optionalConstructorArg(), Sequence.PARSER,
|
||||||
if (Fields.TOTAL.equals(currentFieldName)) {
|
new ParseField(Fields.SEQUENCES));
|
||||||
totalHits = new TotalHits(parser.longValue(), TotalHits.Relation.EQUAL_TO);
|
PARSER.declareObjectArray(ConstructingObjectParser.optionalConstructorArg(), Count.PARSER,
|
||||||
}
|
new ParseField(Fields.COUNTS));
|
||||||
} else if (token == XContentParser.Token.START_ARRAY) {
|
PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), (p, c) -> SearchHits.parseTotalHitsFragment(p),
|
||||||
if (Fields.EVENTS.equals(currentFieldName)) {
|
new ParseField(Fields.TOTAL));
|
||||||
searchHits = new ArrayList<>();
|
}
|
||||||
while (parser.nextToken() != XContentParser.Token.END_ARRAY) {
|
|
||||||
searchHits.add(SearchHit.fromXContent(parser));
|
public static Hits fromXContent(XContentParser parser) throws IOException {
|
||||||
}
|
return PARSER.parse(parser, null);
|
||||||
} 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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in New Issue