Skip to content

Commit f318f79

Browse files
authored
Accept more ingest simulate params as ints or strings (#66197)
_version, _if_seq_no and _if_primary_term params in the simulate pipeline API cannot be parsed correctly when the value is integer orstring type. This PR fix the bug and modify the test method to test it.
1 parent 6dfdacd commit f318f79

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

server/src/main/java/org/elasticsearch/action/ingest/SimulatePipelineRequest.java

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,13 @@ private static List<IngestDocument> parseDocs(Map<String, Object> config) {
186186
dataMap, Metadata.ROUTING.getFieldName());
187187
Long version = null;
188188
if (dataMap.containsKey(Metadata.VERSION.getFieldName())) {
189-
version = (Long) ConfigurationUtils.readObject(null, null, dataMap, Metadata.VERSION.getFieldName());
189+
String versionValue = ConfigurationUtils.readOptionalStringOrIntProperty(null, null,
190+
dataMap, Metadata.VERSION.getFieldName());
191+
if (versionValue != null) {
192+
version = Long.valueOf(versionValue);
193+
} else {
194+
throw new IllegalArgumentException("[_version] cannot be null");
195+
}
190196
}
191197
VersionType versionType = null;
192198
if (dataMap.containsKey(Metadata.VERSION_TYPE.getFieldName())) {
@@ -196,12 +202,24 @@ private static List<IngestDocument> parseDocs(Map<String, Object> config) {
196202
IngestDocument ingestDocument =
197203
new IngestDocument(index, id, routing, version, versionType, document);
198204
if (dataMap.containsKey(Metadata.IF_SEQ_NO.getFieldName())) {
199-
Long ifSeqNo = (Long) ConfigurationUtils.readObject(null, null, dataMap, Metadata.IF_SEQ_NO.getFieldName());
200-
ingestDocument.setFieldValue(Metadata.IF_SEQ_NO.getFieldName(), ifSeqNo);
205+
String ifSeqNoValue = ConfigurationUtils.readOptionalStringOrIntProperty(null, null,
206+
dataMap, Metadata.IF_SEQ_NO.getFieldName());
207+
if (ifSeqNoValue != null) {
208+
Long ifSeqNo = Long.valueOf(ifSeqNoValue);
209+
ingestDocument.setFieldValue(Metadata.IF_SEQ_NO.getFieldName(), ifSeqNo);
210+
} else {
211+
throw new IllegalArgumentException("[_if_seq_no] cannot be null");
212+
}
201213
}
202214
if (dataMap.containsKey(Metadata.IF_PRIMARY_TERM.getFieldName())) {
203-
Long ifPrimaryTerm = (Long) ConfigurationUtils.readObject(null, null, dataMap, Metadata.IF_PRIMARY_TERM.getFieldName());
204-
ingestDocument.setFieldValue(Metadata.IF_PRIMARY_TERM.getFieldName(), ifPrimaryTerm);
215+
String ifPrimaryTermValue = ConfigurationUtils.readOptionalStringOrIntProperty(null, null,
216+
dataMap, Metadata.IF_PRIMARY_TERM.getFieldName());
217+
if (ifPrimaryTermValue != null) {
218+
Long ifPrimaryTerm = Long.valueOf(ifPrimaryTermValue);
219+
ingestDocument.setFieldValue(Metadata.IF_PRIMARY_TERM.getFieldName(), ifPrimaryTerm);
220+
} else {
221+
throw new IllegalArgumentException("[_if_primary_term] cannot be null");
222+
}
205223
}
206224
ingestDocumentList.add(ingestDocument);
207225
}

server/src/test/java/org/elasticsearch/action/ingest/SimulatePipelineRequestParsingTests.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -124,20 +124,12 @@ public void innerTestParseWithProvidedPipeline() throws Exception {
124124
Map<String, Object> expectedDoc = new HashMap<>();
125125
List<IngestDocument.Metadata> fields = Arrays.asList(INDEX, ID, ROUTING, VERSION, VERSION_TYPE, IF_SEQ_NO, IF_PRIMARY_TERM);
126126
for(IngestDocument.Metadata field : fields) {
127-
if (field == VERSION) {
128-
Long value = randomLong();
129-
doc.put(field.getFieldName(), value);
130-
expectedDoc.put(field.getFieldName(), value);
131-
} else if (field == VERSION_TYPE) {
127+
if (field == VERSION_TYPE) {
132128
String value = VersionType.toString(
133129
randomFrom(VersionType.INTERNAL, VersionType.EXTERNAL, VersionType.EXTERNAL_GTE)
134130
);
135131
doc.put(field.getFieldName(), value);
136132
expectedDoc.put(field.getFieldName(), value);
137-
} else if (field == IF_SEQ_NO || field == IF_PRIMARY_TERM) {
138-
Long value = randomNonNegativeLong();
139-
doc.put(field.getFieldName(), value);
140-
expectedDoc.put(field.getFieldName(), value);
141133
} else {
142134
if (randomBoolean()) {
143135
String value = randomAlphaOfLengthBetween(1, 10);

0 commit comments

Comments
 (0)