Search in sources :

Example 16 with ElasticSearchException

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

the class BytesRestResponseTests method testResponseWhenInternalServerError.

public void testResponseWhenInternalServerError() throws IOException {
    final RestRequest request = new FakeRestRequest();
    final RestChannel channel = new DetailedExceptionRestChannel(request);
    final BytesRestResponse response = new BytesRestResponse(channel, new ElasticsearchException("simulated"));
    assertNotNull(response.content());
    final String content = response.content().utf8ToString();
    assertThat(content, containsString("\"type\":\"exception\""));
    assertThat(content, containsString("\"reason\":\"simulated\""));
    assertThat(content, containsString("\"status\":" + 500));
}
Also used : FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest) ElasticsearchException(org.elasticsearch.ElasticsearchException) Matchers.containsString(org.hamcrest.Matchers.containsString) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest)

Example 17 with ElasticSearchException

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

the class TransportAnalyzeAction method shardOperation.

@Override
protected AnalyzeResponse shardOperation(AnalyzeRequest request, ShardId shardId) {
    try {
        final IndexService indexService;
        if (shardId != null) {
            indexService = indicesService.indexServiceSafe(shardId.getIndex());
        } else {
            indexService = null;
        }
        String field = null;
        Analyzer analyzer = null;
        if (request.field() != null) {
            if (indexService == null) {
                throw new IllegalArgumentException("No index provided, and trying to analyzer based on a specific field which requires the index parameter");
            }
            MappedFieldType fieldType = indexService.mapperService().fullName(request.field());
            if (fieldType != null) {
                if (fieldType.tokenized()) {
                    analyzer = fieldType.indexAnalyzer();
                } else if (fieldType instanceof KeywordFieldMapper.KeywordFieldType) {
                    analyzer = ((KeywordFieldMapper.KeywordFieldType) fieldType).normalizer();
                    if (analyzer == null) {
                        // this will be KeywordAnalyzer
                        analyzer = fieldType.indexAnalyzer();
                    }
                } else {
                    throw new IllegalArgumentException("Can't process field [" + request.field() + "], Analysis requests are only supported on tokenized fields");
                }
                field = fieldType.name();
            }
        }
        if (field == null) {
            if (indexService != null) {
                field = indexService.getIndexSettings().getDefaultField();
            } else {
                field = AllFieldMapper.NAME;
            }
        }
        final AnalysisRegistry analysisRegistry = indicesService.getAnalysis();
        return analyze(request, field, analyzer, indexService != null ? indexService.getIndexAnalyzers() : null, analysisRegistry, environment);
    } catch (IOException e) {
        throw new ElasticsearchException("analysis failed", e);
    }
}
Also used : AnalysisRegistry(org.elasticsearch.index.analysis.AnalysisRegistry) KeywordFieldMapper(org.elasticsearch.index.mapper.KeywordFieldMapper) IndexService(org.elasticsearch.index.IndexService) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) IOException(java.io.IOException) ElasticsearchException(org.elasticsearch.ElasticsearchException) NamedAnalyzer(org.elasticsearch.index.analysis.NamedAnalyzer) CustomAnalyzer(org.elasticsearch.index.analysis.CustomAnalyzer) Analyzer(org.apache.lucene.analysis.Analyzer)

Example 18 with ElasticSearchException

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

the class TransportAnalyzeAction method simpleAnalyze.

private static List<AnalyzeResponse.AnalyzeToken> simpleAnalyze(AnalyzeRequest request, Analyzer analyzer, String field) {
    List<AnalyzeResponse.AnalyzeToken> tokens = new ArrayList<>();
    int lastPosition = -1;
    int lastOffset = 0;
    for (String text : request.text()) {
        try (TokenStream stream = analyzer.tokenStream(field, text)) {
            stream.reset();
            CharTermAttribute term = stream.addAttribute(CharTermAttribute.class);
            PositionIncrementAttribute posIncr = stream.addAttribute(PositionIncrementAttribute.class);
            OffsetAttribute offset = stream.addAttribute(OffsetAttribute.class);
            TypeAttribute type = stream.addAttribute(TypeAttribute.class);
            PositionLengthAttribute posLen = stream.addAttribute(PositionLengthAttribute.class);
            while (stream.incrementToken()) {
                int increment = posIncr.getPositionIncrement();
                if (increment > 0) {
                    lastPosition = lastPosition + increment;
                }
                tokens.add(new AnalyzeResponse.AnalyzeToken(term.toString(), lastPosition, lastOffset + offset.startOffset(), lastOffset + offset.endOffset(), posLen.getPositionLength(), type.type(), null));
            }
            stream.end();
            lastOffset += offset.endOffset();
            lastPosition += posIncr.getPositionIncrement();
            lastPosition += analyzer.getPositionIncrementGap(field);
            lastOffset += analyzer.getOffsetGap(field);
        } catch (IOException e) {
            throw new ElasticsearchException("failed to analyze", e);
        }
    }
    return tokens;
}
Also used : PositionLengthAttribute(org.apache.lucene.analysis.tokenattributes.PositionLengthAttribute) TokenStream(org.apache.lucene.analysis.TokenStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ElasticsearchException(org.elasticsearch.ElasticsearchException) PositionIncrementAttribute(org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute) CharTermAttribute(org.apache.lucene.analysis.tokenattributes.CharTermAttribute) TypeAttribute(org.apache.lucene.analysis.tokenattributes.TypeAttribute) OffsetAttribute(org.apache.lucene.analysis.tokenattributes.OffsetAttribute)

Example 19 with ElasticSearchException

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

the class FieldValueFactorFunctionBuilder method doToFunction.

@Override
protected ScoreFunction doToFunction(QueryShardContext context) {
    MappedFieldType fieldType = context.getMapperService().fullName(field);
    IndexNumericFieldData fieldData = null;
    if (fieldType == null) {
        if (missing == null) {
            throw new ElasticsearchException("Unable to find a field mapper for field [" + field + "]. No 'missing' value defined.");
        }
    } else {
        fieldData = context.getForField(fieldType);
    }
    return new FieldValueFactorFunction(field, factor, modifier, missing, fieldData);
}
Also used : FieldValueFactorFunction(org.elasticsearch.common.lucene.search.function.FieldValueFactorFunction) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) IndexNumericFieldData(org.elasticsearch.index.fielddata.IndexNumericFieldData) ElasticsearchException(org.elasticsearch.ElasticsearchException)

Example 20 with ElasticSearchException

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

the class IndexSearcherWrapper method wrap.

/**
     * If there are configured {@link IndexSearcherWrapper} instances, the {@link IndexSearcher} of the provided engine searcher
     * gets wrapped and a new {@link Engine.Searcher} instances is returned, otherwise the provided {@link Engine.Searcher} is returned.
     *
     * This is invoked each time a {@link Engine.Searcher} is requested to do an operation. (for example search)
     */
public final Engine.Searcher wrap(Engine.Searcher engineSearcher) throws IOException {
    final ElasticsearchDirectoryReader elasticsearchDirectoryReader = ElasticsearchDirectoryReader.getElasticsearchDirectoryReader(engineSearcher.getDirectoryReader());
    if (elasticsearchDirectoryReader == null) {
        throw new IllegalStateException("Can't wrap non elasticsearch directory reader");
    }
    NonClosingReaderWrapper nonClosingReaderWrapper = new NonClosingReaderWrapper(engineSearcher.getDirectoryReader());
    DirectoryReader reader = wrap(nonClosingReaderWrapper);
    if (reader != nonClosingReaderWrapper) {
        if (reader.getCoreCacheKey() != elasticsearchDirectoryReader.getCoreCacheKey()) {
            throw new IllegalStateException("wrapped directory reader doesn't delegate IndexReader#getCoreCacheKey, wrappers must override this method and delegate" + " to the original readers core cache key. Wrapped readers can't be used as cache keys since their are used only per request which would lead to subtle bugs");
        }
        if (ElasticsearchDirectoryReader.getElasticsearchDirectoryReader(reader) != elasticsearchDirectoryReader) {
            // prevent that somebody wraps with a non-filter reader
            throw new IllegalStateException("wrapped directory reader hides actual ElasticsearchDirectoryReader but shouldn't");
        }
    }
    final IndexSearcher origIndexSearcher = engineSearcher.searcher();
    final IndexSearcher innerIndexSearcher = new IndexSearcher(reader);
    innerIndexSearcher.setQueryCache(origIndexSearcher.getQueryCache());
    innerIndexSearcher.setQueryCachingPolicy(origIndexSearcher.getQueryCachingPolicy());
    innerIndexSearcher.setSimilarity(origIndexSearcher.getSimilarity(true));
    // TODO: Right now IndexSearcher isn't wrapper friendly, when it becomes wrapper friendly we should revise this extension point
    // For example if IndexSearcher#rewrite() is overwritten than also IndexSearcher#createNormalizedWeight needs to be overwritten
    // This needs to be fixed before we can allow the IndexSearcher from Engine to be wrapped multiple times
    final IndexSearcher indexSearcher = wrap(innerIndexSearcher);
    if (reader == nonClosingReaderWrapper && indexSearcher == innerIndexSearcher) {
        return engineSearcher;
    } else {
        return new Engine.Searcher(engineSearcher.source(), indexSearcher) {

            @Override
            public void close() throws ElasticsearchException {
                try {
                    reader().close();
                // we close the reader to make sure wrappers can release resources if needed....
                // our NonClosingReaderWrapper makes sure that our reader is not closed
                } catch (IOException e) {
                    throw new ElasticsearchException("failed to close reader", e);
                } finally {
                    engineSearcher.close();
                }
            }
        };
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) ElasticsearchDirectoryReader(org.elasticsearch.common.lucene.index.ElasticsearchDirectoryReader) FilterDirectoryReader(org.apache.lucene.index.FilterDirectoryReader) DirectoryReader(org.apache.lucene.index.DirectoryReader) ElasticsearchDirectoryReader(org.elasticsearch.common.lucene.index.ElasticsearchDirectoryReader) IndexSearcher(org.apache.lucene.search.IndexSearcher) IOException(java.io.IOException) ElasticsearchException(org.elasticsearch.ElasticsearchException)

Aggregations

ElasticsearchException (org.elasticsearch.ElasticsearchException)309 IOException (java.io.IOException)127 Settings (org.elasticsearch.common.settings.Settings)32 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)30 HashMap (java.util.HashMap)29 ClusterState (org.elasticsearch.cluster.ClusterState)29 ArrayList (java.util.ArrayList)28 Matchers.containsString (org.hamcrest.Matchers.containsString)25 List (java.util.List)20 Map (java.util.Map)20 AtomicReference (java.util.concurrent.atomic.AtomicReference)20 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)18 XContentParser (org.elasticsearch.common.xcontent.XContentParser)17 Path (java.nio.file.Path)16 Test (org.junit.Test)16 ActionListener (org.elasticsearch.action.ActionListener)15 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)14 Version (org.elasticsearch.Version)14 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)13 ResourceNotFoundException (org.elasticsearch.ResourceNotFoundException)13