Package org.elasticsearch.common.xcontent

Examples of org.elasticsearch.common.xcontent.XContentParser


                            builder.endObject();

                            builder.startObject("mappings");
                            for (Map.Entry<String, CompressedString> entry : templateMetaData.mappings().entrySet()) {
                                byte[] mappingSource = entry.getValue().uncompressed();
                                XContentParser parser = XContentFactory.xContent(mappingSource).createParser(mappingSource);
                                Map<String, Object> mapping = parser.map();
                                if (mapping.size() == 1 && mapping.containsKey(entry.getKey())) {
                                    // the type name is the root value, reduce it
                                    mapping = (Map<String, Object>) mapping.get(entry.getKey());
                                }
                                builder.field(entry.getKey());
                                builder.map(mapping);
                            }
                            builder.endObject();


                            builder.endObject();
                        }
                        builder.endObject();

                        builder.startObject("indices");
                        for (IndexMetaData indexMetaData : state.metaData()) {
                            builder.startObject(indexMetaData.index(), XContentBuilder.FieldCaseConversion.NONE);

                            builder.field("state", indexMetaData.state().toString().toLowerCase());

                            builder.startObject("settings");
                            Settings settings = settingsFilter.filterSettings(indexMetaData.settings());
                            for (Map.Entry<String, String> entry : settings.getAsMap().entrySet()) {
                                builder.field(entry.getKey(), entry.getValue());
                            }
                            builder.endObject();

                            builder.startObject("mappings");
                            for (Map.Entry<String, MappingMetaData> entry : indexMetaData.mappings().entrySet()) {
                                byte[] mappingSource = entry.getValue().source().uncompressed();
                                XContentParser parser = XContentFactory.xContent(mappingSource).createParser(mappingSource);
                                Map<String, Object> mapping = parser.map();
                                if (mapping.size() == 1 && mapping.containsKey(entry.getKey())) {
                                    // the type name is the root value, reduce it
                                    mapping = (Map<String, Object>) mapping.get(entry.getKey());
                                }
                                builder.field(entry.getKey());
View Full Code Here


            //         { add : { index : "test1", alias : "alias1", filter : {"user" : "kimchy"} } }
            //         { remove : { index : "test1", alias : "alias1" } }
            //     ]
            // }
            indicesAliasesRequest.timeout(request.paramAsTime("timeout", timeValueSeconds(10)));
            XContentParser parser = XContentFactory.xContent(request.contentByteArray(), request.contentByteArrayOffset(), request.contentLength())
                    .createParser(request.contentByteArray(), request.contentByteArrayOffset(), request.contentLength());
            XContentParser.Token token = parser.nextToken();
            if (token == null) {
                throw new ElasticSearchIllegalArgumentException("No action is specified");
            }
            while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                if (token == XContentParser.Token.START_ARRAY) {
                    while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
                        if (token == XContentParser.Token.FIELD_NAME) {
                            String action = parser.currentName();
                            AliasAction.Type type;
                            if ("add".equals(action)) {
                                type = AliasAction.Type.ADD;
                            } else if ("remove".equals(action)) {
                                type = AliasAction.Type.REMOVE;
                            } else {
                                throw new ElasticSearchIllegalArgumentException("Alias action [" + action + "] not supported");
                            }
                            String index = null;
                            String alias = null;
                            Map<String, Object> filter = null;
                            String routing = null;
                            boolean routingSet = false;
                            String indexRouting = null;
                            boolean indexRoutingSet = false;
                            String searchRouting = null;
                            boolean searchRoutingSet = false;
                            String currentFieldName = null;
                            while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                                if (token == XContentParser.Token.FIELD_NAME) {
                                    currentFieldName = parser.currentName();
                                } else if (token == XContentParser.Token.VALUE_STRING) {
                                    if ("index".equals(currentFieldName)) {
                                        index = parser.text();
                                    } else if ("alias".equals(currentFieldName)) {
                                        alias = parser.text();
                                    } else if ("routing".equals(currentFieldName)) {
                                        routing = parser.textOrNull();
                                        routingSet = true;
                                    } else if ("indexRouting".equals(currentFieldName) || "index-routing".equals(currentFieldName) || "index_routing".equals(currentFieldName)) {
                                        indexRouting = parser.textOrNull();
                                        indexRoutingSet = true;
                                    } else if ("searchRouting".equals(currentFieldName) || "search-routing".equals(currentFieldName) || "search_routing".equals(currentFieldName)) {
                                        searchRouting = parser.textOrNull();
                                        searchRoutingSet = true;
                                    }
                                } else if (token == XContentParser.Token.START_OBJECT) {
                                    if ("filter".equals(currentFieldName)) {
                                        filter = parser.mapOrdered();
                                    }
                                }
                            }
                            if (index == null) {
                                throw new ElasticSearchIllegalArgumentException("Alias action [" + action + "] requires an [index] to be set");
View Full Code Here

                        builder.endObject();

                        builder.startObject("mappings");
                        for (Map.Entry<String, CompressedString> entry : indexMetaData.mappings().entrySet()) {
                            byte[] mappingSource = entry.getValue().uncompressed();
                            XContentParser parser = XContentFactory.xContent(mappingSource).createParser(mappingSource);
                            Map<String, Object> mapping = parser.map();
                            if (mapping.size() == 1 && mapping.containsKey(entry.getKey())) {
                                // the type name is the root value, reduce it
                                mapping = (Map<String, Object>) mapping.get(entry.getKey());
                            }
                            builder.field(entry.getKey());
View Full Code Here

                                // filter this type out...
                                continue;
                            }
                            foundType = true;
                            byte[] mappingSource = mappingMd.source().uncompressed();
                            XContentParser parser = XContentFactory.xContent(mappingSource).createParser(mappingSource);
                            Map<String, Object> mapping = parser.map();
                            if (mapping.size() == 1 && mapping.containsKey(mappingMd.type())) {
                                // the type name is the root value, reduce it
                                mapping = (Map<String, Object>) mapping.get(mappingMd.type());
                            }
                            builder.field(mappingMd.type());
                            builder.map(mapping);
                        }
                        if (!foundType) {
                            channel.sendResponse(new XContentThrowableRestResponse(request, new TypeMissingException(new Index(indices[0]), types.iterator().next())));
                            return;
                        }
                    } else {
                        for (IndexMetaData indexMetaData : metaData) {
                            builder.startObject(indexMetaData.index());

                            for (MappingMetaData mappingMd : indexMetaData.mappings().values()) {
                                if (!types.isEmpty() && !types.contains(mappingMd.type())) {
                                    // filter this type out...
                                    continue;
                                }
                                byte[] mappingSource = mappingMd.source().uncompressed();
                                XContentParser parser = XContentFactory.xContent(mappingSource).createParser(mappingSource);
                                Map<String, Object> mapping = parser.map();
                                if (mapping.size() == 1 && mapping.containsKey(mappingMd.type())) {
                                    // the type name is the root value, reduce it
                                    mapping = (Map<String, Object>) mapping.get(mappingMd.type());
                                }
                                builder.field(mappingMd.type());
View Full Code Here

    @Override public void parse(ParseContext context) throws IOException {
        byte[] content = null;
        String contentType = null;
        String name = null;

        XContentParser parser = context.parser();
        XContentParser.Token token = parser.currentToken();
        if (token == XContentParser.Token.VALUE_STRING) {
            content = parser.binaryValue();
        } else {
            String currentFieldName = null;
            while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                if (token == XContentParser.Token.FIELD_NAME) {
                    currentFieldName = parser.currentName();
                } else if (token == XContentParser.Token.VALUE_STRING) {
                    if ("content".equals(currentFieldName)) {
                        content = parser.binaryValue();
                    } else if ("_content_type".equals(currentFieldName)) {
                        contentType = parser.text();
                    } else if ("_name".equals(currentFieldName)) {
                        name = parser.text();
                    }
                }
            }
        }
View Full Code Here

    private void parseSource(SearchContext context, byte[] source, int offset, int length) throws SearchParseException {
        // nothing to parse...
        if (source == null || length == 0) {
            return;
        }
        XContentParser parser = null;
        try {
            parser = XContentFactory.xContent(source, offset, length).createParser(source, offset, length);
            XContentParser.Token token;
            while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                if (token == XContentParser.Token.FIELD_NAME) {
                    String fieldName = parser.currentName();
                    parser.nextToken();
                    SearchParseElement element = elementParsers.get(fieldName);
                    if (element == null) {
                        throw new SearchParseException(context, "No parser for element [" + fieldName + "]");
                    }
                    element.parse(parser, context);
                } else if (token == null) {
                    break;
                }
            }
        } catch (Exception e) {
            String sSource = "_na_";
            try {
                sSource = Unicode.fromBytes(source, offset, length);
            } catch (Throwable e1) {
                // ignore
            }
            throw new SearchParseException(context, "Failed to parse source [" + sSource + "]", e);
        } finally {
            if (parser != null) {
                parser.close();
            }
        }
    }
View Full Code Here

*/
public class QueryBinaryParseElement implements SearchParseElement {

    @Override public void parse(XContentParser parser, SearchContext context) throws Exception {
        byte[] querySource = parser.binaryValue();
        XContentParser qSourceParser = XContentFactory.xContent(querySource).createParser(querySource);
        try {
            context.parsedQuery(context.queryParserService().parse(qSourceParser));
        } finally {
            qSourceParser.close();
        }
    }
View Full Code Here

*/
public class FilterBinaryParseElement implements SearchParseElement {

    @Override public void parse(XContentParser parser, SearchContext context) throws Exception {
        byte[] filterSource = parser.binaryValue();
        XContentParser fSourceParser = XContentFactory.xContent(filterSource).createParser(filterSource);
        try {
            context.parsedFilter(context.queryParserService().parseInnerFilter(fSourceParser));
        } finally {
            fSourceParser.close();
        }
    }
View Full Code Here

    private Map<String, Object> loadSourceIfNeeded() {
        if (source != null) {
            return source;
        }
        XContentParser parser = null;
        try {
            Document doc = reader.document(docId, SourceFieldSelector.INSTANCE);
            Fieldable sourceField = doc.getFieldable(SourceFieldMapper.NAME);
            byte[] source = sourceField.getBinaryValue();
            this.source = sourceAsMap(source, 0, source.length);
        } catch (Exception e) {
            throw new ElasticSearchParseException("failed to parse / load source", e);
        } finally {
            if (parser != null) {
                parser.close();
            }
        }
        return this.source;
    }
View Full Code Here

        }
        return this.source;
    }

    public static Map<String, Object> sourceAsMap(byte[] bytes, int offset, int length) {
        XContentParser parser = null;
        try {
            if (LZF.isCompressed(bytes, offset, length)) {
                BytesStreamInput siBytes = new BytesStreamInput(bytes, offset, length);
                LZFStreamInput siLzf = CachedStreamInput.cachedLzf(siBytes);
                XContentType contentType = XContentFactory.xContentType(siLzf);
                siLzf.resetToBufferStart();
                parser = XContentFactory.xContent(contentType).createParser(siLzf);
                return parser.map();
            } else {
                parser = XContentFactory.xContent(bytes, offset, length).createParser(bytes, offset, length);
                return parser.map();
            }
        } catch (Exception e) {
            throw new ElasticSearchParseException("Failed to parse source to map", e);
        } finally {
            if (parser != null) {
                parser.close();
            }
        }
    }
View Full Code Here

TOP

Related Classes of org.elasticsearch.common.xcontent.XContentParser

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.