Search in sources :

Example 51 with ElasticsearchParseException

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

the class TermVectorsRequest method parseRequest.

/**
     * populates a request object (pre-populated with defaults) based on a parser.
     */
public static void parseRequest(TermVectorsRequest termVectorsRequest, XContentParser parser) throws IOException {
    XContentParser.Token token;
    String currentFieldName = null;
    List<String> fields = new ArrayList<>();
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (currentFieldName != null) {
            if (currentFieldName.equals("fields")) {
                if (token == XContentParser.Token.START_ARRAY) {
                    while (parser.nextToken() != XContentParser.Token.END_ARRAY) {
                        fields.add(parser.text());
                    }
                } else {
                    throw new ElasticsearchParseException("failed to parse term vectors request. field [fields] must be an array");
                }
            } else if (currentFieldName.equals("offsets")) {
                termVectorsRequest.offsets(parser.booleanValue());
            } else if (currentFieldName.equals("positions")) {
                termVectorsRequest.positions(parser.booleanValue());
            } else if (currentFieldName.equals("payloads")) {
                termVectorsRequest.payloads(parser.booleanValue());
            } else if (currentFieldName.equals("term_statistics") || currentFieldName.equals("termStatistics")) {
                termVectorsRequest.termStatistics(parser.booleanValue());
            } else if (currentFieldName.equals("field_statistics") || currentFieldName.equals("fieldStatistics")) {
                termVectorsRequest.fieldStatistics(parser.booleanValue());
            } else if (currentFieldName.equals("dfs")) {
                throw new IllegalArgumentException("distributed frequencies is not supported anymore for term vectors");
            } else if (currentFieldName.equals("per_field_analyzer") || currentFieldName.equals("perFieldAnalyzer")) {
                termVectorsRequest.perFieldAnalyzer(readPerFieldAnalyzer(parser.map()));
            } else if (currentFieldName.equals("filter")) {
                termVectorsRequest.filterSettings(readFilterSettings(parser));
            } else if ("_index".equals(currentFieldName)) {
                // the following is important for multi request parsing.
                termVectorsRequest.index = parser.text();
            } else if ("_type".equals(currentFieldName)) {
                termVectorsRequest.type = parser.text();
            } else if ("_id".equals(currentFieldName)) {
                if (termVectorsRequest.doc != null) {
                    throw new ElasticsearchParseException("failed to parse term vectors request. either [id] or [doc] can be specified, but not both!");
                }
                termVectorsRequest.id = parser.text();
            } else if ("doc".equals(currentFieldName)) {
                if (termVectorsRequest.id != null) {
                    throw new ElasticsearchParseException("failed to parse term vectors request. either [id] or [doc] can be specified, but not both!");
                }
                termVectorsRequest.doc(jsonBuilder().copyCurrentStructure(parser));
            } else if ("_routing".equals(currentFieldName) || "routing".equals(currentFieldName)) {
                termVectorsRequest.routing = parser.text();
            } else if ("_parent".equals(currentFieldName) || "parent".equals(currentFieldName)) {
                termVectorsRequest.parent = parser.text();
            } else if ("_version".equals(currentFieldName) || "version".equals(currentFieldName)) {
                termVectorsRequest.version = parser.longValue();
            } else if ("_version_type".equals(currentFieldName) || "_versionType".equals(currentFieldName) || "version_type".equals(currentFieldName) || "versionType".equals(currentFieldName)) {
                termVectorsRequest.versionType = VersionType.fromString(parser.text());
            } else {
                throw new ElasticsearchParseException("failed to parse term vectors request. unknown field [{}]", currentFieldName);
            }
        }
    }
    if (fields.size() > 0) {
        String[] fieldsAsArray = new String[fields.size()];
        termVectorsRequest.selectedFields(fields.toArray(fieldsAsArray));
    }
}
Also used : ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) ArrayList(java.util.ArrayList) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 52 with ElasticsearchParseException

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

the class TermVectorsRequest method readFilterSettings.

private static FilterSettings readFilterSettings(XContentParser parser) throws IOException {
    FilterSettings settings = new FilterSettings();
    XContentParser.Token token;
    String currentFieldName = null;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (currentFieldName != null) {
            if (currentFieldName.equals("max_num_terms")) {
                settings.maxNumTerms = parser.intValue();
            } else if (currentFieldName.equals("min_term_freq")) {
                settings.minTermFreq = parser.intValue();
            } else if (currentFieldName.equals("max_term_freq")) {
                settings.maxTermFreq = parser.intValue();
            } else if (currentFieldName.equals("min_doc_freq")) {
                settings.minDocFreq = parser.intValue();
            } else if (currentFieldName.equals("max_doc_freq")) {
                settings.maxDocFreq = parser.intValue();
            } else if (currentFieldName.equals("min_word_length")) {
                settings.minWordLength = parser.intValue();
            } else if (currentFieldName.equals("max_word_length")) {
                settings.maxWordLength = parser.intValue();
            } else {
                throw new ElasticsearchParseException("failed to parse term vectors request. the field [{}] is not valid for filter parameter for term vector request", currentFieldName);
            }
        }
    }
    return settings;
}
Also used : ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 53 with ElasticsearchParseException

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

the class DateMathParser method parseMath.

private long parseMath(String mathString, long time, boolean roundUp, DateTimeZone timeZone) throws ElasticsearchParseException {
    if (timeZone == null) {
        timeZone = DateTimeZone.UTC;
    }
    MutableDateTime dateTime = new MutableDateTime(time, timeZone);
    for (int i = 0; i < mathString.length(); ) {
        char c = mathString.charAt(i++);
        final boolean round;
        final int sign;
        if (c == '/') {
            round = true;
            sign = 1;
        } else {
            round = false;
            if (c == '+') {
                sign = 1;
            } else if (c == '-') {
                sign = -1;
            } else {
                throw new ElasticsearchParseException("operator not supported for date math [{}]", mathString);
            }
        }
        if (i >= mathString.length()) {
            throw new ElasticsearchParseException("truncated date math [{}]", mathString);
        }
        final int num;
        if (!Character.isDigit(mathString.charAt(i))) {
            num = 1;
        } else {
            int numFrom = i;
            while (i < mathString.length() && Character.isDigit(mathString.charAt(i))) {
                i++;
            }
            if (i >= mathString.length()) {
                throw new ElasticsearchParseException("truncated date math [{}]", mathString);
            }
            num = Integer.parseInt(mathString.substring(numFrom, i));
        }
        if (round) {
            if (num != 1) {
                throw new ElasticsearchParseException("rounding `/` can only be used on single unit types [{}]", mathString);
            }
        }
        char unit = mathString.charAt(i++);
        MutableDateTime.Property propertyToRound = null;
        switch(unit) {
            case 'y':
                if (round) {
                    propertyToRound = dateTime.yearOfCentury();
                } else {
                    dateTime.addYears(sign * num);
                }
                break;
            case 'M':
                if (round) {
                    propertyToRound = dateTime.monthOfYear();
                } else {
                    dateTime.addMonths(sign * num);
                }
                break;
            case 'w':
                if (round) {
                    propertyToRound = dateTime.weekOfWeekyear();
                } else {
                    dateTime.addWeeks(sign * num);
                }
                break;
            case 'd':
                if (round) {
                    propertyToRound = dateTime.dayOfMonth();
                } else {
                    dateTime.addDays(sign * num);
                }
                break;
            case 'h':
            case 'H':
                if (round) {
                    propertyToRound = dateTime.hourOfDay();
                } else {
                    dateTime.addHours(sign * num);
                }
                break;
            case 'm':
                if (round) {
                    propertyToRound = dateTime.minuteOfHour();
                } else {
                    dateTime.addMinutes(sign * num);
                }
                break;
            case 's':
                if (round) {
                    propertyToRound = dateTime.secondOfMinute();
                } else {
                    dateTime.addSeconds(sign * num);
                }
                break;
            default:
                throw new ElasticsearchParseException("unit [{}] not supported for date math [{}]", unit, mathString);
        }
        if (propertyToRound != null) {
            if (roundUp) {
                // we want to go up to the next whole value, even if we are already on a rounded value
                propertyToRound.add(1);
                propertyToRound.roundFloor();
                // subtract 1 millisecond to get the largest inclusive value
                dateTime.addMillis(-1);
            } else {
                propertyToRound.roundFloor();
            }
        }
    }
    return dateTime.getMillis();
}
Also used : ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) MutableDateTime(org.joda.time.MutableDateTime)

Example 54 with ElasticsearchParseException

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

the class DecayFunctionBuilder method parseDateVariable.

private AbstractDistanceScoreFunction parseDateVariable(XContentParser parser, QueryShardContext context, MappedFieldType dateFieldType, MultiValueMode mode) throws IOException {
    XContentParser.Token token;
    String parameterName = null;
    String scaleString = null;
    String originString = null;
    String offsetString = "0d";
    double decay = 0.5;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            parameterName = parser.currentName();
        } else if (DecayFunctionBuilder.SCALE.equals(parameterName)) {
            scaleString = parser.text();
        } else if (DecayFunctionBuilder.ORIGIN.equals(parameterName)) {
            originString = parser.text();
        } else if (DecayFunctionBuilder.DECAY.equals(parameterName)) {
            decay = parser.doubleValue();
        } else if (DecayFunctionBuilder.OFFSET.equals(parameterName)) {
            offsetString = parser.text();
        } else {
            throw new ElasticsearchParseException("parameter [{}] not supported!", parameterName);
        }
    }
    long origin;
    if (originString == null) {
        origin = context.nowInMillis();
    } else {
        origin = ((DateFieldMapper.DateFieldType) dateFieldType).parseToMilliseconds(originString, false, null, null, context);
    }
    if (scaleString == null) {
        throw new ElasticsearchParseException("[{}] must be set for date fields.", DecayFunctionBuilder.SCALE);
    }
    TimeValue val = TimeValue.parseTimeValue(scaleString, TimeValue.timeValueHours(24), DecayFunctionParser.class.getSimpleName() + ".scale");
    double scale = val.getMillis();
    val = TimeValue.parseTimeValue(offsetString, TimeValue.timeValueHours(24), DecayFunctionParser.class.getSimpleName() + ".offset");
    double offset = val.getMillis();
    IndexNumericFieldData numericFieldData = context.getForField(dateFieldType);
    return new NumericFieldDataScoreFunction(origin, scale, decay, offset, getDecayFunction(), numericFieldData, mode);
}
Also used : DateFieldMapper(org.elasticsearch.index.mapper.DateFieldMapper) ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) IndexNumericFieldData(org.elasticsearch.index.fielddata.IndexNumericFieldData) XContentParser(org.elasticsearch.common.xcontent.XContentParser) TimeValue(org.elasticsearch.common.unit.TimeValue)

Example 55 with ElasticsearchParseException

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

the class DecayFunctionBuilder method parseNumberVariable.

private AbstractDistanceScoreFunction parseNumberVariable(XContentParser parser, QueryShardContext context, MappedFieldType fieldType, MultiValueMode mode) throws IOException {
    XContentParser.Token token;
    String parameterName = null;
    double scale = 0;
    double origin = 0;
    double decay = 0.5;
    double offset = 0.0d;
    boolean scaleFound = false;
    boolean refFound = false;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            parameterName = parser.currentName();
        } else if (DecayFunctionBuilder.SCALE.equals(parameterName)) {
            scale = parser.doubleValue();
            scaleFound = true;
        } else if (DecayFunctionBuilder.DECAY.equals(parameterName)) {
            decay = parser.doubleValue();
        } else if (DecayFunctionBuilder.ORIGIN.equals(parameterName)) {
            origin = parser.doubleValue();
            refFound = true;
        } else if (DecayFunctionBuilder.OFFSET.equals(parameterName)) {
            offset = parser.doubleValue();
        } else {
            throw new ElasticsearchParseException("parameter [{}] not supported!", parameterName);
        }
    }
    if (!scaleFound || !refFound) {
        throw new ElasticsearchParseException("both [{}] and [{}] must be set for numeric fields.", DecayFunctionBuilder.SCALE, DecayFunctionBuilder.ORIGIN);
    }
    IndexNumericFieldData numericFieldData = context.getForField(fieldType);
    return new NumericFieldDataScoreFunction(origin, scale, decay, offset, getDecayFunction(), numericFieldData, mode);
}
Also used : ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) IndexNumericFieldData(org.elasticsearch.index.fielddata.IndexNumericFieldData) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

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