Search in sources :

Example 51 with ParsingException

use of org.elasticsearch.common.ParsingException in project elasticsearch by elastic.

the class AggregatorFactories method parseAggregators.

private static AggregatorFactories.Builder parseAggregators(QueryParseContext parseContext, int level) throws IOException {
    Matcher validAggMatcher = VALID_AGG_NAME.matcher("");
    AggregatorFactories.Builder factories = new AggregatorFactories.Builder();
    XContentParser.Token token = null;
    XContentParser parser = parseContext.parser();
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token != XContentParser.Token.FIELD_NAME) {
            throw new ParsingException(parser.getTokenLocation(), "Unexpected token " + token + " in [aggs]: aggregations definitions must start with the name of the aggregation.");
        }
        final String aggregationName = parser.currentName();
        if (!validAggMatcher.reset(aggregationName).matches()) {
            throw new ParsingException(parser.getTokenLocation(), "Invalid aggregation name [" + aggregationName + "]. Aggregation names must be alpha-numeric and can only contain '_' and '-'");
        }
        token = parser.nextToken();
        if (token != XContentParser.Token.START_OBJECT) {
            throw new ParsingException(parser.getTokenLocation(), "Aggregation definition for [" + aggregationName + " starts with a [" + token + "], expected a [" + XContentParser.Token.START_OBJECT + "].");
        }
        BaseAggregationBuilder aggBuilder = null;
        AggregatorFactories.Builder subFactories = null;
        Map<String, Object> metaData = null;
        while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
            if (token != XContentParser.Token.FIELD_NAME) {
                throw new ParsingException(parser.getTokenLocation(), "Expected [" + XContentParser.Token.FIELD_NAME + "] under a [" + XContentParser.Token.START_OBJECT + "], but got a [" + token + "] in [" + aggregationName + "]", parser.getTokenLocation());
            }
            final String fieldName = parser.currentName();
            token = parser.nextToken();
            if (token == XContentParser.Token.START_OBJECT) {
                switch(fieldName) {
                    case "meta":
                        metaData = parser.map();
                        break;
                    case "aggregations":
                    case "aggs":
                        if (subFactories != null) {
                            throw new ParsingException(parser.getTokenLocation(), "Found two sub aggregation definitions under [" + aggregationName + "]");
                        }
                        subFactories = parseAggregators(parseContext, level + 1);
                        break;
                    default:
                        if (aggBuilder != null) {
                            throw new ParsingException(parser.getTokenLocation(), "Found two aggregation type definitions in [" + aggregationName + "]: [" + aggBuilder.getType() + "] and [" + fieldName + "]");
                        }
                        aggBuilder = parser.namedObject(BaseAggregationBuilder.class, fieldName, new AggParseContext(aggregationName, parseContext));
                }
            } else {
                throw new ParsingException(parser.getTokenLocation(), "Expected [" + XContentParser.Token.START_OBJECT + "] under [" + fieldName + "], but got a [" + token + "] in [" + aggregationName + "]");
            }
        }
        if (aggBuilder == null) {
            throw new ParsingException(parser.getTokenLocation(), "Missing definition for aggregation [" + aggregationName + "]", parser.getTokenLocation());
        } else {
            if (metaData != null) {
                aggBuilder.setMetaData(metaData);
            }
            if (subFactories != null) {
                aggBuilder.subAggregations(subFactories);
            }
            if (aggBuilder instanceof AggregationBuilder) {
                factories.addAggregator((AggregationBuilder) aggBuilder);
            } else {
                factories.addPipelineAggregator((PipelineAggregationBuilder) aggBuilder);
            }
        }
    }
    return factories;
}
Also used : Matcher(java.util.regex.Matcher) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) ParsingException(org.elasticsearch.common.ParsingException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 52 with ParsingException

use of org.elasticsearch.common.ParsingException in project elasticsearch by elastic.

the class ElasticsearchExceptionTests method testFailureToAndFromXContentWithDetails.

public void testFailureToAndFromXContentWithDetails() throws IOException {
    final XContent xContent = randomFrom(XContentType.values()).xContent();
    Exception failure;
    Throwable failureCause;
    ElasticsearchException expected;
    ElasticsearchException expectedCause;
    ElasticsearchException suppressed;
    switch(randomIntBetween(0, 6)) {
        case // Simple elasticsearch exception without cause
        0:
            failure = new NoNodeAvailableException("A");
            expected = new ElasticsearchException("Elasticsearch exception [type=no_node_available_exception, reason=A]");
            expected.addSuppressed(new ElasticsearchException("Elasticsearch exception [type=no_node_available_exception, reason=A]"));
            break;
        case // Simple elasticsearch exception with headers (other metadata of type number are not parsed)
        1:
            failure = new CircuitBreakingException("B", 5_000, 2_000);
            ((ElasticsearchException) failure).addHeader("header_name", "0", "1");
            expected = new ElasticsearchException("Elasticsearch exception [type=circuit_breaking_exception, reason=B]");
            expected.addHeader("header_name", "0", "1");
            suppressed = new ElasticsearchException("Elasticsearch exception [type=circuit_breaking_exception, reason=B]");
            suppressed.addHeader("header_name", "0", "1");
            expected.addSuppressed(suppressed);
            break;
        case // Elasticsearch exception with a cause, headers and parsable metadata
        2:
            failureCause = new NullPointerException("var is null");
            failure = new ScriptException("C", failureCause, singletonList("stack"), "test", "painless");
            ((ElasticsearchException) failure).addHeader("script_name", "my_script");
            expectedCause = new ElasticsearchException("Elasticsearch exception [type=null_pointer_exception, reason=var is null]");
            expected = new ElasticsearchException("Elasticsearch exception [type=script_exception, reason=C]", expectedCause);
            expected.addHeader("script_name", "my_script");
            expected.addMetadata("es.lang", "painless");
            expected.addMetadata("es.script", "test");
            expected.addMetadata("es.script_stack", "stack");
            suppressed = new ElasticsearchException("Elasticsearch exception [type=script_exception, reason=C]");
            suppressed.addHeader("script_name", "my_script");
            suppressed.addMetadata("es.lang", "painless");
            suppressed.addMetadata("es.script", "test");
            suppressed.addMetadata("es.script_stack", "stack");
            expected.addSuppressed(suppressed);
            break;
        case // JDK exception without cause
        3:
            failure = new IllegalStateException("D");
            expected = new ElasticsearchException("Elasticsearch exception [type=illegal_state_exception, reason=D]");
            suppressed = new ElasticsearchException("Elasticsearch exception [type=illegal_state_exception, reason=D]");
            expected.addSuppressed(suppressed);
            break;
        case // JDK exception with cause
        4:
            failureCause = new RoutingMissingException("idx", "type", "id");
            failure = new RuntimeException("E", failureCause);
            expectedCause = new ElasticsearchException("Elasticsearch exception [type=routing_missing_exception, " + "reason=routing is required for [idx]/[type]/[id]]");
            expectedCause.addMetadata("es.index", "idx");
            expectedCause.addMetadata("es.index_uuid", "_na_");
            expected = new ElasticsearchException("Elasticsearch exception [type=runtime_exception, reason=E]", expectedCause);
            suppressed = new ElasticsearchException("Elasticsearch exception [type=runtime_exception, reason=E]");
            expected.addSuppressed(suppressed);
            break;
        case // Wrapped exception with cause
        5:
            failureCause = new FileAlreadyExistsException("File exists");
            failure = new BroadcastShardOperationFailedException(new ShardId("_index", "_uuid", 5), "F", failureCause);
            expected = new ElasticsearchException("Elasticsearch exception [type=file_already_exists_exception, reason=File exists]");
            // strangely, the wrapped exception appears as the root cause...
            suppressed = new ElasticsearchException("Elasticsearch exception [type=broadcast_shard_operation_failed_exception, " + "reason=F]");
            expected.addSuppressed(suppressed);
            break;
        case // SearchPhaseExecutionException with cause and multiple failures
        6:
            DiscoveryNode node = new DiscoveryNode("node_g", buildNewFakeTransportAddress(), Version.CURRENT);
            failureCause = new NodeClosedException(node);
            failureCause = new NoShardAvailableActionException(new ShardId("_index_g", "_uuid_g", 6), "node_g", failureCause);
            ShardSearchFailure[] shardFailures = new ShardSearchFailure[] { new ShardSearchFailure(new ParsingException(0, 0, "Parsing g", null), new SearchShardTarget("node_g", new ShardId(new Index("_index_g", "_uuid_g"), 61))), new ShardSearchFailure(new RepositoryException("repository_g", "Repo"), new SearchShardTarget("node_g", new ShardId(new Index("_index_g", "_uuid_g"), 62))), new ShardSearchFailure(new SearchContextMissingException(0L), null) };
            failure = new SearchPhaseExecutionException("phase_g", "G", failureCause, shardFailures);
            expectedCause = new ElasticsearchException("Elasticsearch exception [type=node_closed_exception, " + "reason=node closed " + node + "]");
            expectedCause = new ElasticsearchException("Elasticsearch exception [type=no_shard_available_action_exception, " + "reason=node_g]", expectedCause);
            expectedCause.addMetadata("es.index", "_index_g");
            expectedCause.addMetadata("es.index_uuid", "_uuid_g");
            expectedCause.addMetadata("es.shard", "6");
            expected = new ElasticsearchException("Elasticsearch exception [type=search_phase_execution_exception, " + "reason=G]", expectedCause);
            expected.addMetadata("es.phase", "phase_g");
            expected.addSuppressed(new ElasticsearchException("Elasticsearch exception [type=parsing_exception, reason=Parsing g]"));
            expected.addSuppressed(new ElasticsearchException("Elasticsearch exception [type=repository_exception, " + "reason=[repository_g] Repo]"));
            expected.addSuppressed(new ElasticsearchException("Elasticsearch exception [type=search_context_missing_exception, " + "reason=No search context found for id [0]]"));
            break;
        default:
            throw new UnsupportedOperationException("Failed to generate randomized failure");
    }
    Exception finalFailure = failure;
    BytesReference failureBytes = XContentHelper.toXContent((builder, params) -> {
        ElasticsearchException.generateFailureXContent(builder, params, finalFailure, true);
        return builder;
    }, xContent.type(), randomBoolean());
    try (XContentParser parser = createParser(xContent, failureBytes)) {
        failureBytes = shuffleXContent(parser, randomBoolean()).bytes();
    }
    ElasticsearchException parsedFailure;
    try (XContentParser parser = createParser(xContent, failureBytes)) {
        assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
        assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken());
        parsedFailure = ElasticsearchException.failureFromXContent(parser);
        assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken());
        assertNull(parser.nextToken());
    }
    assertDeepEquals(expected, parsedFailure);
}
Also used : FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) SearchPhaseExecutionException(org.elasticsearch.action.search.SearchPhaseExecutionException) Index(org.elasticsearch.index.Index) ShardId(org.elasticsearch.index.shard.ShardId) ScriptException(org.elasticsearch.script.ScriptException) XContent(org.elasticsearch.common.xcontent.XContent) ToXContent(org.elasticsearch.common.xcontent.ToXContent) ParsingException(org.elasticsearch.common.ParsingException) NodeClosedException(org.elasticsearch.node.NodeClosedException) BroadcastShardOperationFailedException(org.elasticsearch.action.support.broadcast.BroadcastShardOperationFailedException) ShardSearchFailure(org.elasticsearch.action.search.ShardSearchFailure) RoutingMissingException(org.elasticsearch.action.RoutingMissingException) BytesReference(org.elasticsearch.common.bytes.BytesReference) SearchContextMissingException(org.elasticsearch.search.SearchContextMissingException) RepositoryException(org.elasticsearch.repositories.RepositoryException) NoNodeAvailableException(org.elasticsearch.client.transport.NoNodeAvailableException) SearchParseException(org.elasticsearch.search.SearchParseException) NodeClosedException(org.elasticsearch.node.NodeClosedException) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) RoutingMissingException(org.elasticsearch.action.RoutingMissingException) BroadcastShardOperationFailedException(org.elasticsearch.action.support.broadcast.BroadcastShardOperationFailedException) RepositoryException(org.elasticsearch.repositories.RepositoryException) QueryShardException(org.elasticsearch.index.query.QueryShardException) SearchContextMissingException(org.elasticsearch.search.SearchContextMissingException) ScriptException(org.elasticsearch.script.ScriptException) NoNodeAvailableException(org.elasticsearch.client.transport.NoNodeAvailableException) EOFException(java.io.EOFException) FileNotFoundException(java.io.FileNotFoundException) SearchPhaseExecutionException(org.elasticsearch.action.search.SearchPhaseExecutionException) RemoteTransportException(org.elasticsearch.transport.RemoteTransportException) ParsingException(org.elasticsearch.common.ParsingException) IndexShardRecoveringException(org.elasticsearch.index.shard.IndexShardRecoveringException) ClusterBlockException(org.elasticsearch.cluster.block.ClusterBlockException) NoShardAvailableActionException(org.elasticsearch.action.NoShardAvailableActionException) IOException(java.io.IOException) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) CircuitBreakingException(org.elasticsearch.common.breaker.CircuitBreakingException) NoShardAvailableActionException(org.elasticsearch.action.NoShardAvailableActionException) CircuitBreakingException(org.elasticsearch.common.breaker.CircuitBreakingException) SearchShardTarget(org.elasticsearch.search.SearchShardTarget) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 53 with ParsingException

use of org.elasticsearch.common.ParsingException in project elasticsearch by elastic.

the class ExceptionSerializationTests method testUnknownException.

public void testUnknownException() throws IOException {
    ParsingException parsingException = new ParsingException(1, 2, "foobar", null);
    final Exception ex = new UnknownException("eggplant", parsingException);
    Exception exception = serialize(ex);
    assertEquals("unknown_exception: eggplant", exception.getMessage());
    assertTrue(exception instanceof ElasticsearchException);
    ParsingException e = (ParsingException) exception.getCause();
    assertEquals(parsingException.getIndex(), e.getIndex());
    assertEquals(parsingException.getMessage(), e.getMessage());
    assertEquals(parsingException.getLineNumber(), e.getLineNumber());
    assertEquals(parsingException.getColumnNumber(), e.getColumnNumber());
}
Also used : TimestampParsingException(org.elasticsearch.action.TimestampParsingException) ParsingException(org.elasticsearch.common.ParsingException) IndexFormatTooNewException(org.apache.lucene.index.IndexFormatTooNewException) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) ConnectTransportException(org.elasticsearch.transport.ConnectTransportException) IllegalShardRoutingStateException(org.elasticsearch.cluster.routing.IllegalShardRoutingStateException) ActionTransportException(org.elasticsearch.transport.ActionTransportException) SearchContextMissingException(org.elasticsearch.search.SearchContextMissingException) SnapshotException(org.elasticsearch.snapshots.SnapshotException) AccessDeniedException(java.nio.file.AccessDeniedException) SearchException(org.elasticsearch.search.SearchException) LockObtainFailedException(org.apache.lucene.store.LockObtainFailedException) RecoverFilesRecoveryException(org.elasticsearch.indices.recovery.RecoverFilesRecoveryException) ClusterBlockException(org.elasticsearch.cluster.block.ClusterBlockException) NotDirectoryException(java.nio.file.NotDirectoryException) DirectoryNotEmptyException(java.nio.file.DirectoryNotEmptyException) IOException(java.io.IOException) IndexTemplateMissingException(org.elasticsearch.indices.IndexTemplateMissingException) NoSuchFileException(java.nio.file.NoSuchFileException) FailedNodeException(org.elasticsearch.action.FailedNodeException) SearchParseException(org.elasticsearch.search.SearchParseException) URISyntaxException(java.net.URISyntaxException) AliasesNotFoundException(org.elasticsearch.rest.action.admin.indices.AliasesNotFoundException) RecoveryEngineException(org.elasticsearch.index.engine.RecoveryEngineException) CorruptIndexException(org.apache.lucene.index.CorruptIndexException) TimestampParsingException(org.elasticsearch.action.TimestampParsingException) RoutingMissingException(org.elasticsearch.action.RoutingMissingException) RepositoryException(org.elasticsearch.repositories.RepositoryException) AlreadyExpiredException(org.elasticsearch.index.AlreadyExpiredException) InvalidIndexTemplateException(org.elasticsearch.indices.InvalidIndexTemplateException) QueryShardException(org.elasticsearch.index.query.QueryShardException) FileSystemException(java.nio.file.FileSystemException) ShardLockObtainFailedException(org.elasticsearch.env.ShardLockObtainFailedException) IllegalIndexShardStateException(org.elasticsearch.index.shard.IllegalIndexShardStateException) EOFException(java.io.EOFException) FileNotFoundException(java.io.FileNotFoundException) SearchPhaseExecutionException(org.elasticsearch.action.search.SearchPhaseExecutionException) ActionNotFoundTransportException(org.elasticsearch.transport.ActionNotFoundTransportException) ParsingException(org.elasticsearch.common.ParsingException) FileSystemLoopException(java.nio.file.FileSystemLoopException) IndexFormatTooOldException(org.apache.lucene.index.IndexFormatTooOldException) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) CircuitBreakingException(org.elasticsearch.common.breaker.CircuitBreakingException) AtomicMoveNotSupportedException(java.nio.file.AtomicMoveNotSupportedException)

Example 54 with ParsingException

use of org.elasticsearch.common.ParsingException in project elasticsearch by elastic.

the class SuggestionTests method testUnknownSuggestionTypeThrows.

public void testUnknownSuggestionTypeThrows() throws IOException {
    XContent xContent = JsonXContent.jsonXContent;
    String suggestionString = "{\"unknownType#suggestionName\":" + "[{\"text\":\"entryText\"," + "\"offset\":42," + "\"length\":313," + "\"options\":[{\"text\":\"someText\"," + "\"highlighted\":\"somethingHighlighted\"," + "\"score\":1.3," + "\"collate_match\":true}]" + "}]" + "}";
    try (XContentParser parser = xContent.createParser(xContentRegistry(), suggestionString)) {
        ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation);
        ensureExpectedToken(XContentParser.Token.FIELD_NAME, parser.nextToken(), parser::getTokenLocation);
        ParsingException e = expectThrows(ParsingException.class, () -> Suggestion.fromXContent(parser));
        assertEquals("Unknown Suggestion [unknownType]", e.getMessage());
    }
}
Also used : XContent(org.elasticsearch.common.xcontent.XContent) JsonXContent(org.elasticsearch.common.xcontent.json.JsonXContent) ToXContent(org.elasticsearch.common.xcontent.ToXContent) XContentHelper.toXContent(org.elasticsearch.common.xcontent.XContentHelper.toXContent) ParsingException(org.elasticsearch.common.ParsingException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 55 with ParsingException

use of org.elasticsearch.common.ParsingException in project elasticsearch by elastic.

the class SearchTemplateIT method testIndexedTemplateOverwrite.

// Relates to #10397
public void testIndexedTemplateOverwrite() throws Exception {
    createIndex("testindex");
    ensureGreen("testindex");
    client().prepareIndex("testindex", "test", "1").setSource(jsonBuilder().startObject().field("searchtext", "dev1").endObject()).get();
    client().admin().indices().prepareRefresh().get();
    int iterations = randomIntBetween(2, 11);
    for (int i = 1; i < iterations; i++) {
        assertAcked(client().admin().cluster().preparePutStoredScript().setLang(MustacheScriptEngineService.NAME).setId("git01").setContent(new BytesArray("{\"template\":{\"query\": {\"match\": {\"searchtext\": {\"query\": \"{{P_Keyword1}}\"," + "\"type\": \"ooophrase_prefix\"}}}}}"), XContentType.JSON));
        GetStoredScriptResponse getResponse = client().admin().cluster().prepareGetStoredScript(MustacheScriptEngineService.NAME, "git01").get();
        assertNotNull(getResponse.getSource());
        Map<String, Object> templateParams = new HashMap<>();
        templateParams.put("P_Keyword1", "dev");
        ParsingException e = expectThrows(ParsingException.class, () -> new SearchTemplateRequestBuilder(client()).setRequest(new SearchRequest("testindex").types("test")).setScript("git01").setScriptType(ScriptType.STORED).setScriptParams(templateParams).get());
        assertThat(e.getMessage(), containsString("[match] query does not support type ooophrase_prefix"));
        assertWarnings("Deprecated field [type] used, replaced by [match_phrase and match_phrase_prefix query]");
        assertAcked(client().admin().cluster().preparePutStoredScript().setLang(MustacheScriptEngineService.NAME).setId("git01").setContent(new BytesArray("{\"query\": {\"match\": {\"searchtext\": {\"query\": \"{{P_Keyword1}}\"," + "\"type\": \"phrase_prefix\"}}}}"), XContentType.JSON));
        SearchTemplateResponse searchResponse = new SearchTemplateRequestBuilder(client()).setRequest(new SearchRequest("testindex").types("test")).setScript("git01").setScriptType(ScriptType.STORED).setScriptParams(templateParams).get();
        assertHitCount(searchResponse.getResponse(), 1);
        assertWarnings("Deprecated field [type] used, replaced by [match_phrase and match_phrase_prefix query]");
    }
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) BytesArray(org.elasticsearch.common.bytes.BytesArray) HashMap(java.util.HashMap) ParsingException(org.elasticsearch.common.ParsingException) Matchers.containsString(org.hamcrest.Matchers.containsString) GetStoredScriptResponse(org.elasticsearch.action.admin.cluster.storedscripts.GetStoredScriptResponse)

Aggregations

ParsingException (org.elasticsearch.common.ParsingException)165 XContentParser (org.elasticsearch.common.xcontent.XContentParser)96 ArrayList (java.util.ArrayList)26 Matchers.containsString (org.hamcrest.Matchers.containsString)22 IOException (java.io.IOException)12 HashMap (java.util.HashMap)10 Token (org.elasticsearch.common.xcontent.XContentParser.Token)9 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)7 Index (org.elasticsearch.index.Index)7 Script (org.elasticsearch.script.Script)7 SearchShardTarget (org.elasticsearch.search.SearchShardTarget)7 List (java.util.List)6 BytesReference (org.elasticsearch.common.bytes.BytesReference)6 QueryParseContext (org.elasticsearch.index.query.QueryParseContext)6 ElasticsearchParseException (org.elasticsearch.ElasticsearchParseException)5 GeoPoint (org.elasticsearch.common.geo.GeoPoint)5 GapPolicy (org.elasticsearch.search.aggregations.pipeline.BucketHelpers.GapPolicy)5 SearchPhaseExecutionException (org.elasticsearch.action.search.SearchPhaseExecutionException)4 ToXContent (org.elasticsearch.common.xcontent.ToXContent)4 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)4