Search in sources :

Example 91 with OpenSearchParseException

use of org.opensearch.OpenSearchParseException 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 92 with OpenSearchParseException

use of org.opensearch.OpenSearchParseException in project OpenSearch by opensearch-project.

the class SourceLookup method loadSourceIfNeeded.

// Scripting requires this method to be public. Using source()
// is not possible because certain checks use source == null as
// as a determination if source is enabled/disabled, but it should
// never be a null Map for scripting even when disabled.
public Map<String, Object> loadSourceIfNeeded() {
    if (source != null) {
        return source;
    }
    if (sourceAsBytes != null) {
        Tuple<XContentType, Map<String, Object>> tuple = sourceAsMapAndType(sourceAsBytes);
        sourceContentType = tuple.v1();
        source = tuple.v2();
        return source;
    }
    try {
        FieldsVisitor sourceFieldVisitor = new FieldsVisitor(true);
        fieldReader.accept(docId, sourceFieldVisitor);
        BytesReference source = sourceFieldVisitor.source();
        if (source == null) {
            this.source = emptyMap();
            this.sourceContentType = null;
        } else {
            Tuple<XContentType, Map<String, Object>> tuple = sourceAsMapAndType(source);
            this.sourceContentType = tuple.v1();
            this.source = tuple.v2();
        }
    } catch (Exception e) {
        throw new OpenSearchParseException("failed to parse / load source", e);
    }
    return this.source;
}
Also used : BytesReference(org.opensearch.common.bytes.BytesReference) FieldsVisitor(org.opensearch.index.fieldvisitor.FieldsVisitor) XContentType(org.opensearch.common.xcontent.XContentType) OpenSearchParseException(org.opensearch.OpenSearchParseException) Collections.emptyMap(java.util.Collections.emptyMap) Map(java.util.Map) OpenSearchParseException(org.opensearch.OpenSearchParseException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException)

Example 93 with OpenSearchParseException

use of org.opensearch.OpenSearchParseException in project OpenSearch by opensearch-project.

the class GeometryParserTests method testUnsupportedValueParsing.

public void testUnsupportedValueParsing() throws Exception {
    XContentBuilder pointGeoJson = XContentFactory.jsonBuilder().startObject().field("foo", 42).endObject();
    try (XContentParser parser = createParser(pointGeoJson)) {
        // Start object
        parser.nextToken();
        // Field Name
        parser.nextToken();
        // Field Value
        parser.nextToken();
        OpenSearchParseException ex = expectThrows(OpenSearchParseException.class, () -> new GeometryParser(true, randomBoolean(), randomBoolean()).parse(parser));
        assertEquals("shape must be an object consisting of type and coordinates", ex.getMessage());
    }
}
Also used : OpenSearchParseException(org.opensearch.OpenSearchParseException) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 94 with OpenSearchParseException

use of org.opensearch.OpenSearchParseException in project OpenSearch by opensearch-project.

the class GeoContextMappingTests method testMalformedGeoField.

public void testMalformedGeoField() throws Exception {
    XContentBuilder mapping = jsonBuilder();
    mapping.startObject();
    mapping.startObject("type1");
    mapping.startObject("properties");
    mapping.startObject("pin");
    String type = randomFrom("text", "keyword", "long");
    mapping.field("type", type);
    mapping.endObject();
    mapping.startObject("suggestion");
    mapping.field("type", "completion");
    mapping.field("analyzer", "simple");
    mapping.startArray("contexts");
    mapping.startObject();
    mapping.field("name", "st");
    mapping.field("type", "geo");
    mapping.field("path", "pin");
    mapping.field("precision", 5);
    mapping.endObject();
    mapping.endArray();
    mapping.endObject();
    mapping.endObject();
    mapping.endObject();
    mapping.endObject();
    OpenSearchParseException ex = expectThrows(OpenSearchParseException.class, () -> createIndex("test", Settings.EMPTY, "type1", mapping));
    assertThat(ex.getMessage(), equalTo("field [pin] referenced in context [st] must be mapped to geo_point, found [" + type + "]"));
}
Also used : OpenSearchParseException(org.opensearch.OpenSearchParseException) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Example 95 with OpenSearchParseException

use of org.opensearch.OpenSearchParseException in project OpenSearch by opensearch-project.

the class ByteSizeValueTests method testParseInvalidNumber.

public void testParseInvalidNumber() throws IOException {
    OpenSearchParseException exception = expectThrows(OpenSearchParseException.class, () -> ByteSizeValue.parseBytesSizeValue("notANumber", "test"));
    assertEquals("failed to parse setting [test] with value [notANumber] as a size in bytes: unit is missing or unrecognized", exception.getMessage());
    exception = expectThrows(OpenSearchParseException.class, () -> ByteSizeValue.parseBytesSizeValue("notANumberMB", "test"));
    assertEquals("failed to parse [notANumberMB]", exception.getMessage());
}
Also used : OpenSearchParseException(org.opensearch.OpenSearchParseException)

Aggregations

OpenSearchParseException (org.opensearch.OpenSearchParseException)105 XContentParser (org.opensearch.common.xcontent.XContentParser)34 HashMap (java.util.HashMap)27 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)15 ArrayList (java.util.ArrayList)14 Matchers.containsString (org.hamcrest.Matchers.containsString)12 Map (java.util.Map)11 IOException (java.io.IOException)10 List (java.util.List)7 ParsingException (org.opensearch.common.ParsingException)5 GeoPoint (org.opensearch.common.geo.GeoPoint)5 Token (org.opensearch.common.xcontent.XContentParser.Token)5 MappedFieldType (org.opensearch.index.mapper.MappedFieldType)5 GeometryCollectionBuilder (org.opensearch.common.geo.builders.GeometryCollectionBuilder)4 UncheckedIOException (java.io.UncheckedIOException)3 DateTimeParseException (java.time.format.DateTimeParseException)3 HashSet (java.util.HashSet)3 CoordinatesBuilder (org.opensearch.common.geo.builders.CoordinatesBuilder)3 MultiPointBuilder (org.opensearch.common.geo.builders.MultiPointBuilder)3 PointBuilder (org.opensearch.common.geo.builders.PointBuilder)3