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;
}
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;
}
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());
}
}
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 + "]"));
}
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());
}
Aggregations