Search in sources :

Example 1 with QueryShardException

use of org.opensearch.index.query.QueryShardException in project OpenSearch by opensearch-project.

the class TransportValidateQueryAction method shardOperation.

@Override
protected ShardValidateQueryResponse shardOperation(ShardValidateQueryRequest request, Task task) throws IOException {
    boolean valid;
    String explanation = null;
    String error = null;
    ShardSearchRequest shardSearchLocalRequest = new ShardSearchRequest(request.shardId(), request.nowInMillis(), request.filteringAliases());
    SearchContext searchContext = searchService.createSearchContext(shardSearchLocalRequest, SearchService.NO_TIMEOUT);
    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 e) {
        valid = false;
        error = e.getMessage();
    } finally {
        Releasables.close(searchContext);
    }
    return new ShardValidateQueryResponse(request.shardId(), valid, explanation, error);
}
Also used : ParsedQuery(org.opensearch.index.query.ParsedQuery) ParsingException(org.opensearch.common.ParsingException) ShardSearchRequest(org.opensearch.search.internal.ShardSearchRequest) SearchContext(org.opensearch.search.internal.SearchContext) QueryShardException(org.opensearch.index.query.QueryShardException)

Example 2 with QueryShardException

use of org.opensearch.index.query.QueryShardException in project OpenSearch by opensearch-project.

the class PercolateQueryBuilder method doToQuery.

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
    if (context.allowExpensiveQueries() == false) {
        throw new OpenSearchException("[percolate] queries cannot be executed when '" + ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false.");
    }
    // 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 || indexedDocumentId != null || documentSupplier != null) {
        throw new IllegalStateException("query builder must be rewritten first");
    }
    if (documents.isEmpty()) {
        throw new IllegalStateException("no document to percolate");
    }
    MappedFieldType fieldType = context.fieldMapper(field);
    if (fieldType == null) {
        throw new QueryShardException(context, "field [" + field + "] does not exist");
    }
    if (!(fieldType instanceof PercolatorFieldMapper.PercolatorFieldType)) {
        throw new QueryShardException(context, "expected field [" + field + "] to be of type [percolator], but is of type [" + fieldType.typeName() + "]");
    }
    final List<ParsedDocument> docs = new ArrayList<>();
    final DocumentMapper docMapper;
    final MapperService mapperService = context.getMapperService();
    String type = mapperService.documentMapper().type();
    if (documentType != null) {
        deprecationLogger.deprecate("percolate_with_document_type", DOCUMENT_TYPE_DEPRECATION_MESSAGE);
        if (documentType.equals(type) == false) {
            throw new IllegalArgumentException("specified document_type [" + documentType + "] is not equal to the actual type [" + type + "]");
        }
    }
    docMapper = mapperService.documentMapper(type);
    for (BytesReference document : documents) {
        docs.add(docMapper.parse(new SourceToParse(context.index().getName(), type, "_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;
    final boolean excludeNestedDocuments;
    if (docs.size() > 1 || docs.get(0).docs().size() > 1) {
        assert docs.size() != 1 || docMapper.hasNestedObjects();
        docSearcher = createMultiDocumentSearcher(analyzer, docs);
        excludeNestedDocuments = docMapper.hasNestedObjects() && docs.stream().map(ParsedDocument::docs).mapToInt(List::size).anyMatch(size -> size > 1);
    } else {
        MemoryIndex memoryIndex = MemoryIndex.fromDocument(docs.get(0).rootDoc(), analyzer, true, false);
        docSearcher = memoryIndex.createSearcher();
        docSearcher.setQueryCache(null);
        excludeNestedDocuments = false;
    }
    PercolatorFieldMapper.PercolatorFieldType pft = (PercolatorFieldMapper.PercolatorFieldType) fieldType;
    String name = this.name != null ? this.name : pft.name();
    QueryShardContext percolateShardContext = wrap(context);
    PercolatorFieldMapper.configureContext(percolateShardContext, pft.mapUnmappedFieldsAsText);
    ;
    PercolateQuery.QueryStore queryStore = createStore(pft.queryBuilderField, percolateShardContext);
    return pft.percolateQuery(name, queryStore, documents, docSearcher, excludeNestedDocuments, context.indexVersionCreated());
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) ArrayList(java.util.ArrayList) FieldNameAnalyzer(org.opensearch.index.analysis.FieldNameAnalyzer) Analyzer(org.apache.lucene.analysis.Analyzer) ParsedDocument(org.opensearch.index.mapper.ParsedDocument) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) QueryShardException(org.opensearch.index.query.QueryShardException) QueryShardContext(org.opensearch.index.query.QueryShardContext) BytesReference(org.opensearch.common.bytes.BytesReference) FieldNameAnalyzer(org.opensearch.index.analysis.FieldNameAnalyzer) DocumentMapper(org.opensearch.index.mapper.DocumentMapper) SourceToParse(org.opensearch.index.mapper.SourceToParse) MemoryIndex(org.apache.lucene.index.memory.MemoryIndex) DelegatingAnalyzerWrapper(org.apache.lucene.analysis.DelegatingAnalyzerWrapper) OpenSearchException(org.opensearch.OpenSearchException) MapperService(org.opensearch.index.mapper.MapperService)

Example 3 with QueryShardException

use of org.opensearch.index.query.QueryShardException in project OpenSearch by opensearch-project.

the class ParentIdQueryBuilderTests method testIgnoreUnmapped.

public void testIgnoreUnmapped() throws IOException {
    final ParentIdQueryBuilder queryBuilder = new ParentIdQueryBuilder("unmapped", "foo");
    queryBuilder.ignoreUnmapped(true);
    Query query = queryBuilder.toQuery(createShardContext());
    assertThat(query, notNullValue());
    assertThat(query, instanceOf(MatchNoDocsQuery.class));
    final ParentIdQueryBuilder failingQueryBuilder = new ParentIdQueryBuilder("unmapped", "foo");
    failingQueryBuilder.ignoreUnmapped(false);
    QueryShardException e = expectThrows(QueryShardException.class, () -> failingQueryBuilder.toQuery(createShardContext()));
    assertThat(e.getMessage(), containsString("[" + ParentIdQueryBuilder.NAME + "] no relation found for child [unmapped]"));
}
Also used : Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) QueryShardException(org.opensearch.index.query.QueryShardException)

Example 4 with QueryShardException

use of org.opensearch.index.query.QueryShardException in project OpenSearch by opensearch-project.

the class HasParentQueryBuilderTests method testIllegalValues.

public void testIllegalValues() throws IOException {
    QueryBuilder query = new MatchAllQueryBuilder();
    IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> hasParentQuery(null, query, false));
    assertThat(e.getMessage(), equalTo("[has_parent] requires 'parent_type' field"));
    e = expectThrows(IllegalArgumentException.class, () -> hasParentQuery("foo", null, false));
    assertThat(e.getMessage(), equalTo("[has_parent] requires 'query' field"));
    QueryShardContext context = createShardContext();
    HasParentQueryBuilder qb = hasParentQuery("just_a_type", new MatchAllQueryBuilder(), false);
    QueryShardException qse = expectThrows(QueryShardException.class, () -> qb.doToQuery(context));
    assertThat(qse.getMessage(), equalTo("[has_parent] join field [join_field] doesn't hold [just_a_type] as a parent"));
}
Also used : QueryShardContext(org.opensearch.index.query.QueryShardContext) QueryShardException(org.opensearch.index.query.QueryShardException) WrapperQueryBuilder(org.opensearch.index.query.WrapperQueryBuilder) IdsQueryBuilder(org.opensearch.index.query.IdsQueryBuilder) TermQueryBuilder(org.opensearch.index.query.TermQueryBuilder) QueryBuilder(org.opensearch.index.query.QueryBuilder) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder)

Example 5 with QueryShardException

use of org.opensearch.index.query.QueryShardException in project OpenSearch by opensearch-project.

the class ParentIdQueryBuilder method doToQuery.

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
    if (context.allowExpensiveQueries() == false) {
        throw new OpenSearchException("[joining] queries cannot be executed when '" + ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false.");
    }
    ParentJoinFieldMapper joinFieldMapper = ParentJoinFieldMapper.getMapper(context.getMapperService());
    if (joinFieldMapper == null) {
        if (ignoreUnmapped) {
            return new MatchNoDocsQuery();
        } else {
            final String indexName = context.getIndexSettings().getIndex().getName();
            throw new QueryShardException(context, "[" + NAME + "] no join field found for index [" + indexName + "]");
        }
    }
    final ParentIdFieldMapper childMapper = joinFieldMapper.getParentIdFieldMapper(type, false);
    if (childMapper == null) {
        if (ignoreUnmapped) {
            return new MatchNoDocsQuery();
        } else {
            throw new QueryShardException(context, "[" + NAME + "] no relation found for child [" + type + "]");
        }
    }
    return new BooleanQuery.Builder().add(childMapper.fieldType().termQuery(id, context), BooleanClause.Occur.MUST).add(joinFieldMapper.fieldType().termQuery(type, context), BooleanClause.Occur.FILTER).build();
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) ParentIdFieldMapper(org.opensearch.join.mapper.ParentIdFieldMapper) ParentJoinFieldMapper(org.opensearch.join.mapper.ParentJoinFieldMapper) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) QueryShardException(org.opensearch.index.query.QueryShardException) OpenSearchException(org.opensearch.OpenSearchException)

Aggregations

QueryShardException (org.opensearch.index.query.QueryShardException)21 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)6 Query (org.apache.lucene.search.Query)6 MappedFieldType (org.opensearch.index.mapper.MappedFieldType)6 OpenSearchException (org.opensearch.OpenSearchException)4 BooleanQuery (org.apache.lucene.search.BooleanQuery)3 Index (org.opensearch.index.Index)3 QueryShardContext (org.opensearch.index.query.QueryShardContext)3 IOException (java.io.IOException)2 SortField (org.apache.lucene.search.SortField)2 TermQuery (org.apache.lucene.search.TermQuery)2 ParsingException (org.opensearch.common.ParsingException)2 Nested (org.opensearch.index.fielddata.IndexFieldData.XFieldComparatorSource.Nested)2 SortedSetOrdinalsIndexFieldData (org.opensearch.index.fielddata.plain.SortedSetOrdinalsIndexFieldData)2 InnerHitBuilder (org.opensearch.index.query.InnerHitBuilder)2 MatchAllQueryBuilder (org.opensearch.index.query.MatchAllQueryBuilder)2 QueryBuilder (org.opensearch.index.query.QueryBuilder)2 ParentIdFieldMapper (org.opensearch.join.mapper.ParentIdFieldMapper)2 ParentJoinFieldMapper (org.opensearch.join.mapper.ParentJoinFieldMapper)2 EOFException (java.io.EOFException)1