org.elasticsearch.common.xcontent.XContentParseException Java Examples

The following examples show how to use org.elasticsearch.common.xcontent.XContentParseException. 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: AbstractXContentParser.java    From crate with Apache License 2.0 6 votes vote down vote up
static List<Object> readList(XContentParser parser, MapFactory mapFactory) throws IOException {
    XContentParser.Token token = parser.currentToken();
    if (token == null) {
        token = parser.nextToken();
    }
    if (token == XContentParser.Token.FIELD_NAME) {
        token = parser.nextToken();
    }
    if (token == XContentParser.Token.START_ARRAY) {
        token = parser.nextToken();
    } else {
        throw new XContentParseException(parser.getTokenLocation(), "Failed to parse list:  expecting "
                + XContentParser.Token.START_ARRAY + " but got " + token);
    }

    ArrayList<Object> list = new ArrayList<>();
    for (; token != null && token != XContentParser.Token.END_ARRAY; token = parser.nextToken()) {
        list.add(readValue(parser, mapFactory, token));
    }
    return list;
}
 
Example #2
Source File: AnomalyDetectorProfileRunner.java    From anomaly-detection with Apache License 2.0 5 votes vote down vote up
private ActionListener<SearchResponse> onGetLatestAnomalyResult(ActionListener<DetectorProfile> listener, String detectorId) {
    return ActionListener.wrap(searchResponse -> {
        SearchHits hits = searchResponse.getHits();
        if (hits.getHits().length == 0L) {
            listener.onResponse(new DetectorProfile());
        } else {
            SearchHit hit = hits.getAt(0);

            try (
                XContentParser parser = XContentType.JSON
                    .xContent()
                    .createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, hit.getSourceAsString())
            ) {
                ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation);
                AnomalyResult result = parser.namedObject(AnomalyResult.class, AnomalyResult.PARSE_FIELD_NAME, null);
                DetectorProfile profile = new DetectorProfile();
                if (result.getError() != null) {
                    profile.setError(result.getError());
                }
                listener.onResponse(profile);

            } catch (IOException | XContentParseException | NullPointerException e) {
                logger.error("Fail to parse anomaly result with " + hit.toString());
                listener.onFailure(new RuntimeException("Fail to find detector error: " + detectorId, e));
            }
        }
    }, exception -> {
        if (exception instanceof IndexNotFoundException) {
            listener.onResponse(new DetectorProfile());
        } else {
            logger.error("Fail to find any anomaly result after AD job enabled time for detector {}", detectorId);
            listener.onFailure(new RuntimeException("Fail to find detector error: " + detectorId, exception));
        }
    });
}
 
Example #3
Source File: XGBoostJsonParser.java    From elasticsearch-learning-to-rank with Apache License 2.0 5 votes vote down vote up
public static XGBoostDefinition parse(XContentParser parser, FeatureSet set) throws IOException {
    XGBoostDefinition definition;
    XContentParser.Token startToken = parser.nextToken();

    // The model definition can either be an array of tree definitions, or an object containing the
    // tree definitions in the 'splits' field. Using an object allows for specification of additional
    // parameters.
    if (startToken == XContentParser.Token.START_OBJECT) {
        try {
            definition = PARSER.apply(parser, set);
        } catch (XContentParseException e) {
            throw new ParsingException(parser.getTokenLocation(), "Unable to parse XGBoost object", e);
        }
        if (definition.splitParserStates == null) {
            throw new ParsingException(parser.getTokenLocation(), "XGBoost model missing required field [splits]");
        }
    } else if (startToken == XContentParser.Token.START_ARRAY) {
        definition = new XGBoostDefinition();
        definition.splitParserStates = new ArrayList<>();
        while (parser.nextToken() != XContentParser.Token.END_ARRAY) {
            definition.splitParserStates.add(SplitParserState.parse(parser, set));
        }
    } else {
        throw new ParsingException(parser.getTokenLocation(), "Expected [START_ARRAY] or [START_OBJECT] but got ["
                + startToken + "]");
    }
    if (definition.splitParserStates.size() == 0) {
        throw new ParsingException(parser.getTokenLocation(), "XGBoost model must define at lease one tree");
    }
    return definition;
}
 
Example #4
Source File: GeoPointClusteringParserTests.java    From elasticsearch-aggregation-geoclustering with Apache License 2.0 5 votes vote down vote up
public void testParseInValidZoom() throws Exception {
    int zoom = randomIntBetween(26, 99);
    XContentParser stParser = createParser(JsonXContent.jsonXContent,
            "{\"field\":\"my_loc\", \"zoom\":" + zoom + "}");
    XContentParser.Token token = stParser.nextToken();
    assertSame(XContentParser.Token.START_OBJECT, token);

    XContentParseException ex = expectThrows(XContentParseException.class,
            () -> GeoPointClusteringAggregationBuilder.parse("geo_point_clustering", stParser));
    assertThat(ex.getMessage(), containsString("[geo_point_clustering] failed to parse field [zoom]"));
}
 
Example #5
Source File: ElasticsearchException.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the root cause of this exception or multiple if different shards caused different exceptions.
 * If the given exception is not an instance of {@link org.elasticsearch.ElasticsearchException} an empty array
 * is returned.
 */
public static ElasticsearchException[] guessRootCauses(Throwable t) {
    Throwable ex = ExceptionsHelper.unwrapCause(t);
    if (ex instanceof ElasticsearchException) {
        // ElasticsearchException knows how to guess its own root cause
        return ((ElasticsearchException) ex).guessRootCauses();
    }
    if (ex instanceof XContentParseException) {
        /*
         * We'd like to unwrap parsing exceptions to the inner-most
         * parsing exception because that is generally the most interesting
         * exception to return to the user. If that exception is caused by
         * an ElasticsearchException we'd like to keep unwrapping because
         * ElasticserachExceptions tend to contain useful information for
         * the user.
         */
        Throwable cause = ex.getCause();
        if (cause != null) {
            if (cause instanceof XContentParseException || cause instanceof ElasticsearchException) {
                return guessRootCauses(ex.getCause());
            }
        }
    }
    return new ElasticsearchException[] {
        new ElasticsearchException(t.getMessage(), t) {
            @Override
            protected String getExceptionName() {
                return getExceptionName(getCause());
            }
        }
    };
}