Search in sources :

Example 1 with SearchParseException

use of org.elasticsearch.search.SearchParseException in project elasticsearch by elastic.

the class ExtendedBoundsTests method testParseAndValidate.

public void testParseAndValidate() {
    long now = randomLong();
    Settings indexSettings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT).put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 1).build();
    SearchContext context = mock(SearchContext.class);
    QueryShardContext qsc = new QueryShardContext(0, new IndexSettings(IndexMetaData.builder("foo").settings(indexSettings).build(), indexSettings), null, null, null, null, null, xContentRegistry(), null, null, () -> now);
    when(context.getQueryShardContext()).thenReturn(qsc);
    FormatDateTimeFormatter formatter = Joda.forPattern("dateOptionalTime");
    DocValueFormat format = new DocValueFormat.DateTime(formatter, DateTimeZone.UTC);
    ExtendedBounds expected = randomParsedExtendedBounds();
    ExtendedBounds parsed = unparsed(expected).parseAndValidate("test", context, format);
    // parsed won't *equal* expected because equal includes the String parts
    assertEquals(expected.getMin(), parsed.getMin());
    assertEquals(expected.getMax(), parsed.getMax());
    parsed = new ExtendedBounds("now", null).parseAndValidate("test", context, format);
    assertEquals(now, (long) parsed.getMin());
    assertNull(parsed.getMax());
    parsed = new ExtendedBounds(null, "now").parseAndValidate("test", context, format);
    assertNull(parsed.getMin());
    assertEquals(now, (long) parsed.getMax());
    SearchParseException e = expectThrows(SearchParseException.class, () -> new ExtendedBounds(100L, 90L).parseAndValidate("test", context, format));
    assertEquals("[extended_bounds.min][100] cannot be greater than [extended_bounds.max][90] for histogram aggregation [test]", e.getMessage());
    e = expectThrows(SearchParseException.class, () -> unparsed(new ExtendedBounds(100L, 90L)).parseAndValidate("test", context, format));
    assertEquals("[extended_bounds.min][100] cannot be greater than [extended_bounds.max][90] for histogram aggregation [test]", e.getMessage());
}
Also used : SearchParseException(org.elasticsearch.search.SearchParseException) FormatDateTimeFormatter(org.elasticsearch.common.joda.FormatDateTimeFormatter) DocValueFormat(org.elasticsearch.search.DocValueFormat) IndexSettings(org.elasticsearch.index.IndexSettings) SearchContext(org.elasticsearch.search.internal.SearchContext) QueryShardContext(org.elasticsearch.index.query.QueryShardContext) Settings(org.elasticsearch.common.settings.Settings) IndexSettings(org.elasticsearch.index.IndexSettings)

Example 2 with SearchParseException

use of org.elasticsearch.search.SearchParseException in project elasticsearch by elastic.

the class ExceptionSerializationTests method testSearchParseException.

public void testSearchParseException() throws IOException {
    SearchContext ctx = new TestSearchContext(null);
    SearchParseException ex = serialize(new SearchParseException(ctx, "foo", new XContentLocation(66, 666)));
    assertEquals("foo", ex.getMessage());
    assertEquals(66, ex.getLineNumber());
    assertEquals(666, ex.getColumnNumber());
    assertEquals(ctx.shardTarget(), ex.shard());
}
Also used : TestSearchContext(org.elasticsearch.test.TestSearchContext) SearchParseException(org.elasticsearch.search.SearchParseException) TestSearchContext(org.elasticsearch.test.TestSearchContext) SearchContext(org.elasticsearch.search.internal.SearchContext) XContentLocation(org.elasticsearch.common.xcontent.XContentLocation)

Example 3 with SearchParseException

use of org.elasticsearch.search.SearchParseException in project elasticsearch by elastic.

the class ElasticsearchExceptionTests method randomExceptions.

public static Tuple<Throwable, ElasticsearchException> randomExceptions() {
    Throwable actual;
    ElasticsearchException expected;
    int type = randomIntBetween(0, 5);
    switch(type) {
        case 0:
            actual = new ClusterBlockException(singleton(DiscoverySettings.NO_MASTER_BLOCK_WRITES));
            expected = new ElasticsearchException("Elasticsearch exception [type=cluster_block_exception, " + "reason=blocked by: [SERVICE_UNAVAILABLE/2/no master];]");
            break;
        case 1:
            actual = new CircuitBreakingException("Data too large", 123, 456);
            expected = new ElasticsearchException("Elasticsearch exception [type=circuit_breaking_exception, reason=Data too large]");
            break;
        case 2:
            actual = new SearchParseException(new TestSearchContext(null), "Parse failure", new XContentLocation(12, 98));
            expected = new ElasticsearchException("Elasticsearch exception [type=search_parse_exception, reason=Parse failure]");
            break;
        case 3:
            actual = new IllegalArgumentException("Closed resource", new RuntimeException("Resource"));
            expected = new ElasticsearchException("Elasticsearch exception [type=illegal_argument_exception, reason=Closed resource]", new ElasticsearchException("Elasticsearch exception [type=runtime_exception, reason=Resource]"));
            break;
        case 4:
            actual = new SearchPhaseExecutionException("search", "all shards failed", new ShardSearchFailure[] { new ShardSearchFailure(new ParsingException(1, 2, "foobar", null), new SearchShardTarget("node_1", new Index("foo", "_na_"), 1)) });
            expected = new ElasticsearchException("Elasticsearch exception [type=search_phase_execution_exception, " + "reason=all shards failed]");
            expected.addMetadata("es.phase", "search");
            break;
        case 5:
            actual = new ElasticsearchException("Parsing failed", new ParsingException(9, 42, "Wrong state", new NullPointerException("Unexpected null value")));
            ElasticsearchException expectedCause = new ElasticsearchException("Elasticsearch exception [type=parsing_exception, " + "reason=Wrong state]", new ElasticsearchException("Elasticsearch exception [type=null_pointer_exception, " + "reason=Unexpected null value]"));
            expected = new ElasticsearchException("Elasticsearch exception [type=exception, reason=Parsing failed]", expectedCause);
            break;
        default:
            throw new UnsupportedOperationException("No randomized exceptions generated for type [" + type + "]");
    }
    if (actual instanceof ElasticsearchException) {
        ElasticsearchException actualException = (ElasticsearchException) actual;
        if (randomBoolean()) {
            int nbHeaders = randomIntBetween(1, 5);
            Map<String, List<String>> randomHeaders = new HashMap<>(nbHeaders);
            for (int i = 0; i < nbHeaders; i++) {
                List<String> values = new ArrayList<>();
                int nbValues = randomIntBetween(1, 3);
                for (int j = 0; j < nbValues; j++) {
                    values.add(frequently() ? randomAsciiOfLength(5) : "");
                }
                randomHeaders.put("header_" + i, values);
            }
            for (Map.Entry<String, List<String>> entry : randomHeaders.entrySet()) {
                actualException.addHeader(entry.getKey(), entry.getValue());
                expected.addHeader(entry.getKey(), entry.getValue());
            }
            if (rarely()) {
                // Empty or null headers are not printed out by the toXContent method
                actualException.addHeader("ignored", randomBoolean() ? emptyList() : null);
            }
        }
        if (randomBoolean()) {
            int nbMetadata = randomIntBetween(1, 5);
            Map<String, List<String>> randomMetadata = new HashMap<>(nbMetadata);
            for (int i = 0; i < nbMetadata; i++) {
                List<String> values = new ArrayList<>();
                int nbValues = randomIntBetween(1, 3);
                for (int j = 0; j < nbValues; j++) {
                    values.add(frequently() ? randomAsciiOfLength(5) : "");
                }
                randomMetadata.put("es.metadata_" + i, values);
            }
            for (Map.Entry<String, List<String>> entry : randomMetadata.entrySet()) {
                actualException.addMetadata(entry.getKey(), entry.getValue());
                expected.addMetadata(entry.getKey(), entry.getValue());
            }
            if (rarely()) {
                // Empty or null metadata are not printed out by the toXContent method
                actualException.addMetadata("es.ignored", randomBoolean() ? emptyList() : null);
            }
        }
        if (randomBoolean()) {
            int nbResources = randomIntBetween(1, 5);
            for (int i = 0; i < nbResources; i++) {
                String resourceType = "type_" + i;
                String[] resourceIds = new String[randomIntBetween(1, 3)];
                for (int j = 0; j < resourceIds.length; j++) {
                    resourceIds[j] = frequently() ? randomAsciiOfLength(5) : "";
                }
                actualException.setResources(resourceType, resourceIds);
                expected.setResources(resourceType, resourceIds);
            }
        }
    }
    return new Tuple<>(actual, expected);
}
Also used : HashMap(java.util.HashMap) SearchPhaseExecutionException(org.elasticsearch.action.search.SearchPhaseExecutionException) ArrayList(java.util.ArrayList) Index(org.elasticsearch.index.Index) ParsingException(org.elasticsearch.common.ParsingException) Collections.singletonList(java.util.Collections.singletonList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) ArrayList(java.util.ArrayList) ShardSearchFailure(org.elasticsearch.action.search.ShardSearchFailure) XContentLocation(org.elasticsearch.common.xcontent.XContentLocation) ClusterBlockException(org.elasticsearch.cluster.block.ClusterBlockException) TestSearchContext(org.elasticsearch.test.TestSearchContext) SearchParseException(org.elasticsearch.search.SearchParseException) CircuitBreakingException(org.elasticsearch.common.breaker.CircuitBreakingException) SearchShardTarget(org.elasticsearch.search.SearchShardTarget) Map(java.util.Map) HashMap(java.util.HashMap) Tuple(org.elasticsearch.common.collect.Tuple)

Example 4 with SearchParseException

use of org.elasticsearch.search.SearchParseException in project elasticsearch by elastic.

the class ReverseNestedAggregationBuilder method doBuild.

@Override
protected AggregatorFactory<?> doBuild(SearchContext context, AggregatorFactory<?> parent, Builder subFactoriesBuilder) throws IOException {
    if (findNestedAggregatorFactory(parent) == null) {
        throw new SearchParseException(context, "Reverse nested aggregation [" + name + "] can only be used inside a [nested] aggregation", null);
    }
    ObjectMapper parentObjectMapper = null;
    if (path != null) {
        parentObjectMapper = context.getObjectMapper(path);
        if (parentObjectMapper == null) {
            return new ReverseNestedAggregatorFactory(name, true, null, context, parent, subFactoriesBuilder, metaData);
        }
        if (parentObjectMapper.nested().isNested() == false) {
            throw new AggregationExecutionException("[reverse_nested] nested path [" + path + "] is not nested");
        }
    }
    NestedScope nestedScope = context.getQueryShardContext().nestedScope();
    try {
        nestedScope.nextLevel(parentObjectMapper);
        return new ReverseNestedAggregatorFactory(name, false, parentObjectMapper, context, parent, subFactoriesBuilder, metaData);
    } finally {
        nestedScope.previousLevel();
    }
}
Also used : SearchParseException(org.elasticsearch.search.SearchParseException) NestedScope(org.elasticsearch.index.query.support.NestedScope) AggregationExecutionException(org.elasticsearch.search.aggregations.AggregationExecutionException) ObjectMapper(org.elasticsearch.index.mapper.ObjectMapper)

Aggregations

SearchParseException (org.elasticsearch.search.SearchParseException)4 XContentLocation (org.elasticsearch.common.xcontent.XContentLocation)2 SearchContext (org.elasticsearch.search.internal.SearchContext)2 TestSearchContext (org.elasticsearch.test.TestSearchContext)2 ArrayList (java.util.ArrayList)1 Collections.emptyList (java.util.Collections.emptyList)1 Collections.singletonList (java.util.Collections.singletonList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 SearchPhaseExecutionException (org.elasticsearch.action.search.SearchPhaseExecutionException)1 ShardSearchFailure (org.elasticsearch.action.search.ShardSearchFailure)1 ClusterBlockException (org.elasticsearch.cluster.block.ClusterBlockException)1 ParsingException (org.elasticsearch.common.ParsingException)1 CircuitBreakingException (org.elasticsearch.common.breaker.CircuitBreakingException)1 Tuple (org.elasticsearch.common.collect.Tuple)1 FormatDateTimeFormatter (org.elasticsearch.common.joda.FormatDateTimeFormatter)1 Settings (org.elasticsearch.common.settings.Settings)1 Index (org.elasticsearch.index.Index)1 IndexSettings (org.elasticsearch.index.IndexSettings)1