Package org.elasticsearch.common.xcontent

Examples of org.elasticsearch.common.xcontent.XContentParser


    @Override public String[] names() {
        return new String[]{NAME, Strings.toCamelCase(NAME)};
    }

    @Override public Query parse(QueryParseContext parseContext) throws IOException, QueryParsingException {
        XContentParser parser = parseContext.parser();

        float boost = 1.0f;
        int slop = -1;
        boolean inOrder = true;
        boolean collectPayloads = true;

        List<SpanQuery> clauses = newArrayList();

        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 == XContentParser.Token.START_ARRAY) {
                if ("clauses".equals(currentFieldName)) {
                    while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
                        Query query = parseContext.parseInnerQuery();
                        if (!(query instanceof SpanQuery)) {
                            throw new QueryParsingException(parseContext.index(), "spanNear [clauses] must be of type span query");
                        }
                        clauses.add((SpanQuery) query);
                    }
                }
            } else if (token.isValue()) {
                if ("in_order".equals(currentFieldName) || "inOrder".equals(currentFieldName)) {
                    inOrder = parser.booleanValue();
                } else if ("collect_payloads".equals(currentFieldName) || "collectPayloads".equals(currentFieldName)) {
                    collectPayloads = parser.booleanValue();
                } else if ("slop".equals(currentFieldName)) {
                    slop = parser.intValue();
                } else if ("boost".equals(currentFieldName)) {
                    boost = parser.floatValue();
                }
            }
        }
        if (clauses.isEmpty()) {
            throw new QueryParsingException(parseContext.index(), "span_near must include [clauses]");
View Full Code Here


    @Override public String[] names() {
        return new String[]{NAME, "geoBbox", "geo_bounding_box", "geoBoundingBox"};
    }

    @Override public Filter parse(QueryParseContext parseContext) throws IOException, QueryParsingException {
        XContentParser parser = parseContext.parser();

        boolean cache = false;
        CacheKeyFilter.Key cacheKey = null;
        String fieldName = null;
        GeoBoundingBoxFilter.Point topLeft = new GeoBoundingBoxFilter.Point();
        GeoBoundingBoxFilter.Point bottomRight = new GeoBoundingBoxFilter.Point();

        String filterName = null;
        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 == XContentParser.Token.START_OBJECT) {
                fieldName = currentFieldName;

                while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                    if (token == XContentParser.Token.FIELD_NAME) {
                        currentFieldName = parser.currentName();
                    } else if (token == XContentParser.Token.START_ARRAY) {
                        GeoBoundingBoxFilter.Point point = null;
                        if ("top_left".equals(currentFieldName) || "topLeft".equals(currentFieldName)) {
                            point = topLeft;
                        } else if ("bottom_right".equals(currentFieldName) || "bottomRight".equals(currentFieldName)) {
                            point = bottomRight;
                        }

                        if (point != null) {
                            token = parser.nextToken();
                            point.lon = parser.doubleValue();
                            token = parser.nextToken();
                            point.lat = parser.doubleValue();
                            while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {

                            }
                        }
                    } else if (token == XContentParser.Token.START_OBJECT) {
                        GeoBoundingBoxFilter.Point point = null;
                        if ("top_left".equals(currentFieldName) || "topLeft".equals(currentFieldName)) {
                            point = topLeft;
                        } else if ("bottom_right".equals(currentFieldName) || "bottomRight".equals(currentFieldName)) {
                            point = bottomRight;
                        }

                        if (point != null) {
                            while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                                if (token == XContentParser.Token.FIELD_NAME) {
                                    currentFieldName = parser.currentName();
                                } else if (token.isValue()) {
                                    if (currentFieldName.equals(GeoPointFieldMapper.Names.LAT)) {
                                        point.lat = parser.doubleValue();
                                    } else if (currentFieldName.equals(GeoPointFieldMapper.Names.LON)) {
                                        point.lon = parser.doubleValue();
                                    } else if (currentFieldName.equals(GeoPointFieldMapper.Names.GEOHASH)) {
                                        double[] values = GeoHashUtils.decode(parser.text());
                                        point.lat = values[0];
                                        point.lon = values[1];
                                    }
                                }
                            }
                        }
                    } else if (token.isValue()) {
                        if ("field".equals(currentFieldName)) {
                            fieldName = parser.text();
                        } else {
                            GeoBoundingBoxFilter.Point point = null;
                            if ("top_left".equals(currentFieldName) || "topLeft".equals(currentFieldName)) {
                                point = topLeft;
                            } else if ("bottom_right".equals(currentFieldName) || "bottomRight".equals(currentFieldName)) {
                                point = bottomRight;
                            }

                            if (point != null) {
                                String value = parser.text();
                                int comma = value.indexOf(',');
                                if (comma != -1) {
                                    point.lat = Double.parseDouble(value.substring(0, comma).trim());
                                    point.lon = Double.parseDouble(value.substring(comma + 1).trim());
                                } else {
                                    double[] values = GeoHashUtils.decode(value);
                                    point.lat = values[0];
                                    point.lon = values[1];
                                }
                            }
                        }
                    }
                }
            } else if (token.isValue()) {
                if ("_name".equals(currentFieldName)) {
                    filterName = parser.text();
                } else if ("_cache".equals(currentFieldName)) {
                    cache = parser.booleanValue();
                } else if ("_cache_key".equals(currentFieldName) || "_cacheKey".equals(currentFieldName)) {
                    cacheKey = new CacheKeyFilter.Key(parser.text());
                }
            }
        }

        MapperService mapperService = parseContext.mapperService();
View Full Code Here

    @Override public String[] names() {
        return new String[]{NAME};
    }

    @Override public Filter parse(QueryParseContext parseContext) throws IOException, QueryParsingException {
        XContentParser parser = parseContext.parser();

        String fieldName = null;
        String filterName = null;

        XContentParser.Token token;
        String currentFieldName = null;
        while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
            if (token == XContentParser.Token.FIELD_NAME) {
                currentFieldName = parser.currentName();
            } else if (token.isValue()) {
                if ("field".equals(currentFieldName)) {
                    fieldName = parser.text();
                } else if ("_name".equals(currentFieldName)) {
                    filterName = parser.text();
                }
            }
        }

        if (fieldName == null) {
View Full Code Here

    @Override public String[] names() {
        return new String[]{NAME, Strings.toCamelCase(NAME)};
    }

    @Override public Query parse(QueryParseContext parseContext) throws IOException, QueryParsingException {
        XContentParser parser = parseContext.parser();

        Query query = null;
        float boost = 1.0f;
        String childType = null;
        String scope = null;
        TopChildrenQuery.ScoreType scoreType = TopChildrenQuery.ScoreType.MAX;
        int factor = 5;
        int incrementalFactor = 2;

        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 == XContentParser.Token.START_OBJECT) {
                if ("query".equals(currentFieldName)) {
                    query = parseContext.parseInnerQuery();
                }
            } else if (token.isValue()) {
                if ("type".equals(currentFieldName)) {
                    childType = parser.text();
                } else if ("_scope".equals(currentFieldName)) {
                    scope = parser.text();
                } else if ("score".equals(currentFieldName)) {
                    scoreType = TopChildrenQuery.ScoreType.fromString(parser.text());
                } else if ("boost".equals(currentFieldName)) {
                    boost = parser.floatValue();
                } else if ("factor".equals(currentFieldName)) {
                    factor = parser.intValue();
                } else if ("incremental_factor".equals(currentFieldName) || "incrementalFactor".equals(currentFieldName)) {
                    incrementalFactor = parser.intValue();
                }
            }
        }
        if (query == null) {
            throw new QueryParsingException(parseContext.index(), "[child] requires 'query' field");
View Full Code Here

    public void addQuery(String name, byte[] source, int sourceOffset, int sourceLength) throws ElasticSearchException {
        addQuery(name, parseQuery(name, source, sourceOffset, sourceLength));
    }

    public Query parseQuery(String name, byte[] source, int sourceOffset, int sourceLength) throws ElasticSearchException {
        XContentParser parser = null;
        try {
            parser = XContentFactory.xContent(source, sourceOffset, sourceLength).createParser(source, sourceOffset, sourceLength);
            Query query = null;
            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 == XContentParser.Token.START_OBJECT) {
                    if ("query".equals(currentFieldName)) {
                        query = queryParserService.parse(parser).query();
                    }
                }
            }
            return query;
        } catch (IOException e) {
            throw new ElasticSearchException("Failed to add query [" + name + "]", e);
        } finally {
            if (parser != null) {
                parser.close();
            }
        }
    }
View Full Code Here

    }

    public Response percolate(final SourceRequest request) throws ElasticSearchException {
        Query query = null;
        ParsedDocument doc = null;
        XContentParser parser = null;
        try {

            parser = XContentFactory.xContent(request.source(), request.offset(), request.length()).createParser(request.source(), request.offset(), request.length());
            String currentFieldName = null;
            XContentParser.Token token;
            while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                if (token == XContentParser.Token.FIELD_NAME) {
                    currentFieldName = parser.currentName();
                    // we need to check the "doc" here, so the next token will be START_OBJECT which is
                    // the actual document starting
                    if ("doc".equals(currentFieldName)) {
                        DocumentMapper docMapper = mapperService.documentMapperWithAutoCreate(request.type());
                        doc = docMapper.parse(source(parser).type(request.type()).flyweight(true));
                    }
                } else if (token == XContentParser.Token.START_OBJECT) {
                    if ("query".equals(currentFieldName)) {
                        query = queryParserService.parse(parser).query();
                    }
                } else if (token == null) {
                    break;
                }
            }
        } catch (IOException e) {
            throw new PercolatorException(index, "failed to parse request", e);
        } finally {
            if (parser != null) {
                parser.close();
            }
        }

        if (doc == null) {
            throw new PercolatorException(index, "No doc to percolate in the request");
View Full Code Here

        }
        if (sourceAsMap != null) {
            return sourceAsMap;
        }
        byte[] source = source();
        XContentParser parser = null;
        try {
            parser = XContentFactory.xContent(source).createParser(source);
            sourceAsMap = parser.map();
            parser.close();
            return sourceAsMap;
        } catch (Exception e) {
            throw new ElasticSearchParseException("Failed to parse source to map", e);
        } finally {
            if (parser != null) {
                parser.close();
            }
        }
    }
View Full Code Here

        builder.endObject();
        return builder.copiedBytes();
    }

    public static CommitPoint fromXContent(byte[] data) throws Exception {
        XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(data);
        try {
            String currentFieldName = null;
            XContentParser.Token token = parser.nextToken();
            if (token == null) {
                // no data...
                throw new IOException("No commit point data");
            }
            long version = -1;
            String name = null;
            CommitPoint.Type type = null;
            List<CommitPoint.FileInfo> indexFiles = Lists.newArrayList();
            List<CommitPoint.FileInfo> translogFiles = Lists.newArrayList();
            while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                if (token == XContentParser.Token.FIELD_NAME) {
                    currentFieldName = parser.currentName();
                } else if (token == XContentParser.Token.START_OBJECT) {
                    List<CommitPoint.FileInfo> files = null;
                    if ("index_files".equals(currentFieldName) || "indexFiles".equals(currentFieldName)) {
                        files = indexFiles;
                    } else if ("translog_files".equals(currentFieldName) || "translogFiles".equals(currentFieldName)) {
                        files = translogFiles;
                    } else {
                        throw new IOException("Can't handle object with name [" + currentFieldName + "]");
                    }
                    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                        if (token == XContentParser.Token.FIELD_NAME) {
                            currentFieldName = parser.currentName();
                        } else if (token == XContentParser.Token.START_OBJECT) {
                            String fileName = currentFieldName;
                            String physicalName = null;
                            long size = -1;
                            String checksum = null;
                            while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                                if (token == XContentParser.Token.FIELD_NAME) {
                                    currentFieldName = parser.currentName();
                                } else if (token.isValue()) {
                                    if ("physical_name".equals(currentFieldName) || "physicalName".equals(currentFieldName)) {
                                        physicalName = parser.text();
                                    } else if ("length".equals(currentFieldName)) {
                                        size = parser.longValue();
                                    } else if ("checksum".equals(currentFieldName)) {
                                        checksum = parser.text();
                                    }
                                }
                            }
                            if (physicalName == null) {
                                throw new IOException("Malformed commit, missing physical_name for [" + fileName + "]");
                            }
                            if (size == -1) {
                                throw new IOException("Malformed commit, missing length for [" + fileName + "]");
                            }
                            files.add(new CommitPoint.FileInfo(fileName, physicalName, size, checksum));
                        }
                    }
                } else if (token.isValue()) {
                    if ("version".equals(currentFieldName)) {
                        version = parser.longValue();
                    } else if ("name".equals(currentFieldName)) {
                        name = parser.text();
                    } else if ("type".equals(currentFieldName)) {
                        type = CommitPoint.Type.valueOf(parser.text());
                    }
                }
            }

            if (version == -1) {
                throw new IOException("Malformed commit, missing version");
            }
            if (name == null) {
                throw new IOException("Malformed commit, missing name");
            }
            if (type == null) {
                throw new IOException("Malformed commit, missing type");
            }

            return new CommitPoint(version, name, type, indexFiles, translogFiles);
        } finally {
            parser.close();
        }
    }
View Full Code Here

            int nextMarker = findNextMarker(marker, from, data, length);
            if (nextMarker == -1) {
                break;
            }
            // now parse the action
            XContentParser parser = xContent.createParser(data, from, nextMarker - from);

            // move pointers
            from = nextMarker + 1;

            // Move to START_OBJECT
            XContentParser.Token token = parser.nextToken();
            if (token == null) {
                continue;
            }
            assert token == XContentParser.Token.START_OBJECT;
            // Move to FIELD_NAME, that's the action
            token = parser.nextToken();
            assert token == XContentParser.Token.FIELD_NAME;
            String action = parser.currentName();
            // Move to START_OBJECT
            token = parser.nextToken();
            assert token == XContentParser.Token.START_OBJECT;

            String index = null;
            String type = null;
            String id = null;
            String routing = null;
            String parent = null;
            String opType = null;
            long version = 0;
            VersionType versionType = VersionType.INTERNAL;
            String percolate = null;

            String currentFieldName = null;
            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 ("_type".equals(currentFieldName)) {
                        type = parser.text();
                    } else if ("_id".equals(currentFieldName)) {
                        id = parser.text();
                    } else if ("_routing".equals(currentFieldName) || "routing".equals(currentFieldName)) {
                        routing = parser.text();
                    } else if ("_parent".equals(currentFieldName) || "parent".equals(currentFieldName)) {
                        parent = parser.text();
                    } else if ("op_type".equals(currentFieldName) || "opType".equals(currentFieldName)) {
                        opType = parser.text();
                    } else if ("_version".equals(currentFieldName) || "version".equals(currentFieldName)) {
                        version = parser.longValue();
                    } else if ("_version_type".equals(currentFieldName) || "_versionType".equals(currentFieldName) || "version_type".equals(currentFieldName) || "versionType".equals(currentFieldName)) {
                        versionType = VersionType.fromString(parser.text());
                    } else if ("percolate".equals(currentFieldName)) {
                        percolate = parser.textOrNull();
                    }
                }
            }

            if ("delete".equals(action)) {
View Full Code Here

        return this.percolate;
    }

    public void processRouting(MappingMetaData mappingMd) throws ElasticSearchException {
        if (routing == null && mappingMd.routing().hasPath()) {
            XContentParser parser = null;
            try {
                parser = XContentFactory.xContent(source, sourceOffset, sourceLength)
                        .createParser(source, sourceOffset, sourceLength);
                routing = mappingMd.parseRouting(parser);
            } catch (Exception e) {
                throw new ElasticSearchParseException("failed to parse doc to extract routing", e);
            } finally {
                if (parser != null) {
                    parser.close();
                }
            }
        }
        // might as well check for routing here
        if (mappingMd.routing().required() && routing == null) {
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.