Search in sources :

Example 41 with XContentParser

use of org.elasticsearch.common.xcontent.XContentParser in project elasticsearch by elastic.

the class RepositoryDataTests method testXContent.

public void testXContent() throws IOException {
    RepositoryData repositoryData = generateRandomRepoData();
    XContentBuilder builder = JsonXContent.contentBuilder();
    repositoryData.snapshotsToXContent(builder, ToXContent.EMPTY_PARAMS);
    XContentParser parser = createParser(JsonXContent.jsonXContent, builder.bytes());
    long gen = (long) randomIntBetween(0, 500);
    RepositoryData fromXContent = RepositoryData.snapshotsFromXContent(parser, gen);
    assertEquals(repositoryData, fromXContent);
    assertEquals(gen, fromXContent.getGenId());
}
Also used : XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 42 with XContentParser

use of org.elasticsearch.common.xcontent.XContentParser in project elasticsearch by elastic.

the class RestAnalyzeActionTests method testParseXContentForAnalyzeRequest.

public void testParseXContentForAnalyzeRequest() throws Exception {
    XContentParser content = createParser(XContentFactory.jsonBuilder().startObject().field("text", "THIS IS A TEST").field("tokenizer", "keyword").array("filter", "lowercase").endObject());
    AnalyzeRequest analyzeRequest = new AnalyzeRequest("for test");
    RestAnalyzeAction.buildFromContent(content, analyzeRequest);
    assertThat(analyzeRequest.text().length, equalTo(1));
    assertThat(analyzeRequest.text(), equalTo(new String[] { "THIS IS A TEST" }));
    assertThat(analyzeRequest.tokenizer().name, equalTo("keyword"));
    assertThat(analyzeRequest.tokenFilters().size(), equalTo(1));
    for (AnalyzeRequest.NameOrDefinition filter : analyzeRequest.tokenFilters()) {
        assertThat(filter.name, equalTo("lowercase"));
    }
}
Also used : AnalyzeRequest(org.elasticsearch.action.admin.indices.analyze.AnalyzeRequest) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 43 with XContentParser

use of org.elasticsearch.common.xcontent.XContentParser in project elasticsearch by elastic.

the class ShapeBuilder method parseCoordinates.

/**
     * Recursive method which parses the arrays of coordinates used to define
     * Shapes
     *
     * @param parser
     *            Parser that will be read from
     * @return CoordinateNode representing the start of the coordinate tree
     * @throws IOException
     *             Thrown if an error occurs while reading from the
     *             XContentParser
     */
private static CoordinateNode parseCoordinates(XContentParser parser) throws IOException {
    XContentParser.Token token = parser.nextToken();
    // Base cases
    if (token != XContentParser.Token.START_ARRAY && token != XContentParser.Token.END_ARRAY && token != XContentParser.Token.VALUE_NULL) {
        double lon = parser.doubleValue();
        token = parser.nextToken();
        double lat = parser.doubleValue();
        token = parser.nextToken();
        while (token == XContentParser.Token.VALUE_NUMBER) {
            token = parser.nextToken();
        }
        return new CoordinateNode(new Coordinate(lon, lat));
    } else if (token == XContentParser.Token.VALUE_NULL) {
        throw new IllegalArgumentException("coordinates cannot contain NULL values)");
    }
    List<CoordinateNode> nodes = new ArrayList<>();
    while (token != XContentParser.Token.END_ARRAY) {
        nodes.add(parseCoordinates(parser));
        token = parser.nextToken();
    }
    return new CoordinateNode(nodes);
}
Also used : Coordinate(com.vividsolutions.jts.geom.Coordinate) ArrayList(java.util.ArrayList) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 44 with XContentParser

use of org.elasticsearch.common.xcontent.XContentParser in project elasticsearch by elastic.

the class CancelAllocationCommand method fromXContent.

public static CancelAllocationCommand fromXContent(XContentParser parser) throws IOException {
    String index = null;
    int shardId = -1;
    String nodeId = null;
    boolean allowPrimary = false;
    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 ("index".equals(currentFieldName)) {
                index = parser.text();
            } else if ("shard".equals(currentFieldName)) {
                shardId = parser.intValue();
            } else if ("node".equals(currentFieldName)) {
                nodeId = parser.text();
            } else if ("allow_primary".equals(currentFieldName) || "allowPrimary".equals(currentFieldName)) {
                allowPrimary = parser.booleanValue();
            } else {
                throw new ElasticsearchParseException("[{}] command does not support field [{}]", NAME, currentFieldName);
            }
        } else {
            throw new ElasticsearchParseException("[{}] command does not support complex json tokens [{}]", NAME, token);
        }
    }
    if (index == null) {
        throw new ElasticsearchParseException("[{}] command missing the index parameter", NAME);
    }
    if (shardId == -1) {
        throw new ElasticsearchParseException("[{}] command missing the shard parameter", NAME);
    }
    if (nodeId == null) {
        throw new ElasticsearchParseException("[{}] command missing the node parameter", NAME);
    }
    return new CancelAllocationCommand(index, shardId, nodeId, allowPrimary);
}
Also used : ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 45 with XContentParser

use of org.elasticsearch.common.xcontent.XContentParser in project elasticsearch by elastic.

the class NestedQueryBuilder method fromXContent.

public static NestedQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException {
    XContentParser parser = parseContext.parser();
    float boost = AbstractQueryBuilder.DEFAULT_BOOST;
    ScoreMode scoreMode = ScoreMode.Avg;
    String queryName = null;
    QueryBuilder query = null;
    String path = null;
    String currentFieldName = null;
    InnerHitBuilder innerHitBuilder = null;
    boolean ignoreUnmapped = DEFAULT_IGNORE_UNMAPPED;
    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 (QUERY_FIELD.match(currentFieldName)) {
                query = parseContext.parseInnerQueryBuilder();
            } else if (INNER_HITS_FIELD.match(currentFieldName)) {
                innerHitBuilder = InnerHitBuilder.fromXContent(parseContext);
            } else {
                throw new ParsingException(parser.getTokenLocation(), "[nested] query does not support [" + currentFieldName + "]");
            }
        } else if (token.isValue()) {
            if (PATH_FIELD.match(currentFieldName)) {
                path = parser.text();
            } else if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName)) {
                boost = parser.floatValue();
            } else if (IGNORE_UNMAPPED_FIELD.match(currentFieldName)) {
                ignoreUnmapped = parser.booleanValue();
            } else if (SCORE_MODE_FIELD.match(currentFieldName)) {
                scoreMode = HasChildQueryBuilder.parseScoreMode(parser.text());
            } else if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName)) {
                queryName = parser.text();
            } else {
                throw new ParsingException(parser.getTokenLocation(), "[nested] query does not support [" + currentFieldName + "]");
            }
        }
    }
    NestedQueryBuilder queryBuilder = new NestedQueryBuilder(path, query, scoreMode).ignoreUnmapped(ignoreUnmapped).queryName(queryName).boost(boost);
    if (innerHitBuilder != null) {
        queryBuilder.innerHit(innerHitBuilder, ignoreUnmapped);
    }
    return queryBuilder;
}
Also used : ScoreMode(org.apache.lucene.search.join.ScoreMode) ParsingException(org.elasticsearch.common.ParsingException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Aggregations

XContentParser (org.elasticsearch.common.xcontent.XContentParser)463 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)147 ParsingException (org.elasticsearch.common.ParsingException)110 IOException (java.io.IOException)77 BytesReference (org.elasticsearch.common.bytes.BytesReference)63 ArrayList (java.util.ArrayList)59 ElasticsearchParseException (org.elasticsearch.ElasticsearchParseException)56 XContentType (org.elasticsearch.common.xcontent.XContentType)47 QueryParseContext (org.elasticsearch.index.query.QueryParseContext)44 HashMap (java.util.HashMap)33 Map (java.util.Map)27 Matchers.containsString (org.hamcrest.Matchers.containsString)23 List (java.util.List)22 Settings (org.elasticsearch.common.settings.Settings)18 ToXContent (org.elasticsearch.common.xcontent.ToXContent)16 ShardId (org.elasticsearch.index.shard.ShardId)16 Script (org.elasticsearch.script.Script)16 XContent (org.elasticsearch.common.xcontent.XContent)15 ElasticsearchException (org.elasticsearch.ElasticsearchException)14 NodeClient (org.elasticsearch.client.node.NodeClient)14