use of org.elasticsearch.common.xcontent.XContentParser in project elasticsearch by elastic.
the class ContextMapping method parseQueryContext.
/**
* Parses query contexts for this mapper
*/
public final List<InternalQueryContext> parseQueryContext(QueryParseContext context) throws IOException, ElasticsearchParseException {
List<T> queryContexts = new ArrayList<>();
XContentParser parser = context.parser();
Token token = parser.nextToken();
if (token == Token.START_OBJECT || token == Token.VALUE_STRING) {
queryContexts.add(fromXContent(context));
} else if (token == Token.START_ARRAY) {
while (parser.nextToken() != Token.END_ARRAY) {
queryContexts.add(fromXContent(context));
}
}
return toInternalQueryContexts(queryContexts);
}
use of org.elasticsearch.common.xcontent.XContentParser in project elasticsearch by elastic.
the class Laplace method fromXContent.
public static SmoothingModel fromXContent(XContentParser parser) throws IOException {
XContentParser.Token token;
String fieldName = null;
double alpha = DEFAULT_LAPLACE_ALPHA;
while ((token = parser.nextToken()) != Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
fieldName = parser.currentName();
}
if (token.isValue() && ALPHA_FIELD.match(fieldName)) {
alpha = parser.doubleValue();
}
}
return new Laplace(alpha);
}
use of org.elasticsearch.common.xcontent.XContentParser in project elasticsearch by elastic.
the class MultiGetRequestTests method testAddWithValidSourceValueIsAccepted.
public void testAddWithValidSourceValueIsAccepted() throws Exception {
XContentParser parser = createParser(XContentFactory.jsonBuilder().startObject().startArray("docs").startObject().field("_source", randomFrom("false", "true")).endObject().startObject().field("_source", randomBoolean()).endObject().endArray().endObject());
MultiGetRequest multiGetRequest = new MultiGetRequest();
multiGetRequest.add(randomAsciiOfLength(5), randomAsciiOfLength(3), null, FetchSourceContext.FETCH_SOURCE, null, parser, true);
assertEquals(2, multiGetRequest.getItems().size());
}
use of org.elasticsearch.common.xcontent.XContentParser in project elasticsearch by elastic.
the class MultiGetRequestTests method testAddWithInvalidSourceValueIsRejected.
public void testAddWithInvalidSourceValueIsRejected() throws Exception {
String sourceValue = randomFrom("on", "off", "0", "1");
XContentParser parser = createParser(XContentFactory.jsonBuilder().startObject().startArray("docs").startObject().field("_source", sourceValue).endObject().endArray().endObject());
MultiGetRequest multiGetRequest = new MultiGetRequest();
IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> multiGetRequest.add(randomAsciiOfLength(5), randomAsciiOfLength(3), null, FetchSourceContext.FETCH_SOURCE, null, parser, true));
assertEquals("Failed to parse value [" + sourceValue + "] as only [true] or [false] are allowed.", ex.getMessage());
}
use of org.elasticsearch.common.xcontent.XContentParser in project elasticsearch by elastic.
the class MainResponseTests method testFromXContent.
public void testFromXContent() throws IOException {
MainResponse mainResponse = createTestItem();
XContentType xContentType = randomFrom(XContentType.values());
boolean humanReadable = randomBoolean();
BytesReference originalBytes = toXContent(mainResponse, xContentType, humanReadable);
MainResponse parsed;
try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
parsed = MainResponse.fromXContent(parser);
assertNull(parser.nextToken());
}
assertEquals(mainResponse.getClusterUuid(), parsed.getClusterUuid());
assertEquals(mainResponse.getClusterName(), parsed.getClusterName());
assertEquals(mainResponse.getNodeName(), parsed.getNodeName());
assertEquals(mainResponse.getBuild(), parsed.getBuild());
assertEquals(mainResponse.getVersion(), parsed.getVersion());
// we cannot recreate the "available" flag from xContent, but should be "true" if request came through
assertEquals(true, parsed.isAvailable());
assertToXContentEquivalent(originalBytes, toXContent(parsed, xContentType, humanReadable), xContentType);
}
Aggregations