use of org.opensearch.OpenSearchParseException in project OpenSearch by opensearch-project.
the class MultiGetRequest method parseDocuments.
private static void parseDocuments(XContentParser parser, List<Item> items, @Nullable String defaultIndex, @Nullable String[] defaultFields, @Nullable FetchSourceContext defaultFetchSource, @Nullable String defaultRouting, boolean allowExplicitIndex) throws IOException {
String currentFieldName = null;
Token token;
while ((token = parser.nextToken()) != Token.END_ARRAY) {
if (token != Token.START_OBJECT) {
throw new IllegalArgumentException("docs array element should include an object");
}
String index = defaultIndex;
String id = null;
String routing = defaultRouting;
List<String> storedFields = null;
long version = Versions.MATCH_ANY;
VersionType versionType = VersionType.INTERNAL;
FetchSourceContext fetchSourceContext = FetchSourceContext.FETCH_SOURCE;
while ((token = parser.nextToken()) != Token.END_OBJECT) {
if (token == Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else if (token.isValue()) {
if (INDEX.match(currentFieldName, parser.getDeprecationHandler())) {
if (!allowExplicitIndex) {
throw new IllegalArgumentException("explicit index in multi get is not allowed");
}
index = parser.text();
} else if (ID.match(currentFieldName, parser.getDeprecationHandler())) {
id = parser.text();
} else if (ROUTING.match(currentFieldName, parser.getDeprecationHandler())) {
routing = parser.text();
} else if (FIELDS.match(currentFieldName, parser.getDeprecationHandler())) {
throw new ParsingException(parser.getTokenLocation(), "Unsupported field [fields] used, expected [stored_fields] instead");
} else if (STORED_FIELDS.match(currentFieldName, parser.getDeprecationHandler())) {
storedFields = new ArrayList<>();
storedFields.add(parser.text());
} else if (VERSION.match(currentFieldName, parser.getDeprecationHandler())) {
version = parser.longValue();
} else if (VERSION_TYPE.match(currentFieldName, parser.getDeprecationHandler())) {
versionType = VersionType.fromString(parser.text());
} else if (SOURCE.match(currentFieldName, parser.getDeprecationHandler())) {
if (parser.isBooleanValue()) {
fetchSourceContext = new FetchSourceContext(parser.booleanValue(), fetchSourceContext.includes(), fetchSourceContext.excludes());
} else if (token == Token.VALUE_STRING) {
fetchSourceContext = new FetchSourceContext(fetchSourceContext.fetchSource(), new String[] { parser.text() }, fetchSourceContext.excludes());
} else {
throw new OpenSearchParseException("illegal type for _source: [{}]", token);
}
} else {
throw new OpenSearchParseException("failed to parse multi get request. unknown field [{}]", currentFieldName);
}
} else if (token == Token.START_ARRAY) {
if (FIELDS.match(currentFieldName, parser.getDeprecationHandler())) {
throw new ParsingException(parser.getTokenLocation(), "Unsupported field [fields] used, expected [stored_fields] instead");
} else if (STORED_FIELDS.match(currentFieldName, parser.getDeprecationHandler())) {
storedFields = new ArrayList<>();
while ((token = parser.nextToken()) != Token.END_ARRAY) {
storedFields.add(parser.text());
}
} else if (SOURCE.match(currentFieldName, parser.getDeprecationHandler())) {
ArrayList<String> includes = new ArrayList<>();
while ((token = parser.nextToken()) != Token.END_ARRAY) {
includes.add(parser.text());
}
fetchSourceContext = new FetchSourceContext(fetchSourceContext.fetchSource(), includes.toArray(Strings.EMPTY_ARRAY), fetchSourceContext.excludes());
}
} else if (token == Token.START_OBJECT) {
if (SOURCE.match(currentFieldName, parser.getDeprecationHandler())) {
List<String> currentList = null, includes = null, excludes = null;
while ((token = parser.nextToken()) != Token.END_OBJECT) {
if (token == Token.FIELD_NAME) {
currentFieldName = parser.currentName();
if ("includes".equals(currentFieldName) || "include".equals(currentFieldName)) {
currentList = includes != null ? includes : (includes = new ArrayList<>(2));
} else if ("excludes".equals(currentFieldName) || "exclude".equals(currentFieldName)) {
currentList = excludes != null ? excludes : (excludes = new ArrayList<>(2));
} else {
throw new OpenSearchParseException("source definition may not contain [{}]", parser.text());
}
} else if (token == Token.START_ARRAY) {
while ((token = parser.nextToken()) != Token.END_ARRAY) {
currentList.add(parser.text());
}
} else if (token.isValue()) {
currentList.add(parser.text());
} else {
throw new OpenSearchParseException("unexpected token while parsing source settings");
}
}
fetchSourceContext = new FetchSourceContext(fetchSourceContext.fetchSource(), includes == null ? Strings.EMPTY_ARRAY : includes.toArray(new String[includes.size()]), excludes == null ? Strings.EMPTY_ARRAY : excludes.toArray(new String[excludes.size()]));
}
}
}
String[] aFields;
if (storedFields != null) {
aFields = storedFields.toArray(new String[storedFields.size()]);
} else {
aFields = defaultFields;
}
items.add(new Item(index, id).routing(routing).storedFields(aFields).version(version).versionType(versionType).fetchSourceContext(fetchSourceContext == FetchSourceContext.FETCH_SOURCE ? defaultFetchSource : fetchSourceContext));
}
}
use of org.opensearch.OpenSearchParseException in project OpenSearch by opensearch-project.
the class RepositoriesMetadata method fromXContent.
public static RepositoriesMetadata fromXContent(XContentParser parser) throws IOException {
XContentParser.Token token;
List<RepositoryMetadata> repository = new ArrayList<>();
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
String name = parser.currentName();
if (parser.nextToken() != XContentParser.Token.START_OBJECT) {
throw new OpenSearchParseException("failed to parse repository [{}], expected object", name);
}
String type = null;
Settings settings = Settings.EMPTY;
long generation = RepositoryData.UNKNOWN_REPO_GEN;
long pendingGeneration = RepositoryData.EMPTY_REPO_GEN;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
String currentFieldName = parser.currentName();
if ("type".equals(currentFieldName)) {
if (parser.nextToken() != XContentParser.Token.VALUE_STRING) {
throw new OpenSearchParseException("failed to parse repository [{}], unknown type", name);
}
type = parser.text();
} else if ("settings".equals(currentFieldName)) {
if (parser.nextToken() != XContentParser.Token.START_OBJECT) {
throw new OpenSearchParseException("failed to parse repository [{}], incompatible params", name);
}
settings = Settings.fromXContent(parser);
} else if ("generation".equals(currentFieldName)) {
if (parser.nextToken() != XContentParser.Token.VALUE_NUMBER) {
throw new OpenSearchParseException("failed to parse repository [{}], unknown type", name);
}
generation = parser.longValue();
} else if ("pending_generation".equals(currentFieldName)) {
if (parser.nextToken() != XContentParser.Token.VALUE_NUMBER) {
throw new OpenSearchParseException("failed to parse repository [{}], unknown type", name);
}
pendingGeneration = parser.longValue();
} else {
throw new OpenSearchParseException("failed to parse repository [{}], unknown field [{}]", name, currentFieldName);
}
} else {
throw new OpenSearchParseException("failed to parse repository [{}]", name);
}
}
if (type == null) {
throw new OpenSearchParseException("failed to parse repository [{}], missing repository type", name);
}
repository.add(new RepositoryMetadata(name, type, settings, generation, pendingGeneration));
} else {
throw new OpenSearchParseException("failed to parse repositories");
}
}
return new RepositoriesMetadata(repository);
}
use of org.opensearch.OpenSearchParseException in project OpenSearch by opensearch-project.
the class DateFieldMapper method parseCreateField.
@Override
protected void parseCreateField(ParseContext context) throws IOException {
String dateAsString;
if (context.externalValueSet()) {
Object dateAsObject = context.externalValue();
if (dateAsObject == null) {
dateAsString = null;
} else {
dateAsString = dateAsObject.toString();
}
} else {
dateAsString = context.parser().textOrNull();
}
long timestamp;
if (dateAsString == null) {
if (nullValue == null) {
return;
}
timestamp = nullValue;
} else {
try {
timestamp = fieldType().parse(dateAsString);
} catch (IllegalArgumentException | OpenSearchParseException | DateTimeException | ArithmeticException e) {
if (ignoreMalformed) {
context.addIgnoredField(mappedFieldType.name());
return;
} else {
throw e;
}
}
}
if (indexed) {
context.doc().add(new LongPoint(fieldType().name(), timestamp));
}
if (hasDocValues) {
context.doc().add(new SortedNumericDocValuesField(fieldType().name(), timestamp));
} else if (store || indexed) {
createFieldNamesField(context);
}
if (store) {
context.doc().add(new StoredField(fieldType().name(), timestamp));
}
}
use of org.opensearch.OpenSearchParseException in project OpenSearch by opensearch-project.
the class RangeFieldTypeTests method testDateRangeQueryUsingMappingFormat.
public void testDateRangeQueryUsingMappingFormat() {
QueryShardContext context = createContext();
RangeFieldType strict = new RangeFieldType("field", RangeFieldMapper.Defaults.DATE_FORMATTER);
// don't use DISJOINT here because it doesn't work on date fields which we want to compare bounds with
ShapeRelation relation = randomValueOtherThan(ShapeRelation.DISJOINT, () -> randomFrom(ShapeRelation.values()));
// dates will break the default format, month/day of month is turned around in the format
final String from = "2016-15-06T15:29:50+08:00";
final String to = "2016-16-06T15:29:50+08:00";
OpenSearchParseException ex = expectThrows(OpenSearchParseException.class, () -> strict.rangeQuery(from, to, true, true, relation, null, null, context));
assertThat(ex.getMessage(), containsString("failed to parse date field [2016-15-06T15:29:50+08:00] with format [strict_date_optional_time||epoch_millis]"));
// setting mapping format which is compatible with those dates
final DateFormatter formatter = DateFormatter.forPattern("yyyy-dd-MM'T'HH:mm:ssZZZZZ");
assertEquals(1465975790000L, formatter.parseMillis(from));
assertEquals(1466062190000L, formatter.parseMillis(to));
RangeFieldType fieldType = new RangeFieldType("field", formatter);
final Query query = fieldType.rangeQuery(from, to, true, true, relation, null, fieldType.dateMathParser(), context);
assertEquals("field:<ranges:[1465975790000 : 1466062190999]>", query.toString());
// compare lower and upper bounds with what we would get on a `date` field
DateFieldType dateFieldType = new DateFieldType("field", DateFieldMapper.Resolution.MILLISECONDS, formatter);
final Query queryOnDateField = dateFieldType.rangeQuery(from, to, true, true, relation, null, fieldType.dateMathParser(), context);
assertEquals("field:[1465975790000 TO 1466062190999]", queryOnDateField.toString());
}
use of org.opensearch.OpenSearchParseException in project OpenSearch by opensearch-project.
the class FileInfoTests method testInvalidFieldsInFromXContent.
public void testInvalidFieldsInFromXContent() throws IOException {
final int iters = scaledRandomIntBetween(1, 10);
for (int iter = 0; iter < iters; iter++) {
final BytesRef hash = new BytesRef(scaledRandomIntBetween(0, 1024 * 1024));
hash.length = hash.bytes.length;
for (int i = 0; i < hash.length; i++) {
hash.bytes[i] = randomByte();
}
String name = "foobar";
String physicalName = "_foobar";
String failure = null;
long length = Math.max(0, Math.abs(randomLong()));
// random corruption
switch(randomIntBetween(0, 3)) {
case 0:
name = "foo,bar";
failure = "missing or invalid file name";
break;
case 1:
physicalName = "_foo,bar";
failure = "missing or invalid physical file name";
break;
case 2:
length = -Math.abs(randomLong());
failure = "missing or invalid file length";
break;
case 3:
break;
default:
fail("shouldn't be here");
}
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
builder.startObject();
builder.field(FileInfo.NAME, name);
builder.field(FileInfo.PHYSICAL_NAME, physicalName);
builder.field(FileInfo.LENGTH, length);
builder.field(FileInfo.WRITTEN_BY, Version.LATEST.toString());
builder.field(FileInfo.CHECKSUM, "666");
builder.endObject();
byte[] xContent = BytesReference.toBytes(BytesReference.bytes(builder));
if (failure == null) {
// No failures should read as usual
final BlobStoreIndexShardSnapshot.FileInfo parsedInfo;
try (XContentParser parser = createParser(JsonXContent.jsonXContent, xContent)) {
parser.nextToken();
parsedInfo = BlobStoreIndexShardSnapshot.FileInfo.fromXContent(parser);
}
assertThat(name, equalTo(parsedInfo.name()));
assertThat(physicalName, equalTo(parsedInfo.physicalName()));
assertThat(length, equalTo(parsedInfo.length()));
assertEquals("666", parsedInfo.checksum());
assertEquals("666", parsedInfo.metadata().checksum());
assertEquals(Version.LATEST, parsedInfo.metadata().writtenBy());
} else {
try (XContentParser parser = createParser(JsonXContent.jsonXContent, xContent)) {
parser.nextToken();
BlobStoreIndexShardSnapshot.FileInfo.fromXContent(parser);
fail("Should have failed with [" + failure + "]");
} catch (OpenSearchParseException ex) {
assertThat(ex.getMessage(), containsString(failure));
}
}
}
}
Aggregations