Search in sources :

Example 1 with QueryShardException

use of org.elasticsearch.index.query.QueryShardException in project elasticsearch by elastic.

the class FieldSortBuilder method build.

@Override
public SortFieldAndFormat build(QueryShardContext context) throws IOException {
    if (DOC_FIELD_NAME.equals(fieldName)) {
        if (order == SortOrder.DESC) {
            return SORT_DOC_REVERSE;
        } else {
            return SORT_DOC;
        }
    } else {
        MappedFieldType fieldType = context.fieldMapper(fieldName);
        if (fieldType == null) {
            if (unmappedType != null) {
                fieldType = context.getMapperService().unmappedFieldType(unmappedType);
            } else {
                throw new QueryShardException(context, "No mapping found for [" + fieldName + "] in order to sort on");
            }
        }
        MultiValueMode localSortMode = null;
        if (sortMode != null) {
            localSortMode = MultiValueMode.fromString(sortMode.toString());
        }
        boolean reverse = (order == SortOrder.DESC);
        if (localSortMode == null) {
            localSortMode = reverse ? MultiValueMode.MAX : MultiValueMode.MIN;
        }
        final Nested nested = resolveNested(context, nestedPath, nestedFilter);
        IndexFieldData<?> fieldData = context.getForField(fieldType);
        if (fieldData instanceof IndexNumericFieldData == false && (sortMode == SortMode.SUM || sortMode == SortMode.AVG || sortMode == SortMode.MEDIAN)) {
            throw new QueryShardException(context, "we only support AVG, MEDIAN and SUM on number based fields");
        }
        IndexFieldData.XFieldComparatorSource fieldComparatorSource = fieldData.comparatorSource(missing, localSortMode, nested);
        SortField field = new SortField(fieldType.name(), fieldComparatorSource, reverse);
        return new SortFieldAndFormat(field, fieldType.docValueFormat(null, null));
    }
}
Also used : MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) Nested(org.elasticsearch.index.fielddata.IndexFieldData.XFieldComparatorSource.Nested) IndexNumericFieldData(org.elasticsearch.index.fielddata.IndexNumericFieldData) QueryShardException(org.elasticsearch.index.query.QueryShardException) IndexFieldData(org.elasticsearch.index.fielddata.IndexFieldData) SortField(org.apache.lucene.search.SortField) MultiValueMode(org.elasticsearch.search.MultiValueMode)

Example 2 with QueryShardException

use of org.elasticsearch.index.query.QueryShardException in project elasticsearch by elastic.

the class ScriptSortBuilder method build.

@Override
public SortFieldAndFormat build(QueryShardContext context) throws IOException {
    final SearchScript searchScript = context.getSearchScript(script, ScriptContext.Standard.SEARCH);
    MultiValueMode valueMode = null;
    if (sortMode != null) {
        valueMode = MultiValueMode.fromString(sortMode.toString());
    }
    boolean reverse = (order == SortOrder.DESC);
    if (valueMode == null) {
        valueMode = reverse ? MultiValueMode.MAX : MultiValueMode.MIN;
    }
    final Nested nested = resolveNested(context, nestedPath, nestedFilter);
    final IndexFieldData.XFieldComparatorSource fieldComparatorSource;
    switch(type) {
        case STRING:
            fieldComparatorSource = new BytesRefFieldComparatorSource(null, null, valueMode, nested) {

                LeafSearchScript leafScript;

                @Override
                protected SortedBinaryDocValues getValues(LeafReaderContext context) throws IOException {
                    leafScript = searchScript.getLeafSearchScript(context);
                    final BinaryDocValues values = new BinaryDocValues() {

                        final BytesRefBuilder spare = new BytesRefBuilder();

                        @Override
                        public BytesRef get(int docID) {
                            leafScript.setDocument(docID);
                            spare.copyChars(leafScript.run().toString());
                            return spare.get();
                        }
                    };
                    return FieldData.singleton(values, null);
                }

                @Override
                protected void setScorer(Scorer scorer) {
                    leafScript.setScorer(scorer);
                }
            };
            break;
        case NUMBER:
            fieldComparatorSource = new DoubleValuesComparatorSource(null, Double.MAX_VALUE, valueMode, nested) {

                LeafSearchScript leafScript;

                @Override
                protected SortedNumericDoubleValues getValues(LeafReaderContext context) throws IOException {
                    leafScript = searchScript.getLeafSearchScript(context);
                    final NumericDoubleValues values = new NumericDoubleValues() {

                        @Override
                        public double get(int docID) {
                            leafScript.setDocument(docID);
                            return leafScript.runAsDouble();
                        }
                    };
                    return FieldData.singleton(values, null);
                }

                @Override
                protected void setScorer(Scorer scorer) {
                    leafScript.setScorer(scorer);
                }
            };
            break;
        default:
            throw new QueryShardException(context, "custom script sort type [" + type + "] not supported");
    }
    return new SortFieldAndFormat(new SortField("_script", fieldComparatorSource, reverse), DocValueFormat.RAW);
}
Also used : BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) DoubleValuesComparatorSource(org.elasticsearch.index.fielddata.fieldcomparator.DoubleValuesComparatorSource) Nested(org.elasticsearch.index.fielddata.IndexFieldData.XFieldComparatorSource.Nested) BytesRefFieldComparatorSource(org.elasticsearch.index.fielddata.fieldcomparator.BytesRefFieldComparatorSource) Scorer(org.apache.lucene.search.Scorer) NumericDoubleValues(org.elasticsearch.index.fielddata.NumericDoubleValues) SortedNumericDoubleValues(org.elasticsearch.index.fielddata.SortedNumericDoubleValues) SortField(org.apache.lucene.search.SortField) IOException(java.io.IOException) MultiValueMode(org.elasticsearch.search.MultiValueMode) SortedBinaryDocValues(org.elasticsearch.index.fielddata.SortedBinaryDocValues) BinaryDocValues(org.apache.lucene.index.BinaryDocValues) SortedBinaryDocValues(org.elasticsearch.index.fielddata.SortedBinaryDocValues) LeafSearchScript(org.elasticsearch.script.LeafSearchScript) SearchScript(org.elasticsearch.script.SearchScript) LeafSearchScript(org.elasticsearch.script.LeafSearchScript) IndexFieldData(org.elasticsearch.index.fielddata.IndexFieldData) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) QueryShardException(org.elasticsearch.index.query.QueryShardException) SortedNumericDoubleValues(org.elasticsearch.index.fielddata.SortedNumericDoubleValues) BytesRef(org.apache.lucene.util.BytesRef)

Example 3 with QueryShardException

use of org.elasticsearch.index.query.QueryShardException in project elasticsearch by elastic.

the class SortBuilder method resolveNested.

protected static Nested resolveNested(QueryShardContext context, String nestedPath, QueryBuilder nestedFilter) throws IOException {
    Nested nested = null;
    if (nestedPath != null) {
        BitSetProducer rootDocumentsFilter = context.bitsetFilter(Queries.newNonNestedFilter());
        ObjectMapper nestedObjectMapper = context.getObjectMapper(nestedPath);
        if (nestedObjectMapper == null) {
            throw new QueryShardException(context, "[nested] failed to find nested object under path [" + nestedPath + "]");
        }
        if (!nestedObjectMapper.nested().isNested()) {
            throw new QueryShardException(context, "[nested] nested object under path [" + nestedPath + "] is not of nested type");
        }
        Query innerDocumentsQuery;
        if (nestedFilter != null) {
            context.nestedScope().nextLevel(nestedObjectMapper);
            innerDocumentsQuery = QueryBuilder.rewriteQuery(nestedFilter, context).toFilter(context);
            context.nestedScope().previousLevel();
        } else {
            innerDocumentsQuery = nestedObjectMapper.nestedTypeFilter();
        }
        nested = new Nested(rootDocumentsFilter, innerDocumentsQuery);
    }
    return nested;
}
Also used : Query(org.apache.lucene.search.Query) BitSetProducer(org.apache.lucene.search.join.BitSetProducer) Nested(org.elasticsearch.index.fielddata.IndexFieldData.XFieldComparatorSource.Nested) QueryShardException(org.elasticsearch.index.query.QueryShardException) ObjectMapper(org.elasticsearch.index.mapper.ObjectMapper)

Example 4 with QueryShardException

use of org.elasticsearch.index.query.QueryShardException in project elasticsearch by elastic.

the class PercolateQueryBuilder method doToQuery.

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
    // Call nowInMillis() so that this query becomes un-cacheable since we
    // can't be sure that it doesn't use now or scripts
    context.nowInMillis();
    if (indexedDocumentIndex != null || indexedDocumentType != null || indexedDocumentId != null) {
        throw new IllegalStateException("query builder must be rewritten first");
    }
    if (document == null) {
        throw new IllegalStateException("no document to percolate");
    }
    MapperService mapperService = context.getMapperService();
    DocumentMapperForType docMapperForType = mapperService.documentMapperWithAutoCreate(documentType);
    DocumentMapper docMapper = docMapperForType.getDocumentMapper();
    ParsedDocument doc = docMapper.parse(source(context.index().getName(), documentType, "_temp_id", document, documentXContentType));
    FieldNameAnalyzer fieldNameAnalyzer = (FieldNameAnalyzer) docMapper.mappers().indexAnalyzer();
    // Need to this custom impl because FieldNameAnalyzer is strict and the percolator sometimes isn't when
    // 'index.percolator.map_unmapped_fields_as_string' is enabled:
    Analyzer analyzer = new DelegatingAnalyzerWrapper(Analyzer.PER_FIELD_REUSE_STRATEGY) {

        @Override
        protected Analyzer getWrappedAnalyzer(String fieldName) {
            Analyzer analyzer = fieldNameAnalyzer.analyzers().get(fieldName);
            if (analyzer != null) {
                return analyzer;
            } else {
                return context.getIndexAnalyzers().getDefaultIndexAnalyzer();
            }
        }
    };
    final IndexSearcher docSearcher;
    if (doc.docs().size() > 1) {
        assert docMapper.hasNestedObjects();
        docSearcher = createMultiDocumentSearcher(analyzer, doc);
    } else {
        MemoryIndex memoryIndex = MemoryIndex.fromDocument(doc.rootDoc(), analyzer, true, false);
        docSearcher = memoryIndex.createSearcher();
        docSearcher.setQueryCache(null);
    }
    Version indexVersionCreated = context.getIndexSettings().getIndexVersionCreated();
    boolean mapUnmappedFieldsAsString = context.getIndexSettings().getValue(PercolatorFieldMapper.INDEX_MAP_UNMAPPED_FIELDS_AS_STRING_SETTING);
    // We have to make a copy of the QueryShardContext here so we can have a unfrozen version for parsing the legacy
    // percolator queries
    QueryShardContext percolateShardContext = new QueryShardContext(context);
    MappedFieldType fieldType = context.fieldMapper(field);
    if (fieldType == null) {
        throw new QueryShardException(context, "field [" + field + "] does not exist");
    }
    if (!(fieldType instanceof PercolatorFieldMapper.FieldType)) {
        throw new QueryShardException(context, "expected field [" + field + "] to be of type [percolator], but is of type [" + fieldType.typeName() + "]");
    }
    PercolatorFieldMapper.FieldType pft = (PercolatorFieldMapper.FieldType) fieldType;
    PercolateQuery.QueryStore queryStore = createStore(pft, percolateShardContext, mapUnmappedFieldsAsString);
    return pft.percolateQuery(documentType, queryStore, document, docSearcher);
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) FieldNameAnalyzer(org.elasticsearch.index.analysis.FieldNameAnalyzer) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) Analyzer(org.apache.lucene.analysis.Analyzer) FieldNameAnalyzer(org.elasticsearch.index.analysis.FieldNameAnalyzer) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) MemoryIndex(org.apache.lucene.index.memory.MemoryIndex) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) DelegatingAnalyzerWrapper(org.apache.lucene.analysis.DelegatingAnalyzerWrapper) Version(org.elasticsearch.Version) DocumentMapperForType(org.elasticsearch.index.mapper.DocumentMapperForType) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) QueryShardContext(org.elasticsearch.index.query.QueryShardContext) QueryShardException(org.elasticsearch.index.query.QueryShardException) MapperService(org.elasticsearch.index.mapper.MapperService)

Example 5 with QueryShardException

use of org.elasticsearch.index.query.QueryShardException in project elasticsearch by elastic.

the class TransportValidateQueryAction method shardOperation.

@Override
protected ShardValidateQueryResponse shardOperation(ShardValidateQueryRequest request) throws IOException {
    boolean valid;
    String explanation = null;
    String error = null;
    ShardSearchLocalRequest shardSearchLocalRequest = new ShardSearchLocalRequest(request.shardId(), request.types(), request.nowInMillis(), request.filteringAliases());
    SearchContext searchContext = searchService.createSearchContext(shardSearchLocalRequest, SearchService.NO_TIMEOUT, null);
    try {
        ParsedQuery parsedQuery = searchContext.getQueryShardContext().toQuery(request.query());
        searchContext.parsedQuery(parsedQuery);
        searchContext.preProcess(request.rewrite());
        valid = true;
        explanation = explain(searchContext, request.rewrite());
    } catch (QueryShardException | ParsingException e) {
        valid = false;
        error = e.getDetailedMessage();
    } catch (AssertionError | IOException e) {
        valid = false;
        error = e.getMessage();
    } finally {
        Releasables.close(searchContext);
    }
    return new ShardValidateQueryResponse(request.shardId(), valid, explanation, error);
}
Also used : ShardSearchLocalRequest(org.elasticsearch.search.internal.ShardSearchLocalRequest) ParsedQuery(org.elasticsearch.index.query.ParsedQuery) ParsingException(org.elasticsearch.common.ParsingException) SearchContext(org.elasticsearch.search.internal.SearchContext) QueryShardException(org.elasticsearch.index.query.QueryShardException) IOException(java.io.IOException)

Aggregations

QueryShardException (org.elasticsearch.index.query.QueryShardException)7 IOException (java.io.IOException)3 Nested (org.elasticsearch.index.fielddata.IndexFieldData.XFieldComparatorSource.Nested)3 SortField (org.apache.lucene.search.SortField)2 ParsingException (org.elasticsearch.common.ParsingException)2 Index (org.elasticsearch.index.Index)2 IndexFieldData (org.elasticsearch.index.fielddata.IndexFieldData)2 MappedFieldType (org.elasticsearch.index.mapper.MappedFieldType)2 MultiValueMode (org.elasticsearch.search.MultiValueMode)2 EOFException (java.io.EOFException)1 FileNotFoundException (java.io.FileNotFoundException)1 URISyntaxException (java.net.URISyntaxException)1 AccessDeniedException (java.nio.file.AccessDeniedException)1 AtomicMoveNotSupportedException (java.nio.file.AtomicMoveNotSupportedException)1 DirectoryNotEmptyException (java.nio.file.DirectoryNotEmptyException)1 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)1 FileSystemException (java.nio.file.FileSystemException)1 FileSystemLoopException (java.nio.file.FileSystemLoopException)1 NoSuchFileException (java.nio.file.NoSuchFileException)1 NotDirectoryException (java.nio.file.NotDirectoryException)1