Java Code Examples for org.elasticsearch.common.xcontent.XContentParser#mapOrdered()

The following examples show how to use org.elasticsearch.common.xcontent.XContentParser#mapOrdered() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SQLTransportExecutor.java    From crate with Apache License 2.0 6 votes vote down vote up
private static Object jsonToObject(String json) {
    try {
        if (json != null) {
            byte[] bytes = json.getBytes(StandardCharsets.UTF_8);
            XContentParser parser = JsonXContent.JSON_XCONTENT.createParser(
                NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, bytes);
            if (bytes.length >= 1 && bytes[0] == '[') {
                parser.nextToken();
                return parser.list();
            } else {
                return parser.mapOrdered();
            }
        } else {
            return null;
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 2
Source File: ArrayMapper.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
    /**
     * array mapping should look like:
     *
     * "fieldName": {
     *      "type": "array":
     *      "inner": {
     *          "type": "string"
     *          ...
     *      }
     * }
     *
     *
     * Use the innerMapper to generate the mapping for the inner type which will look like:
     *
     * "fieldName": {
     *      "type": "string",
     *      ...
     * }
     *
     * and then parse the contents of the object to set it into the "inner" field of the outer array type.
     */
    XContentBuilder innerBuilder = new XContentBuilder(builder.contentType().xContent(), new BytesStreamOutput(0));
    innerBuilder = innerMapper.toXContent(innerBuilder, params);
    innerBuilder.close();
    XContentParser parser = builder.contentType().xContent().createParser(innerBuilder.bytes());

    //noinspection StatementWithEmptyBody
    while ((parser.nextToken() != XContentParser.Token.START_OBJECT)) {
        // consume tokens until start of object
    }
    Map<String, Object> innerMap = parser.mapOrdered();

    builder.startObject(simpleName());
    builder.field("type", contentType());
    builder.field(INNER, innerMap);
    return builder.endObject();
}
 
Example 3
Source File: AliasMetaData.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public static AliasMetaData fromXContent(XContentParser parser) throws IOException {
    Builder builder = new Builder(parser.currentName());

    String currentFieldName = null;
    XContentParser.Token token = parser.nextToken();
    if (token == null) {
        // no data...
        return builder.build();
    }
    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 ("filter".equals(currentFieldName)) {
                Map<String, Object> filter = parser.mapOrdered();
                builder.filter(filter);
            }
        } else if (token == XContentParser.Token.VALUE_EMBEDDED_OBJECT) {
            if ("filter".equals(currentFieldName)) {
                builder.filter(new CompressedXContent(parser.binaryValue()));
            }
        } else if (token == XContentParser.Token.VALUE_STRING) {
            if ("routing".equals(currentFieldName)) {
                builder.routing(parser.text());
            } else if ("index_routing".equals(currentFieldName) || "indexRouting".equals(currentFieldName)) {
                builder.indexRouting(parser.text());
            } else if ("search_routing".equals(currentFieldName) || "searchRouting".equals(currentFieldName)) {
                builder.searchRouting(parser.text());
            }
        }
    }
    return builder.build();
}
 
Example 4
Source File: Alias.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
/**
 * Parses an alias and returns its parsed representation
 */
public static Alias fromXContent(XContentParser parser) throws IOException {
    Alias alias = new Alias(parser.currentName());

    String currentFieldName = null;
    XContentParser.Token token = parser.nextToken();
    if (token == null) {
        throw new IllegalArgumentException("No alias is specified");
    }
    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 ("filter".equals(currentFieldName)) {
                Map<String, Object> filter = parser.mapOrdered();
                alias.filter(filter);
            }
        } else if (token == XContentParser.Token.VALUE_STRING) {
            if ("routing".equals(currentFieldName)) {
                alias.routing(parser.text());
            } else if ("index_routing".equals(currentFieldName) || "indexRouting".equals(currentFieldName) || "index-routing".equals(currentFieldName)) {
                alias.indexRouting(parser.text());
            } else if ("search_routing".equals(currentFieldName) || "searchRouting".equals(currentFieldName) || "search-routing".equals(currentFieldName)) {
                alias.searchRouting(parser.text());
            }
        }
    }
    return alias;
}
 
Example 5
Source File: Alias.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Parses an alias and returns its parsed representation
 */
public static Alias fromXContent(XContentParser parser) throws IOException {
    Alias alias = new Alias(parser.currentName());

    String currentFieldName = null;
    XContentParser.Token token = parser.nextToken();
    if (token == null) {
        throw new IllegalArgumentException("No alias is specified");
    }
    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 (FILTER.match(currentFieldName, parser.getDeprecationHandler())) {
                Map<String, Object> filter = parser.mapOrdered();
                alias.filter(filter);
            }
        } else if (token == XContentParser.Token.VALUE_STRING) {
            if (ROUTING.match(currentFieldName, parser.getDeprecationHandler())) {
                alias.routing(parser.text());
            } else if (INDEX_ROUTING.match(currentFieldName, parser.getDeprecationHandler())) {
                alias.indexRouting(parser.text());
            } else if (SEARCH_ROUTING.match(currentFieldName, parser.getDeprecationHandler())) {
                alias.searchRouting(parser.text());
            }
        } else if (token == XContentParser.Token.VALUE_BOOLEAN) {
            if (IS_WRITE_INDEX.match(currentFieldName, parser.getDeprecationHandler())) {
                alias.writeIndex(parser.booleanValue());
            }
        }
    }
    return alias;
}
 
Example 6
Source File: AliasMetaData.java    From crate with Apache License 2.0 4 votes vote down vote up
public static AliasMetaData fromXContent(XContentParser parser) throws IOException {
    Builder builder = new Builder(parser.currentName());

    String currentFieldName = null;
    XContentParser.Token token = parser.nextToken();
    if (token == null) {
        // no data...
        return builder.build();
    }
    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 ("filter".equals(currentFieldName)) {
                Map<String, Object> filter = parser.mapOrdered();
                builder.filter(filter);
            } else {
                parser.skipChildren();
            }
        } else if (token == XContentParser.Token.VALUE_EMBEDDED_OBJECT) {
            if ("filter".equals(currentFieldName)) {
                builder.filter(new CompressedXContent(parser.binaryValue()));
            }
        } else if (token == XContentParser.Token.VALUE_STRING) {
            if ("routing".equals(currentFieldName)) {
                builder.routing(parser.text());
            } else if ("index_routing".equals(currentFieldName) || "indexRouting".equals(currentFieldName)) {
                builder.indexRouting(parser.text());
            } else if ("search_routing".equals(currentFieldName) || "searchRouting".equals(currentFieldName)) {
                builder.searchRouting(parser.text());
            }
        } else if (token == XContentParser.Token.START_ARRAY) {
            parser.skipChildren();
        } else if (token == XContentParser.Token.VALUE_BOOLEAN) {
            if ("is_write_index".equals(currentFieldName)) {
                builder.writeIndex(parser.booleanValue());
            }
        }
    }
    return builder.build();
}