use of org.opensearch.common.xcontent.XContentParser 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));
}
}
}
}
use of org.opensearch.common.xcontent.XContentParser in project OpenSearch by opensearch-project.
the class SpanWithinQueryBuilder method fromXContent.
public static SpanWithinQueryBuilder fromXContent(XContentParser parser) throws IOException {
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
String queryName = null;
SpanQueryBuilder big = null;
SpanQueryBuilder little = null;
String currentFieldName = null;
XContentParser.Token token;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else if (token == XContentParser.Token.START_OBJECT) {
if (BIG_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
QueryBuilder query = parseInnerQueryBuilder(parser);
if (query instanceof SpanQueryBuilder == false) {
throw new ParsingException(parser.getTokenLocation(), "span_within [big] must be of type span query");
}
big = (SpanQueryBuilder) query;
checkNoBoost(NAME, currentFieldName, parser, big);
} else if (LITTLE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
QueryBuilder query = parseInnerQueryBuilder(parser);
if (query instanceof SpanQueryBuilder == false) {
throw new ParsingException(parser.getTokenLocation(), "span_within [little] must be of type span query");
}
little = (SpanQueryBuilder) query;
checkNoBoost(NAME, currentFieldName, parser, little);
} else {
throw new ParsingException(parser.getTokenLocation(), "[span_within] query does not support [" + currentFieldName + "]");
}
} else if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
boost = parser.floatValue();
} else if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
queryName = parser.text();
} else {
throw new ParsingException(parser.getTokenLocation(), "[span_within] query does not support [" + currentFieldName + "]");
}
}
if (big == null) {
throw new ParsingException(parser.getTokenLocation(), "span_within must include [big]");
}
if (little == null) {
throw new ParsingException(parser.getTokenLocation(), "span_within must include [little]");
}
SpanWithinQueryBuilder query = new SpanWithinQueryBuilder(big, little);
query.boost(boost).queryName(queryName);
return query;
}
use of org.opensearch.common.xcontent.XContentParser in project OpenSearch by opensearch-project.
the class TypeQueryBuilder method fromXContent.
public static TypeQueryBuilder fromXContent(XContentParser parser) throws IOException {
String type = null;
String queryName = null;
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
String currentFieldName = null;
XContentParser.Token token;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else if (token.isValue()) {
if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
queryName = parser.text();
} else if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
boost = parser.floatValue();
} else if (VALUE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
type = parser.text();
} else {
throw new ParsingException(parser.getTokenLocation(), "[" + TypeQueryBuilder.NAME + "] filter doesn't support [" + currentFieldName + "]");
}
} else {
throw new ParsingException(parser.getTokenLocation(), "[" + TypeQueryBuilder.NAME + "] filter doesn't support [" + currentFieldName + "]");
}
}
if (type == null) {
throw new ParsingException(parser.getTokenLocation(), "[" + TypeQueryBuilder.NAME + "] filter needs to be provided with a value for the type");
}
return new TypeQueryBuilder(type).boost(boost).queryName(queryName);
}
use of org.opensearch.common.xcontent.XContentParser in project OpenSearch by opensearch-project.
the class MatchNoneQueryBuilder method fromXContent.
public static MatchNoneQueryBuilder fromXContent(XContentParser parser) throws IOException {
String currentFieldName = null;
XContentParser.Token token;
String queryName = null;
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
while (((token = parser.nextToken()) != XContentParser.Token.END_OBJECT && token != XContentParser.Token.END_ARRAY)) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else if (token.isValue()) {
if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
queryName = parser.text();
} else if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
boost = parser.floatValue();
} else {
throw new ParsingException(parser.getTokenLocation(), "[" + MatchNoneQueryBuilder.NAME + "] query does not support [" + currentFieldName + "]");
}
} else {
throw new ParsingException(parser.getTokenLocation(), "[" + MatchNoneQueryBuilder.NAME + "] unknown token [" + token + "] after [" + currentFieldName + "]");
}
}
MatchNoneQueryBuilder matchNoneQueryBuilder = new MatchNoneQueryBuilder();
matchNoneQueryBuilder.boost(boost);
matchNoneQueryBuilder.queryName(queryName);
return matchNoneQueryBuilder;
}
use of org.opensearch.common.xcontent.XContentParser in project OpenSearch by opensearch-project.
the class MatchPhraseQueryBuilder method fromXContent.
public static MatchPhraseQueryBuilder fromXContent(XContentParser parser) throws IOException {
String fieldName = null;
Object value = null;
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
String analyzer = null;
int slop = MatchQuery.DEFAULT_PHRASE_SLOP;
ZeroTermsQuery zeroTermsQuery = MatchQuery.DEFAULT_ZERO_TERMS_QUERY;
String queryName = null;
String currentFieldName = null;
XContentParser.Token token;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else if (token == XContentParser.Token.START_OBJECT) {
throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName, currentFieldName);
fieldName = currentFieldName;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else if (token.isValue()) {
if (MatchQueryBuilder.QUERY_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
value = parser.objectText();
} else if (MatchQueryBuilder.ANALYZER_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
analyzer = parser.text();
} else if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
boost = parser.floatValue();
} else if (SLOP_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
slop = parser.intValue();
} else if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
queryName = parser.text();
} else if (ZERO_TERMS_QUERY_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
String zeroTermsValue = parser.text();
if ("none".equalsIgnoreCase(zeroTermsValue)) {
zeroTermsQuery = ZeroTermsQuery.NONE;
} else if ("all".equalsIgnoreCase(zeroTermsValue)) {
zeroTermsQuery = ZeroTermsQuery.ALL;
} else {
throw new ParsingException(parser.getTokenLocation(), "Unsupported zero_terms_query value [" + zeroTermsValue + "]");
}
} else {
throw new ParsingException(parser.getTokenLocation(), "[" + NAME + "] query does not support [" + currentFieldName + "]");
}
} else {
throw new ParsingException(parser.getTokenLocation(), "[" + NAME + "] unknown token [" + token + "] after [" + currentFieldName + "]");
}
}
} else {
throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName, parser.currentName());
fieldName = parser.currentName();
value = parser.objectText();
}
}
MatchPhraseQueryBuilder matchQuery = new MatchPhraseQueryBuilder(fieldName, value);
matchQuery.analyzer(analyzer);
matchQuery.slop(slop);
matchQuery.zeroTermsQuery(zeroTermsQuery);
matchQuery.queryName(queryName);
matchQuery.boost(boost);
return matchQuery;
}
Aggregations