Search in sources :

Example 66 with ElasticsearchParseException

use of org.elasticsearch.ElasticsearchParseException in project elasticsearch by elastic.

the class ContextMappings method load.

/**
     * Loads {@link ContextMappings} from configuration
     *
     * Expected configuration:
     *  List of maps representing {@link ContextMapping}
     *  [{"name": .., "type": .., ..}, {..}]
     *
     */
public static ContextMappings load(Object configuration, Version indexVersionCreated) throws ElasticsearchParseException {
    final List<ContextMapping> contextMappings;
    if (configuration instanceof List) {
        contextMappings = new ArrayList<>();
        List<Object> configurations = (List<Object>) configuration;
        for (Object contextConfig : configurations) {
            contextMappings.add(load((Map<String, Object>) contextConfig, indexVersionCreated));
        }
        if (contextMappings.size() == 0) {
            throw new ElasticsearchParseException("expected at least one context mapping");
        }
    } else if (configuration instanceof Map) {
        contextMappings = Collections.singletonList(load(((Map<String, Object>) configuration), indexVersionCreated));
    } else {
        throw new ElasticsearchParseException("expected a list or an entry of context mapping");
    }
    return new ContextMappings(contextMappings);
}
Also used : ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 67 with ElasticsearchParseException

use of org.elasticsearch.ElasticsearchParseException in project elasticsearch by elastic.

the class GeoQueryContext method fromXContent.

public static GeoQueryContext fromXContent(QueryParseContext context) throws IOException {
    XContentParser parser = context.parser();
    XContentParser.Token token = parser.currentToken();
    GeoQueryContext.Builder builder = new Builder();
    if (token == XContentParser.Token.START_OBJECT) {
        GEO_CONTEXT_PARSER.parse(parser, builder, null);
    } else if (token == XContentParser.Token.VALUE_STRING) {
        builder.setGeoPoint(GeoPoint.fromGeohash(parser.text()));
    } else {
        throw new ElasticsearchParseException("geo context must be an object or string");
    }
    return builder.build();
}
Also used : XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 68 with ElasticsearchParseException

use of org.elasticsearch.ElasticsearchParseException in project elasticsearch by elastic.

the class SnapshotInfo method fromXContent.

/**
     * This method creates a SnapshotInfo from internal x-content.  It does not
     * handle x-content written with the external version as external x-content
     * is only for display purposes and does not need to be parsed.
     */
public static SnapshotInfo fromXContent(final XContentParser parser) throws IOException {
    String name = null;
    String uuid = null;
    Version version = Version.CURRENT;
    SnapshotState state = SnapshotState.IN_PROGRESS;
    String reason = null;
    List<String> indices = Collections.emptyList();
    long startTime = 0;
    long endTime = 0;
    int totalShards = 0;
    int successfulShards = 0;
    List<SnapshotShardFailure> shardFailures = Collections.emptyList();
    if (parser.currentToken() == null) {
        // fresh parser? move to the first token
        parser.nextToken();
    }
    if (parser.currentToken() == XContentParser.Token.START_OBJECT) {
        // on a start object move to next token
        parser.nextToken();
    }
    XContentParser.Token token;
    if ((token = parser.nextToken()) == XContentParser.Token.START_OBJECT) {
        String currentFieldName = parser.currentName();
        if (SNAPSHOT.equals(currentFieldName)) {
            while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                if (token == XContentParser.Token.FIELD_NAME) {
                    currentFieldName = parser.currentName();
                    token = parser.nextToken();
                    if (token.isValue()) {
                        if (NAME.equals(currentFieldName)) {
                            name = parser.text();
                        } else if (UUID.equals(currentFieldName)) {
                            uuid = parser.text();
                        } else if (STATE.equals(currentFieldName)) {
                            state = SnapshotState.valueOf(parser.text());
                        } else if (REASON.equals(currentFieldName)) {
                            reason = parser.text();
                        } else if (START_TIME.equals(currentFieldName)) {
                            startTime = parser.longValue();
                        } else if (END_TIME.equals(currentFieldName)) {
                            endTime = parser.longValue();
                        } else if (TOTAL_SHARDS.equals(currentFieldName)) {
                            totalShards = parser.intValue();
                        } else if (SUCCESSFUL_SHARDS.equals(currentFieldName)) {
                            successfulShards = parser.intValue();
                        } else if (VERSION_ID.equals(currentFieldName)) {
                            version = Version.fromId(parser.intValue());
                        }
                    } else if (token == XContentParser.Token.START_ARRAY) {
                        if (INDICES.equals(currentFieldName)) {
                            ArrayList<String> indicesArray = new ArrayList<>();
                            while (parser.nextToken() != XContentParser.Token.END_ARRAY) {
                                indicesArray.add(parser.text());
                            }
                            indices = Collections.unmodifiableList(indicesArray);
                        } else if (FAILURES.equals(currentFieldName)) {
                            ArrayList<SnapshotShardFailure> shardFailureArrayList = new ArrayList<>();
                            while (parser.nextToken() != XContentParser.Token.END_ARRAY) {
                                shardFailureArrayList.add(SnapshotShardFailure.fromXContent(parser));
                            }
                            shardFailures = Collections.unmodifiableList(shardFailureArrayList);
                        } else {
                            // It was probably created by newer version - ignoring
                            parser.skipChildren();
                        }
                    } else if (token == XContentParser.Token.START_OBJECT) {
                        // It was probably created by newer version - ignoring
                        parser.skipChildren();
                    }
                }
            }
        }
    } else {
        throw new ElasticsearchParseException("unexpected token  [" + token + "]");
    }
    if (uuid == null) {
        // the old format where there wasn't a UUID
        uuid = name;
    }
    return new SnapshotInfo(new SnapshotId(name, uuid), indices, state, reason, version, startTime, endTime, totalShards, successfulShards, shardFailures);
}
Also used : ArrayList(java.util.ArrayList) Version(org.elasticsearch.Version) ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 69 with ElasticsearchParseException

use of org.elasticsearch.ElasticsearchParseException in project elasticsearch by elastic.

the class PhraseSuggestionBuilder method fromXContent.

public static PhraseSuggestionBuilder fromXContent(XContentParser parser) throws IOException {
    PhraseSuggestionBuilder tmpSuggestion = new PhraseSuggestionBuilder("_na_");
    XContentParser.Token token;
    String currentFieldName = null;
    String fieldname = null;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (token.isValue()) {
            if (SuggestionBuilder.ANALYZER_FIELD.match(currentFieldName)) {
                tmpSuggestion.analyzer(parser.text());
            } else if (SuggestionBuilder.FIELDNAME_FIELD.match(currentFieldName)) {
                fieldname = parser.text();
            } else if (SuggestionBuilder.SIZE_FIELD.match(currentFieldName)) {
                tmpSuggestion.size(parser.intValue());
            } else if (SuggestionBuilder.SHARDSIZE_FIELD.match(currentFieldName)) {
                tmpSuggestion.shardSize(parser.intValue());
            } else if (PhraseSuggestionBuilder.RWE_LIKELIHOOD_FIELD.match(currentFieldName)) {
                tmpSuggestion.realWordErrorLikelihood(parser.floatValue());
            } else if (PhraseSuggestionBuilder.CONFIDENCE_FIELD.match(currentFieldName)) {
                tmpSuggestion.confidence(parser.floatValue());
            } else if (PhraseSuggestionBuilder.SEPARATOR_FIELD.match(currentFieldName)) {
                tmpSuggestion.separator(parser.text());
            } else if (PhraseSuggestionBuilder.MAXERRORS_FIELD.match(currentFieldName)) {
                tmpSuggestion.maxErrors(parser.floatValue());
            } else if (PhraseSuggestionBuilder.GRAMSIZE_FIELD.match(currentFieldName)) {
                tmpSuggestion.gramSize(parser.intValue());
            } else if (PhraseSuggestionBuilder.FORCE_UNIGRAM_FIELD.match(currentFieldName)) {
                tmpSuggestion.forceUnigrams(parser.booleanValue());
            } else if (PhraseSuggestionBuilder.TOKEN_LIMIT_FIELD.match(currentFieldName)) {
                tmpSuggestion.tokenLimit(parser.intValue());
            } else {
                throw new ParsingException(parser.getTokenLocation(), "suggester[phrase] doesn't support field [" + currentFieldName + "]");
            }
        } else if (token == Token.START_ARRAY) {
            if (DirectCandidateGeneratorBuilder.DIRECT_GENERATOR_FIELD.match(currentFieldName)) {
                // for now we only have a single type of generators
                while ((token = parser.nextToken()) == Token.START_OBJECT) {
                    tmpSuggestion.addCandidateGenerator(DirectCandidateGeneratorBuilder.PARSER.apply(parser, null));
                }
            } else {
                throw new ParsingException(parser.getTokenLocation(), "suggester[phrase]  doesn't support array field [" + currentFieldName + "]");
            }
        } else if (token == Token.START_OBJECT) {
            if (PhraseSuggestionBuilder.SMOOTHING_MODEL_FIELD.match(currentFieldName)) {
                ensureNoSmoothing(tmpSuggestion);
                tmpSuggestion.smoothingModel(SmoothingModel.fromXContent(parser));
            } else if (PhraseSuggestionBuilder.HIGHLIGHT_FIELD.match(currentFieldName)) {
                String preTag = null;
                String postTag = null;
                while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                    if (token == XContentParser.Token.FIELD_NAME) {
                        currentFieldName = parser.currentName();
                    } else if (token.isValue()) {
                        if (PhraseSuggestionBuilder.PRE_TAG_FIELD.match(currentFieldName)) {
                            preTag = parser.text();
                        } else if (PhraseSuggestionBuilder.POST_TAG_FIELD.match(currentFieldName)) {
                            postTag = parser.text();
                        } else {
                            throw new ParsingException(parser.getTokenLocation(), "suggester[phrase][highlight] doesn't support field [" + currentFieldName + "]");
                        }
                    }
                }
                tmpSuggestion.highlight(preTag, postTag);
            } else if (PhraseSuggestionBuilder.COLLATE_FIELD.match(currentFieldName)) {
                while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                    if (token == XContentParser.Token.FIELD_NAME) {
                        currentFieldName = parser.currentName();
                    } else if (PhraseSuggestionBuilder.COLLATE_QUERY_FIELD.match(currentFieldName)) {
                        if (tmpSuggestion.collateQuery() != null) {
                            throw new ParsingException(parser.getTokenLocation(), "suggester[phrase][collate] query already set, doesn't support additional [" + currentFieldName + "]");
                        }
                        Script template = Script.parse(parser, Script.DEFAULT_TEMPLATE_LANG);
                        tmpSuggestion.collateQuery(template);
                    } else if (PhraseSuggestionBuilder.COLLATE_QUERY_PARAMS.match(currentFieldName)) {
                        tmpSuggestion.collateParams(parser.map());
                    } else if (PhraseSuggestionBuilder.COLLATE_QUERY_PRUNE.match(currentFieldName)) {
                        if (parser.isBooleanValue()) {
                            tmpSuggestion.collatePrune(parser.booleanValue());
                        } else {
                            throw new ParsingException(parser.getTokenLocation(), "suggester[phrase][collate] prune must be either 'true' or 'false'");
                        }
                    } else {
                        throw new ParsingException(parser.getTokenLocation(), "suggester[phrase][collate] doesn't support field [" + currentFieldName + "]");
                    }
                }
            } else {
                throw new ParsingException(parser.getTokenLocation(), "suggester[phrase]  doesn't support array field [" + currentFieldName + "]");
            }
        } else {
            throw new ParsingException(parser.getTokenLocation(), "suggester[phrase] doesn't support field [" + currentFieldName + "]");
        }
    }
    // now we should have field name, check and copy fields over to the suggestion builder we return
    if (fieldname == null) {
        throw new ElasticsearchParseException("the required field option [" + FIELDNAME_FIELD.getPreferredName() + "] is missing");
    }
    return new PhraseSuggestionBuilder(fieldname, tmpSuggestion);
}
Also used : Token(org.elasticsearch.common.xcontent.XContentParser.Token) Script(org.elasticsearch.script.Script) ExecutableScript(org.elasticsearch.script.ExecutableScript) ParsingException(org.elasticsearch.common.ParsingException) ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 70 with ElasticsearchParseException

use of org.elasticsearch.ElasticsearchParseException in project elasticsearch by elastic.

the class IngestClientIT method testPutWithPipelineFactoryError.

public void testPutWithPipelineFactoryError() throws Exception {
    BytesReference source = jsonBuilder().startObject().field("description", "my_pipeline").startArray("processors").startObject().startObject("test").field("unused", ":sad_face:").endObject().endObject().endArray().endObject().bytes();
    PutPipelineRequest putPipelineRequest = new PutPipelineRequest("_id", source, XContentType.JSON);
    try {
        client().admin().cluster().putPipeline(putPipelineRequest).get();
    } catch (ExecutionException e) {
        ElasticsearchParseException ex = (ElasticsearchParseException) ExceptionsHelper.unwrap(e, ElasticsearchParseException.class);
        assertNotNull(ex);
        assertThat(ex.getMessage(), equalTo("processor [test] doesn't support one or more provided configuration parameters [unused]"));
    }
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) PutPipelineRequest(org.elasticsearch.action.ingest.PutPipelineRequest) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

ElasticsearchParseException (org.elasticsearch.ElasticsearchParseException)106 XContentParser (org.elasticsearch.common.xcontent.XContentParser)44 HashMap (java.util.HashMap)28 ArrayList (java.util.ArrayList)18 IOException (java.io.IOException)13 Map (java.util.Map)11 List (java.util.List)7 GeoPoint (org.elasticsearch.common.geo.GeoPoint)7 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)6 Matchers.containsString (org.hamcrest.Matchers.containsString)6 ParsingException (org.elasticsearch.common.ParsingException)5 HashSet (java.util.HashSet)4 Set (java.util.Set)4 ElasticsearchTimeoutException (org.elasticsearch.ElasticsearchTimeoutException)4 Version (org.elasticsearch.Version)4 NoNodeAvailableException (org.elasticsearch.client.transport.NoNodeAvailableException)4 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)4 UncheckedIOException (java.io.UncheckedIOException)3 LinkedHashSet (java.util.LinkedHashSet)3 PutPipelineRequest (org.elasticsearch.action.ingest.PutPipelineRequest)3