Search in sources :

Example 81 with XContentParser

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);
}
Also used : ArrayList(java.util.ArrayList) Token(org.elasticsearch.common.xcontent.XContentParser.Token) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 82 with XContentParser

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);
}
Also used : Token(org.elasticsearch.common.xcontent.XContentParser.Token) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 83 with XContentParser

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());
}
Also used : XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 84 with XContentParser

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());
}
Also used : XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 85 with XContentParser

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);
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) XContentType(org.elasticsearch.common.xcontent.XContentType) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Aggregations

XContentParser (org.elasticsearch.common.xcontent.XContentParser)411 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)125 ParsingException (org.elasticsearch.common.ParsingException)109 IOException (java.io.IOException)69 BytesReference (org.elasticsearch.common.bytes.BytesReference)57 ArrayList (java.util.ArrayList)50 XContentType (org.elasticsearch.common.xcontent.XContentType)43 QueryParseContext (org.elasticsearch.index.query.QueryParseContext)43 ElasticsearchParseException (org.elasticsearch.ElasticsearchParseException)42 HashMap (java.util.HashMap)28 Matchers.containsString (org.hamcrest.Matchers.containsString)23 Map (java.util.Map)22 List (java.util.List)17 Settings (org.elasticsearch.common.settings.Settings)17 ShardId (org.elasticsearch.index.shard.ShardId)16 Script (org.elasticsearch.script.Script)16 ToXContent (org.elasticsearch.common.xcontent.ToXContent)15 ElasticsearchException (org.elasticsearch.ElasticsearchException)14 NodeClient (org.elasticsearch.client.node.NodeClient)14 BaseRestHandler (org.elasticsearch.rest.BaseRestHandler)14