Bulk: throw exception if unrecognized parameter in action/metadata line
Closes #10977
This commit is contained in:
parent
9d5e789508
commit
acb07c72b9
|
@ -246,6 +246,7 @@ public class BulkRequest extends ActionRequest<BulkRequest> implements Composite
|
||||||
|
|
||||||
public BulkRequest add(BytesReference data, @Nullable String defaultIndex, @Nullable String defaultType, @Nullable String defaultRouting, @Nullable Object payload, boolean allowExplicitIndex) throws Exception {
|
public BulkRequest add(BytesReference data, @Nullable String defaultIndex, @Nullable String defaultType, @Nullable String defaultRouting, @Nullable Object payload, boolean allowExplicitIndex) throws Exception {
|
||||||
XContent xContent = XContentFactory.xContent(data);
|
XContent xContent = XContentFactory.xContent(data);
|
||||||
|
int line = 0;
|
||||||
int from = 0;
|
int from = 0;
|
||||||
int length = data.length();
|
int length = data.length();
|
||||||
byte marker = xContent.streamSeparator();
|
byte marker = xContent.streamSeparator();
|
||||||
|
@ -254,8 +255,9 @@ public class BulkRequest extends ActionRequest<BulkRequest> implements Composite
|
||||||
if (nextMarker == -1) {
|
if (nextMarker == -1) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
// now parse the action
|
line++;
|
||||||
|
|
||||||
|
// now parse the action
|
||||||
try (XContentParser parser = xContent.createParser(data.slice(from, nextMarker - from))) {
|
try (XContentParser parser = xContent.createParser(data.slice(from, nextMarker - from))) {
|
||||||
// move pointers
|
// move pointers
|
||||||
from = nextMarker + 1;
|
from = nextMarker + 1;
|
||||||
|
@ -285,43 +287,53 @@ public class BulkRequest extends ActionRequest<BulkRequest> implements Composite
|
||||||
|
|
||||||
// at this stage, next token can either be END_OBJECT (and use default index and type, with auto generated id)
|
// at this stage, next token can either be END_OBJECT (and use default index and type, with auto generated id)
|
||||||
// or START_OBJECT which will have another set of parameters
|
// or START_OBJECT which will have another set of parameters
|
||||||
|
token = parser.nextToken();
|
||||||
|
|
||||||
String currentFieldName = null;
|
if (token == XContentParser.Token.START_OBJECT) {
|
||||||
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
|
String currentFieldName = null;
|
||||||
if (token == XContentParser.Token.FIELD_NAME) {
|
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
|
||||||
currentFieldName = parser.currentName();
|
if (token == XContentParser.Token.FIELD_NAME) {
|
||||||
} else if (token.isValue()) {
|
currentFieldName = parser.currentName();
|
||||||
if ("_index".equals(currentFieldName)) {
|
} else if (token.isValue()) {
|
||||||
if (!allowExplicitIndex) {
|
if ("_index".equals(currentFieldName)) {
|
||||||
throw new IllegalArgumentException("explicit index in bulk is not allowed");
|
if (!allowExplicitIndex) {
|
||||||
}
|
throw new IllegalArgumentException("explicit index in bulk is not allowed");
|
||||||
index = parser.text();
|
}
|
||||||
} else if ("_type".equals(currentFieldName)) {
|
index = parser.text();
|
||||||
type = parser.text();
|
} else if ("_type".equals(currentFieldName)) {
|
||||||
} else if ("_id".equals(currentFieldName)) {
|
type = parser.text();
|
||||||
id = parser.text();
|
} else if ("_id".equals(currentFieldName)) {
|
||||||
} else if ("_routing".equals(currentFieldName) || "routing".equals(currentFieldName)) {
|
id = parser.text();
|
||||||
routing = parser.text();
|
} else if ("_routing".equals(currentFieldName) || "routing".equals(currentFieldName)) {
|
||||||
} else if ("_parent".equals(currentFieldName) || "parent".equals(currentFieldName)) {
|
routing = parser.text();
|
||||||
parent = parser.text();
|
} else if ("_parent".equals(currentFieldName) || "parent".equals(currentFieldName)) {
|
||||||
} else if ("_timestamp".equals(currentFieldName) || "timestamp".equals(currentFieldName)) {
|
parent = parser.text();
|
||||||
timestamp = parser.text();
|
} else if ("_timestamp".equals(currentFieldName) || "timestamp".equals(currentFieldName)) {
|
||||||
} else if ("_ttl".equals(currentFieldName) || "ttl".equals(currentFieldName)) {
|
timestamp = parser.text();
|
||||||
if (parser.currentToken() == XContentParser.Token.VALUE_STRING) {
|
} else if ("_ttl".equals(currentFieldName) || "ttl".equals(currentFieldName)) {
|
||||||
ttl = TimeValue.parseTimeValue(parser.text(), null).millis();
|
if (parser.currentToken() == XContentParser.Token.VALUE_STRING) {
|
||||||
|
ttl = TimeValue.parseTimeValue(parser.text(), null).millis();
|
||||||
|
} else {
|
||||||
|
ttl = parser.longValue();
|
||||||
|
}
|
||||||
|
} else if ("op_type".equals(currentFieldName) || "opType".equals(currentFieldName)) {
|
||||||
|
opType = parser.text();
|
||||||
|
} else if ("_version".equals(currentFieldName) || "version".equals(currentFieldName)) {
|
||||||
|
version = parser.longValue();
|
||||||
|
} else if ("_version_type".equals(currentFieldName) || "_versionType".equals(currentFieldName) || "version_type".equals(currentFieldName) || "versionType".equals(currentFieldName)) {
|
||||||
|
versionType = VersionType.fromString(parser.text());
|
||||||
|
} else if ("_retry_on_conflict".equals(currentFieldName) || "_retryOnConflict".equals(currentFieldName)) {
|
||||||
|
retryOnConflict = parser.intValue();
|
||||||
} else {
|
} else {
|
||||||
ttl = parser.longValue();
|
throw new IllegalArgumentException("Action/metadata line [" + line + "] contains an unknown parameter [" + currentFieldName + "]");
|
||||||
}
|
}
|
||||||
} else if ("op_type".equals(currentFieldName) || "opType".equals(currentFieldName)) {
|
} else {
|
||||||
opType = parser.text();
|
throw new IllegalArgumentException("Malformed action/metadata line [" + line + "], expected a simple value for field [" + currentFieldName + "] but found [" + token + "]");
|
||||||
} else if ("_version".equals(currentFieldName) || "version".equals(currentFieldName)) {
|
|
||||||
version = parser.longValue();
|
|
||||||
} else if ("_version_type".equals(currentFieldName) || "_versionType".equals(currentFieldName) || "version_type".equals(currentFieldName) || "versionType".equals(currentFieldName)) {
|
|
||||||
versionType = VersionType.fromString(parser.text());
|
|
||||||
} else if ("_retry_on_conflict".equals(currentFieldName) || "_retryOnConflict".equals(currentFieldName)) {
|
|
||||||
retryOnConflict = parser.intValue();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else if (token != XContentParser.Token.END_OBJECT) {
|
||||||
|
throw new IllegalArgumentException("Malformed action/metadata line [" + line + "], expected " + XContentParser.Token.START_OBJECT
|
||||||
|
+ " or " + XContentParser.Token.END_OBJECT + " but found [" + token + "]");
|
||||||
}
|
}
|
||||||
|
|
||||||
if ("delete".equals(action)) {
|
if ("delete".equals(action)) {
|
||||||
|
@ -331,6 +343,8 @@ public class BulkRequest extends ActionRequest<BulkRequest> implements Composite
|
||||||
if (nextMarker == -1) {
|
if (nextMarker == -1) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
line++;
|
||||||
|
|
||||||
// order is important, we set parent after routing, so routing will be set to parent if not set explicitly
|
// order is important, we set parent after routing, so routing will be set to parent if not set explicitly
|
||||||
// we use internalAdd so we don't fork here, this allows us not to copy over the big byte array to small chunks
|
// we use internalAdd so we don't fork here, this allows us not to copy over the big byte array to small chunks
|
||||||
// of index request.
|
// of index request.
|
||||||
|
|
|
@ -117,4 +117,56 @@ public class BulkRequestTests extends ElasticsearchTestCase {
|
||||||
assertThat(bulkRequest.requests().get(1), instanceOf(UpdateRequest.class));
|
assertThat(bulkRequest.requests().get(1), instanceOf(UpdateRequest.class));
|
||||||
assertThat(bulkRequest.requests().get(2), instanceOf(DeleteRequest.class));
|
assertThat(bulkRequest.requests().get(2), instanceOf(DeleteRequest.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSimpleBulk6() throws Exception {
|
||||||
|
String bulkAction = copyToStringFromClasspath("/org/elasticsearch/action/bulk/simple-bulk6.json");
|
||||||
|
BulkRequest bulkRequest = new BulkRequest();
|
||||||
|
try {
|
||||||
|
bulkRequest.add(bulkAction.getBytes(Charsets.UTF_8), 0, bulkAction.length(), null, null);
|
||||||
|
fail("should have thrown an exception about the wrong format of line 1");
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
assertThat("message contains error about the wrong format of line 1: " + e.getMessage(),
|
||||||
|
e.getMessage().contains("Malformed action/metadata line [1], expected a simple value for field [_source] but found [START_OBJECT]"), equalTo(true));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSimpleBulk7() throws Exception {
|
||||||
|
String bulkAction = copyToStringFromClasspath("/org/elasticsearch/action/bulk/simple-bulk7.json");
|
||||||
|
BulkRequest bulkRequest = new BulkRequest();
|
||||||
|
try {
|
||||||
|
bulkRequest.add(bulkAction.getBytes(Charsets.UTF_8), 0, bulkAction.length(), null, null);
|
||||||
|
fail("should have thrown an exception about the wrong format of line 5");
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
assertThat("message contains error about the wrong format of line 5: " + e.getMessage(),
|
||||||
|
e.getMessage().contains("Malformed action/metadata line [5], expected a simple value for field [_unkown] but found [START_ARRAY]"), equalTo(true));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSimpleBulk8() throws Exception {
|
||||||
|
String bulkAction = copyToStringFromClasspath("/org/elasticsearch/action/bulk/simple-bulk8.json");
|
||||||
|
BulkRequest bulkRequest = new BulkRequest();
|
||||||
|
try {
|
||||||
|
bulkRequest.add(bulkAction.getBytes(Charsets.UTF_8), 0, bulkAction.length(), null, null);
|
||||||
|
fail("should have thrown an exception about the unknown paramater _foo");
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
assertThat("message contains error about the unknown paramater _foo: " + e.getMessage(),
|
||||||
|
e.getMessage().contains("Action/metadata line [3] contains an unknown parameter [_foo]"), equalTo(true));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSimpleBulk9() throws Exception {
|
||||||
|
String bulkAction = copyToStringFromClasspath("/org/elasticsearch/action/bulk/simple-bulk9.json");
|
||||||
|
BulkRequest bulkRequest = new BulkRequest();
|
||||||
|
try {
|
||||||
|
bulkRequest.add(bulkAction.getBytes(Charsets.UTF_8), 0, bulkAction.length(), null, null);
|
||||||
|
fail("should have thrown an exception about the wrong format of line 3");
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
assertThat("message contains error about the wrong format of line 3: " + e.getMessage(),
|
||||||
|
e.getMessage().contains("Malformed action/metadata line [3], expected START_OBJECT or END_OBJECT but found [START_ARRAY]"), equalTo(true));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,6 @@
|
||||||
|
{"index": {"_index": "test", "_type": "doc", "_source": {"hello": "world"}, "_id": 0}}
|
||||||
|
{"field1": "value0"}
|
||||||
|
{"index": {"_index": "test", "_type": "doc", "_id": 1}}
|
||||||
|
{"field1": "value1"}
|
||||||
|
{"index": {"_index": "test", "_type": "doc", "_id": 2}}
|
||||||
|
{"field1": "value2"}
|
|
@ -0,0 +1,6 @@
|
||||||
|
{"index": {"_index": "test", "_type": "doc", "_id": 0}}
|
||||||
|
{"field1": "value0"}
|
||||||
|
{"index": {"_index": "test", "_type": "doc", "_id": 1}}
|
||||||
|
{"field1": "value1"}
|
||||||
|
{"index": {"_index": "test", "_type": "doc", "_id": 2, "_unkown": ["foo", "bar"]}}
|
||||||
|
{"field1": "value2"}
|
|
@ -0,0 +1,6 @@
|
||||||
|
{"index": {"_index": "test", "_type": "doc", "_id": 0}}
|
||||||
|
{"field1": "value0"}
|
||||||
|
{"index": {"_index": "test", "_type": "doc", "_id": 1, "_foo": "bar"}}
|
||||||
|
{"field1": "value1"}
|
||||||
|
{"index": {"_index": "test", "_type": "doc", "_id": 2}}
|
||||||
|
{"field1": "value2"}
|
|
@ -0,0 +1,4 @@
|
||||||
|
{"index": {}}
|
||||||
|
{"field1": "value0"}
|
||||||
|
{"index": ["bar"] }
|
||||||
|
{"field1": "value1"}
|
Loading…
Reference in New Issue