Search in sources :

Example 76 with ElasticsearchParseException

use of org.elasticsearch.ElasticsearchParseException in project elasticsearch by elastic.

the class XContentHelper method convertToMap.

/**
     * Converts the given bytes into a map that is optionally ordered. The provided {@link XContentType} must be non-null.
     */
public static Tuple<XContentType, Map<String, Object>> convertToMap(BytesReference bytes, boolean ordered, XContentType xContentType) throws ElasticsearchParseException {
    try {
        final XContentType contentType;
        InputStream input;
        Compressor compressor = CompressorFactory.compressor(bytes);
        if (compressor != null) {
            InputStream compressedStreamInput = compressor.streamInput(bytes.streamInput());
            if (compressedStreamInput.markSupported() == false) {
                compressedStreamInput = new BufferedInputStream(compressedStreamInput);
            }
            input = compressedStreamInput;
        } else {
            input = bytes.streamInput();
        }
        contentType = xContentType != null ? xContentType : XContentFactory.xContentType(input);
        return new Tuple<>(Objects.requireNonNull(contentType), convertToMap(XContentFactory.xContent(contentType), input, ordered));
    } catch (IOException e) {
        throw new ElasticsearchParseException("Failed to parse content to map", e);
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) Compressor(org.elasticsearch.common.compress.Compressor) IOException(java.io.IOException) Tuple(org.elasticsearch.common.collect.Tuple)

Example 77 with ElasticsearchParseException

use of org.elasticsearch.ElasticsearchParseException in project elasticsearch by elastic.

the class ByteSizeValue method parseBytesSizeValue.

public static ByteSizeValue parseBytesSizeValue(String sValue, ByteSizeValue defaultValue, String settingName) throws ElasticsearchParseException {
    settingName = Objects.requireNonNull(settingName);
    if (sValue == null) {
        return defaultValue;
    }
    long bytes;
    try {
        String lowerSValue = sValue.toLowerCase(Locale.ROOT).trim();
        if (lowerSValue.endsWith("k")) {
            bytes = (long) (Double.parseDouble(lowerSValue.substring(0, lowerSValue.length() - 1)) * ByteSizeUnit.C1);
        } else if (lowerSValue.endsWith("kb")) {
            bytes = (long) (Double.parseDouble(lowerSValue.substring(0, lowerSValue.length() - 2)) * ByteSizeUnit.C1);
        } else if (lowerSValue.endsWith("m")) {
            bytes = (long) (Double.parseDouble(lowerSValue.substring(0, lowerSValue.length() - 1)) * ByteSizeUnit.C2);
        } else if (lowerSValue.endsWith("mb")) {
            bytes = (long) (Double.parseDouble(lowerSValue.substring(0, lowerSValue.length() - 2)) * ByteSizeUnit.C2);
        } else if (lowerSValue.endsWith("g")) {
            bytes = (long) (Double.parseDouble(lowerSValue.substring(0, lowerSValue.length() - 1)) * ByteSizeUnit.C3);
        } else if (lowerSValue.endsWith("gb")) {
            bytes = (long) (Double.parseDouble(lowerSValue.substring(0, lowerSValue.length() - 2)) * ByteSizeUnit.C3);
        } else if (lowerSValue.endsWith("t")) {
            bytes = (long) (Double.parseDouble(lowerSValue.substring(0, lowerSValue.length() - 1)) * ByteSizeUnit.C4);
        } else if (lowerSValue.endsWith("tb")) {
            bytes = (long) (Double.parseDouble(lowerSValue.substring(0, lowerSValue.length() - 2)) * ByteSizeUnit.C4);
        } else if (lowerSValue.endsWith("p")) {
            bytes = (long) (Double.parseDouble(lowerSValue.substring(0, lowerSValue.length() - 1)) * ByteSizeUnit.C5);
        } else if (lowerSValue.endsWith("pb")) {
            bytes = (long) (Double.parseDouble(lowerSValue.substring(0, lowerSValue.length() - 2)) * ByteSizeUnit.C5);
        } else if (lowerSValue.endsWith("b")) {
            bytes = Long.parseLong(lowerSValue.substring(0, lowerSValue.length() - 1).trim());
        } else if (lowerSValue.equals("-1")) {
            // Allow this special value to be unit-less:
            bytes = -1;
        } else if (lowerSValue.equals("0")) {
            // Allow this special value to be unit-less:
            bytes = 0;
        } else {
            // Missing units:
            throw new ElasticsearchParseException("failed to parse setting [{}] with value [{}] as a size in bytes: unit is missing or unrecognized", settingName, sValue);
        }
    } catch (NumberFormatException e) {
        throw new ElasticsearchParseException("failed to parse [{}]", e, sValue);
    }
    return new ByteSizeValue(bytes, ByteSizeUnit.BYTES);
}
Also used : ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException)

Example 78 with ElasticsearchParseException

use of org.elasticsearch.ElasticsearchParseException in project elasticsearch by elastic.

the class DateProcessorFactoryTests method testMatchFormatsIsMandatory.

public void testMatchFormatsIsMandatory() throws Exception {
    DateProcessor.Factory factory = new DateProcessor.Factory();
    Map<String, Object> config = new HashMap<>();
    String sourceField = randomAsciiOfLengthBetween(1, 10);
    String targetField = randomAsciiOfLengthBetween(1, 10);
    config.put("field", sourceField);
    config.put("target_field", targetField);
    try {
        factory.create(null, null, config);
        fail("processor creation should have failed");
    } catch (ElasticsearchParseException e) {
        assertThat(e.getMessage(), containsString("[formats] required property is missing"));
    }
}
Also used : HashMap(java.util.HashMap) ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 79 with ElasticsearchParseException

use of org.elasticsearch.ElasticsearchParseException in project elasticsearch by elastic.

the class SuggestionBuilder method fromXContent.

static SuggestionBuilder<?> fromXContent(XContentParser parser) throws IOException {
    XContentParser.Token token;
    String currentFieldName = null;
    String suggestText = null;
    String prefix = null;
    String regex = null;
    SuggestionBuilder<?> suggestionBuilder = null;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (token.isValue()) {
            if (TEXT_FIELD.match(currentFieldName)) {
                suggestText = parser.text();
            } else if (PREFIX_FIELD.match(currentFieldName)) {
                prefix = parser.text();
            } else if (REGEX_FIELD.match(currentFieldName)) {
                regex = parser.text();
            } else {
                throw new ParsingException(parser.getTokenLocation(), "suggestion does not support [" + currentFieldName + "]");
            }
        } else if (token == XContentParser.Token.START_OBJECT) {
            suggestionBuilder = parser.namedObject(SuggestionBuilder.class, currentFieldName, null);
        }
    }
    if (suggestionBuilder == null) {
        throw new ElasticsearchParseException("missing suggestion object");
    }
    if (suggestText != null) {
        suggestionBuilder.text(suggestText);
    }
    if (prefix != null) {
        suggestionBuilder.prefix(prefix);
    }
    if (regex != null) {
        suggestionBuilder.regex(regex);
    }
    return suggestionBuilder;
}
Also used : ParsingException(org.elasticsearch.common.ParsingException) ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 80 with ElasticsearchParseException

use of org.elasticsearch.ElasticsearchParseException in project elasticsearch by elastic.

the class LeafFieldsLookup method loadFieldData.

private FieldLookup loadFieldData(String name) {
    FieldLookup data = cachedFieldData.get(name);
    if (data == null) {
        MappedFieldType fieldType = mapperService.fullName(name);
        if (fieldType == null) {
            throw new IllegalArgumentException("No field found for [" + name + "] in mapping with types " + Arrays.toString(types) + "");
        }
        data = new FieldLookup(fieldType);
        cachedFieldData.put(name, data);
    }
    if (data.fields() == null) {
        String fieldName = data.fieldType().name();
        fieldVisitor.reset(fieldName);
        try {
            reader.document(docId, fieldVisitor);
            fieldVisitor.postProcess(data.fieldType());
            data.fields(singletonMap(name, fieldVisitor.fields().get(data.fieldType().name())));
        } catch (IOException e) {
            throw new ElasticsearchParseException("failed to load field [{}]", e, name);
        }
    }
    return data;
}
Also used : ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) IOException(java.io.IOException)

Aggregations

ElasticsearchParseException (org.elasticsearch.ElasticsearchParseException)106 XContentParser (org.elasticsearch.common.xcontent.XContentParser)44 HashMap (java.util.HashMap)28 ArrayList (java.util.ArrayList)18 IOException (java.io.IOException)13 Map (java.util.Map)11 List (java.util.List)7 GeoPoint (org.elasticsearch.common.geo.GeoPoint)7 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)6 Matchers.containsString (org.hamcrest.Matchers.containsString)6 ParsingException (org.elasticsearch.common.ParsingException)5 HashSet (java.util.HashSet)4 Set (java.util.Set)4 ElasticsearchTimeoutException (org.elasticsearch.ElasticsearchTimeoutException)4 Version (org.elasticsearch.Version)4 NoNodeAvailableException (org.elasticsearch.client.transport.NoNodeAvailableException)4 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)4 UncheckedIOException (java.io.UncheckedIOException)3 LinkedHashSet (java.util.LinkedHashSet)3 PutPipelineRequest (org.elasticsearch.action.ingest.PutPipelineRequest)3