Search in sources :

Example 16 with QueryShardException

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

the class FieldSortBuilder method build.

@Override
public SortFieldAndFormat build(QueryShardContext context) throws IOException {
    if (DOC_FIELD_NAME.equals(fieldName)) {
        return order == SortOrder.DESC ? SORT_DOC_REVERSE : SORT_DOC;
    }
    MappedFieldType fieldType = context.fieldMapper(fieldName);
    Nested nested = nested(context, fieldType);
    if (fieldType == null) {
        fieldType = resolveUnmappedType(context);
    }
    boolean reverse = order == SortOrder.DESC;
    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");
    }
    final SortField field;
    boolean isNanosecond = false;
    if (numericType != null) {
        if (fieldData instanceof IndexNumericFieldData == false) {
            throw new QueryShardException(context, "[numeric_type] option cannot be set on a non-numeric field, got " + fieldType.typeName());
        }
        IndexNumericFieldData numericFieldData = (IndexNumericFieldData) fieldData;
        NumericType resolvedType = resolveNumericType(numericType);
        field = numericFieldData.sortField(resolvedType, missing, localSortMode(), nested, reverse);
        isNanosecond = resolvedType == NumericType.DATE_NANOSECONDS;
    } else {
        field = fieldData.sortField(missing, localSortMode(), nested, reverse);
        if (fieldData instanceof IndexNumericFieldData) {
            isNanosecond = ((IndexNumericFieldData) fieldData).getNumericType() == NumericType.DATE_NANOSECONDS;
        }
    }
    DocValueFormat format = fieldType.docValueFormat(null, null);
    if (isNanosecond) {
        format = DocValueFormat.withNanosecondResolution(format);
    }
    return new SortFieldAndFormat(field, format);
}
Also used : NumericType(org.opensearch.index.fielddata.IndexNumericFieldData.NumericType) DocValueFormat(org.opensearch.search.DocValueFormat) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) Nested(org.opensearch.index.fielddata.IndexFieldData.XFieldComparatorSource.Nested) IndexNumericFieldData(org.opensearch.index.fielddata.IndexNumericFieldData) QueryShardException(org.opensearch.index.query.QueryShardException) SortField(org.apache.lucene.search.SortField)

Example 17 with QueryShardException

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

the class ExceptionSerializationTests method testWriteThrowable.

public void testWriteThrowable() throws IOException {
    final QueryShardException queryShardException = new QueryShardException(new Index("foo", "_na_"), "foobar", null);
    final UnknownException unknownException = new UnknownException("this exception is unknown", queryShardException);
    final Exception[] causes = new Exception[] { new IllegalStateException("foobar"), new IllegalArgumentException("alalaal"), new NullPointerException("boom"), new EOFException("dadada"), new OpenSearchSecurityException("nono!"), new NumberFormatException("not a number"), new CorruptIndexException("baaaam booom", "this is my resource"), new IndexFormatTooNewException("tooo new", 1, 2, 3), new IndexFormatTooOldException("tooo new", 1, 2, 3), new IndexFormatTooOldException("tooo new", "very old version"), new ArrayIndexOutOfBoundsException("booom"), new StringIndexOutOfBoundsException("booom"), new FileNotFoundException("booom"), new NoSuchFileException("booom"), new AlreadyClosedException("closed!!", new NullPointerException()), new LockObtainFailedException("can't lock directory", new NullPointerException()), unknownException };
    for (final Exception cause : causes) {
        OpenSearchException ex = new OpenSearchException("topLevel", cause);
        OpenSearchException deserialized = serialize(ex);
        assertEquals(deserialized.getMessage(), ex.getMessage());
        assertTrue("Expected: " + deserialized.getCause().getMessage() + " to contain: " + ex.getCause().getClass().getName() + " but it didn't", deserialized.getCause().getMessage().contains(ex.getCause().getMessage()));
        if (ex.getCause().getClass() != UnknownException.class) {
            // unknown exception is not directly mapped
            assertEquals(deserialized.getCause().getClass(), ex.getCause().getClass());
        } else {
            assertEquals(deserialized.getCause().getClass(), NotSerializableExceptionWrapper.class);
        }
        assertArrayEquals(deserialized.getStackTrace(), ex.getStackTrace());
        assertTrue(deserialized.getStackTrace().length > 1);
    }
}
Also used : FileNotFoundException(java.io.FileNotFoundException) NoSuchFileException(java.nio.file.NoSuchFileException) CorruptIndexException(org.apache.lucene.index.CorruptIndexException) Index(org.opensearch.index.Index) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) IndexFormatTooNewException(org.apache.lucene.index.IndexFormatTooNewException) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) RecoveryEngineException(org.opensearch.index.engine.RecoveryEngineException) RetentionLeaseAlreadyExistsException(org.opensearch.index.seqno.RetentionLeaseAlreadyExistsException) RetentionLeaseInvalidRetainingSeqNoException(org.opensearch.index.seqno.RetentionLeaseInvalidRetainingSeqNoException) IngestProcessorException(org.opensearch.ingest.IngestProcessorException) AccessDeniedException(java.nio.file.AccessDeniedException) LockObtainFailedException(org.apache.lucene.store.LockObtainFailedException) SnapshotException(org.opensearch.snapshots.SnapshotException) RecoverFilesRecoveryException(org.opensearch.indices.recovery.RecoverFilesRecoveryException) CircuitBreakingException(org.opensearch.common.breaker.CircuitBreakingException) NotDirectoryException(java.nio.file.NotDirectoryException) DirectoryNotEmptyException(java.nio.file.DirectoryNotEmptyException) IOException(java.io.IOException) SearchPhaseExecutionException(org.opensearch.action.search.SearchPhaseExecutionException) ActionTransportException(org.opensearch.transport.ActionTransportException) NoSuchFileException(java.nio.file.NoSuchFileException) URISyntaxException(java.net.URISyntaxException) NoSuchRemoteClusterException(org.opensearch.transport.NoSuchRemoteClusterException) CorruptIndexException(org.apache.lucene.index.CorruptIndexException) NodeHealthCheckFailureException(org.opensearch.cluster.coordination.NodeHealthCheckFailureException) AliasesNotFoundException(org.opensearch.rest.action.admin.indices.AliasesNotFoundException) TimestampParsingException(org.opensearch.action.TimestampParsingException) ParsingException(org.opensearch.common.ParsingException) RepositoryException(org.opensearch.repositories.RepositoryException) ActionNotFoundTransportException(org.opensearch.transport.ActionNotFoundTransportException) SnapshotInProgressException(org.opensearch.snapshots.SnapshotInProgressException) FileSystemException(java.nio.file.FileSystemException) ClusterBlockException(org.opensearch.cluster.block.ClusterBlockException) QueryShardException(org.opensearch.index.query.QueryShardException) SearchParseException(org.opensearch.search.SearchParseException) SearchContextMissingException(org.opensearch.search.SearchContextMissingException) EOFException(java.io.EOFException) FileNotFoundException(java.io.FileNotFoundException) CoordinationStateRejectedException(org.opensearch.cluster.coordination.CoordinationStateRejectedException) ConnectTransportException(org.opensearch.transport.ConnectTransportException) NoSeedNodeLeftException(org.opensearch.transport.NoSeedNodeLeftException) IndexTemplateMissingException(org.opensearch.indices.IndexTemplateMissingException) IllegalShardRoutingStateException(org.opensearch.cluster.routing.IllegalShardRoutingStateException) RoutingMissingException(org.opensearch.action.RoutingMissingException) ShardNotInPrimaryModeException(org.opensearch.index.shard.ShardNotInPrimaryModeException) SearchException(org.opensearch.search.SearchException) FileSystemLoopException(java.nio.file.FileSystemLoopException) IllegalIndexShardStateException(org.opensearch.index.shard.IllegalIndexShardStateException) FailedNodeException(org.opensearch.action.FailedNodeException) ShardLockObtainFailedException(org.opensearch.env.ShardLockObtainFailedException) IndexFormatTooOldException(org.apache.lucene.index.IndexFormatTooOldException) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) AtomicMoveNotSupportedException(java.nio.file.AtomicMoveNotSupportedException) InvalidIndexTemplateException(org.opensearch.indices.InvalidIndexTemplateException) RetentionLeaseNotFoundException(org.opensearch.index.seqno.RetentionLeaseNotFoundException) IndexFormatTooOldException(org.apache.lucene.index.IndexFormatTooOldException) LockObtainFailedException(org.apache.lucene.store.LockObtainFailedException) ShardLockObtainFailedException(org.opensearch.env.ShardLockObtainFailedException) EOFException(java.io.EOFException) QueryShardException(org.opensearch.index.query.QueryShardException) IndexFormatTooNewException(org.apache.lucene.index.IndexFormatTooNewException)

Example 18 with QueryShardException

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

the class ExceptionsHelperTests method createShardFailureQueryShardException.

private static ShardSearchFailure createShardFailureQueryShardException(String error, String indexName, String clusterAlias) {
    Index index = new Index(RemoteClusterAware.buildRemoteIndexName(clusterAlias, indexName), "uuid");
    QueryShardException queryShardException = new QueryShardException(index, error, new IllegalArgumentException("parse error"));
    return new ShardSearchFailure(queryShardException, null);
}
Also used : QueryShardException(org.opensearch.index.query.QueryShardException) Index(org.opensearch.index.Index) ShardSearchFailure(org.opensearch.action.search.ShardSearchFailure)

Example 19 with QueryShardException

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

the class QueryParserHelper method resolveMappingField.

/**
 * Resolves the provided pattern or field name from the {@link QueryShardContext} and return a map of
 * the expanded fields with their original boost.
 * @param context The context of the query
 * @param fieldOrPattern The field name or the pattern to resolve
 * @param weight The weight for the field
 * @param acceptAllTypes Whether all field type should be added when a pattern is expanded.
 *                       If false, only searchable field types are added.
 * @param acceptMetadataField Whether metadata fields should be added when a pattern is expanded.
 * @param fieldSuffix The suffix name to add to the expanded field names if a mapping exists for that name.
 *                    The original name of the field is kept if adding the suffix to the field name does not point to a valid field
 *                    in the mapping.
 */
static Map<String, Float> resolveMappingField(QueryShardContext context, String fieldOrPattern, float weight, boolean acceptAllTypes, boolean acceptMetadataField, String fieldSuffix) {
    Set<String> allFields = context.simpleMatchToIndexNames(fieldOrPattern);
    Map<String, Float> fields = new HashMap<>();
    for (String fieldName : allFields) {
        if (fieldSuffix != null && context.fieldMapper(fieldName + fieldSuffix) != null) {
            fieldName = fieldName + fieldSuffix;
        }
        MappedFieldType fieldType = context.getMapperService().fieldType(fieldName);
        if (fieldType == null) {
            continue;
        }
        if (acceptMetadataField == false && fieldType.name().startsWith("_")) {
            // Ignore metadata fields
            continue;
        }
        if (acceptAllTypes == false) {
            try {
                fieldType.termQuery("", context);
            } catch (QueryShardException | UnsupportedOperationException e) {
                // field type is never searchable with term queries (eg. geo point): ignore
                continue;
            } catch (IllegalArgumentException | OpenSearchParseException e) {
            // other exceptions are parsing errors or not indexed fields: keep
            }
        }
        // Deduplicate aliases and their concrete fields.
        String resolvedFieldName = fieldType.name();
        if (allFields.contains(resolvedFieldName)) {
            fieldName = resolvedFieldName;
        }
        float w = fields.getOrDefault(fieldName, 1.0F);
        fields.put(fieldName, w * weight);
    }
    return fields;
}
Also used : HashMap(java.util.HashMap) OpenSearchParseException(org.opensearch.OpenSearchParseException) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) QueryShardException(org.opensearch.index.query.QueryShardException)

Example 20 with QueryShardException

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

the class FieldSortBuilderTests method testModeNonNumericField.

/**
 * Test that MIN, MAX mode work on non-numeric fields, but other modes throw exception
 */
public void testModeNonNumericField() throws IOException {
    QueryShardContext shardContextMock = createMockShardContext();
    FieldSortBuilder sortBuilder = new FieldSortBuilder(MAPPED_STRING_FIELDNAME).sortMode(SortMode.MIN);
    SortField sortField = sortBuilder.build(shardContextMock).field;
    assertThat(sortField, instanceOf(SortedSetSortField.class));
    assertEquals(SortedSetSelector.Type.MIN, ((SortedSetSortField) sortField).getSelector());
    sortBuilder = new FieldSortBuilder(MAPPED_STRING_FIELDNAME).sortMode(SortMode.MAX);
    sortField = sortBuilder.build(shardContextMock).field;
    assertThat(sortField, instanceOf(SortedSetSortField.class));
    assertEquals(SortedSetSelector.Type.MAX, ((SortedSetSortField) sortField).getSelector());
    String expectedError = "we only support AVG, MEDIAN and SUM on number based fields";
    QueryShardException e = expectThrows(QueryShardException.class, () -> new FieldSortBuilder(MAPPED_STRING_FIELDNAME).sortMode(SortMode.AVG).build(shardContextMock));
    assertEquals(expectedError, e.getMessage());
    e = expectThrows(QueryShardException.class, () -> new FieldSortBuilder(MAPPED_STRING_FIELDNAME).sortMode(SortMode.SUM).build(shardContextMock));
    assertEquals(expectedError, e.getMessage());
    e = expectThrows(QueryShardException.class, () -> new FieldSortBuilder(MAPPED_STRING_FIELDNAME).sortMode(SortMode.MEDIAN).build(shardContextMock));
    assertEquals(expectedError, e.getMessage());
}
Also used : SortedSetSortField(org.apache.lucene.search.SortedSetSortField) QueryShardContext(org.opensearch.index.query.QueryShardContext) QueryShardException(org.opensearch.index.query.QueryShardException) SortField(org.apache.lucene.search.SortField) SortedSetSortField(org.apache.lucene.search.SortedSetSortField) SortedNumericSortField(org.apache.lucene.search.SortedNumericSortField)

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