org.elasticsearch.search.SearchParseException Java Examples

The following examples show how to use org.elasticsearch.search.SearchParseException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: NumericValuesSourceMetricsAggregatorParser.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public AggregatorFactory parse(String aggregationName, XContentParser parser, SearchContext context) throws IOException {

    ValuesSourceParser<ValuesSource.Numeric> vsParser = ValuesSourceParser.numeric(aggregationName, aggType, context).formattable(true)
            .build();

    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 (!vsParser.token(currentFieldName, token, parser)) {
            throw new SearchParseException(context, "Unexpected token " + token + " in [" + aggregationName + "].",
                    parser.getTokenLocation());
        }
    }

    return createFactory(aggregationName, vsParser.config());
}
 
Example #2
Source File: ExportParser.java    From elasticsearch-inout-plugin with Apache License 2.0 6 votes vote down vote up
/**
 * validate given payload
 *
 * @param context
 */
private void validate(ExportContext context) {
    if (!context.hasFieldNames()) {
        throw new SearchParseException(context, "No export fields defined");
    }
    for (String field : context.fieldNames()) {
        if (context.mapperService().name(field) == null && !field.equals("_version")) {
            throw new SearchParseException(context, "Export field [" + field + "] does not exist in the mapping");
        }
    }
    if (context.outputFile() != null) {
        if (context.outputCmdArray() != null || context.outputCmd() != null) {
            throw new SearchParseException(context, "Concurrent definition of 'output_cmd' and 'output_file'");
        }
    } else if (context.outputCmdArray() == null && context.outputCmd() == null) {
        throw new SearchParseException(context, "'output_cmd' or 'output_file' has not been defined");
    } else if (context.outputFile() == null && context.settings()) {
        throw new SearchParseException(context, "Parameter 'settings' requires usage of 'output_file'");
    } else if (context.outputFile() == null && context.mappings()) {
        throw new SearchParseException(context, "Parameter 'mappings' requires usage of 'output_file'");
    }
}
 
Example #3
Source File: SearchIntoParser.java    From elasticsearch-inout-plugin with Apache License 2.0 6 votes vote down vote up
@Override
protected void validate(SearchIntoContext context) {
    if (!context.hasFieldNames()) {
        throw new SearchParseException(context, "No fields defined");
    }

    for (String field : context.fieldNames()) {
        FieldMapper<?> mapper = context.mapperService().smartNameFieldMapper(
                field);
        if (mapper == null && !field.equals(
                "_version") && !field.startsWith(
                FieldsParseElement.SCRIPT_FIELD_PREFIX)) {
            throw new SearchParseException(context,
                    "SearchInto field [" + field + "] does not exist in " +
                            "the mapping");
        }
    }
    super.validate(context);
}
 
Example #4
Source File: MissingParser.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public AggregatorFactory parse(String aggregationName, XContentParser parser, SearchContext context) throws IOException {

    ValuesSourceParser vsParser = ValuesSourceParser.any(aggregationName, InternalMissing.TYPE, context)
            .scriptable(false)
            .build();

    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 (vsParser.token(currentFieldName, token, parser)) {
            continue;
        } else {
            throw new SearchParseException(context, "Unexpected token " + token + " in [" + aggregationName + "].",
                    parser.getTokenLocation());
        }
    }

    return new MissingAggregator.Factory(aggregationName, vsParser.config());
}
 
Example #5
Source File: GeoCentroidParser.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public AggregatorFactory parse(String aggregationName, XContentParser parser, SearchContext context) throws IOException {
    ValuesSourceParser<ValuesSource.GeoPoint> vsParser = ValuesSourceParser.geoPoint(aggregationName, InternalGeoCentroid.TYPE, context)
            .targetValueType(ValueType.GEOPOINT)
            .formattable(true)
            .build();
    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 (vsParser.token(currentFieldName, token, parser)) {
            continue;
        } else {
            throw new SearchParseException(context, "Unknown key for a " + token + " in aggregation [" + aggregationName + "]: ["
                    + currentFieldName + "].", parser.getTokenLocation());
        }
    }
    return new GeoCentroidAggregator.Factory(aggregationName, vsParser.config());
}
 
Example #6
Source File: ValueCountParser.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public AggregatorFactory parse(String aggregationName, XContentParser parser, SearchContext context) throws IOException {

    ValuesSourceParser vsParser = ValuesSourceParser.any(aggregationName, InternalValueCount.TYPE, context)
            .build();

    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 (!vsParser.token(currentFieldName, token, parser)) {
            throw new SearchParseException(context, "Unexpected token " + token + " in [" + aggregationName + "].",
                    parser.getTokenLocation());
        }
    }

    return new ValueCountAggregator.Factory(aggregationName, vsParser.config());
}
 
Example #7
Source File: SignificantTermsParametersParser.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public void parseSpecial(String aggregationName, XContentParser parser, SearchContext context, XContentParser.Token token, String currentFieldName) throws IOException {

    if (token == XContentParser.Token.START_OBJECT) {
        SignificanceHeuristicParser significanceHeuristicParser = significanceHeuristicParserMapper.get(currentFieldName);
        if (significanceHeuristicParser != null) {
            significanceHeuristic = significanceHeuristicParser.parse(parser, context.parseFieldMatcher(), context);
        } else if (context.parseFieldMatcher().match(currentFieldName, BACKGROUND_FILTER)) {
            filter = context.queryParserService().parseInnerFilter(parser).query();
        } else {
            throw new SearchParseException(context, "Unknown key for a " + token + " in [" + aggregationName + "]: ["
                    + currentFieldName + "].", parser.getTokenLocation());
        }
    } else {
        throw new SearchParseException(context, "Unknown key for a " + token + " in [" + aggregationName + "]: [" + currentFieldName
                + "].", parser.getTokenLocation());
    }
}
 
Example #8
Source File: ReverseNestedParser.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public AggregatorFactory parse(String aggregationName, XContentParser parser, SearchContext context) throws IOException {
    String path = null;

    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 (token == XContentParser.Token.VALUE_STRING) {
            if ("path".equals(currentFieldName)) {
                path = parser.text();
            } else {
                throw new SearchParseException(context, "Unknown key for a " + token + " in [" + aggregationName + "]: ["
                        + currentFieldName + "].", parser.getTokenLocation());
            }
        } else {
            throw new SearchParseException(context, "Unexpected token " + token + " in [" + aggregationName + "].",
                    parser.getTokenLocation());
        }
    }

    return new ReverseNestedAggregator.Factory(aggregationName, path);
}
 
Example #9
Source File: ReverseNestedAggregator.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public Aggregator createInternal(AggregationContext context, Aggregator parent, boolean collectsFromSingleBucket,
        List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) throws IOException {
    // Early validation
    NestedAggregator closestNestedAggregator = findClosestNestedAggregator(parent);
    if (closestNestedAggregator == null) {
        throw new SearchParseException(context.searchContext(), "Reverse nested aggregation [" + name
                + "] can only be used inside a [nested] aggregation", null);
    }

    final ObjectMapper objectMapper;
    if (path != null) {
        objectMapper = context.searchContext().getObjectMapper(path);
        if (objectMapper == null) {
            return new Unmapped(name, context, parent, pipelineAggregators, metaData);
        }
        if (!objectMapper.nested().isNested()) {
            throw new AggregationExecutionException("[reverse_nested] nested path [" + path + "] is not nested");
        }
    } else {
        objectMapper = null;
    }
    return new ReverseNestedAggregator(name, factories, objectMapper, context, parent, pipelineAggregators, metaData);
}
 
Example #10
Source File: HighlighterParseElement.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void parse(XContentParser parser, SearchContext context) throws Exception {
    try {
        context.highlight(parse(parser, context.queryParserService()));
    } catch (IllegalArgumentException ex) {
        throw new SearchParseException(context, "Error while trying to parse Highlighter element in request", parser.getTokenLocation());
    }
}
 
Example #11
Source File: ScriptedMetricAggregator.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({ "unchecked" })
private static <T> T deepCopyParams(T original, SearchContext context) {
    T clone;
    if (original instanceof Map) {
        Map<?, ?> originalMap = (Map<?, ?>) original;
        Map<Object, Object> clonedMap = new HashMap<>();
        for (Entry<?, ?> e : originalMap.entrySet()) {
            clonedMap.put(deepCopyParams(e.getKey(), context), deepCopyParams(e.getValue(), context));
        }
        clone = (T) clonedMap;
    } else if (original instanceof List) {
        List<?> originalList = (List<?>) original;
        List<Object> clonedList = new ArrayList<Object>();
        for (Object o : originalList) {
            clonedList.add(deepCopyParams(o, context));
        }
        clone = (T) clonedList;
    } else if (original instanceof String || original instanceof Integer || original instanceof Long || original instanceof Short
            || original instanceof Byte || original instanceof Float || original instanceof Double || original instanceof Character
            || original instanceof Boolean) {
        clone = original;
    } else {
        throw new SearchParseException(context, "Can only clone primitives, String, ArrayList, and HashMap. Found: "
                + original.getClass().getCanonicalName(), null);
    }
    return clone;
}
 
Example #12
Source File: TermsParametersParser.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private OrderElement parseOrderParam(String aggregationName, XContentParser parser, SearchContext context) throws IOException {
    XContentParser.Token token;
    OrderElement orderParam = null;
    String orderKey = null;
    boolean orderAsc = false;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            orderKey = parser.currentName();
        } else if (token == XContentParser.Token.VALUE_STRING) {
            String dir = parser.text();
            if ("asc".equalsIgnoreCase(dir)) {
                orderAsc = true;
            } else if ("desc".equalsIgnoreCase(dir)) {
                orderAsc = false;
            } else {
                throw new SearchParseException(context, "Unknown terms order direction [" + dir + "] in terms aggregation ["
                        + aggregationName + "]", parser.getTokenLocation());
            }
        } else {
            throw new SearchParseException(context, "Unexpected token " + token + " for [order] in [" + aggregationName + "].",
                    parser.getTokenLocation());
        }
    }
    if (orderKey == null) {
        throw new SearchParseException(context, "Must specify at least one field for [order] in [" + aggregationName + "].",
                parser.getTokenLocation());
    } else {
        orderParam = new OrderElement(orderKey, orderAsc);
    }
    return orderParam;
}
 
Example #13
Source File: NestedParser.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public AggregatorFactory parse(String aggregationName, XContentParser parser, SearchContext context) throws IOException {
    String path = null;

    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 (token == XContentParser.Token.VALUE_STRING) {
            if ("path".equals(currentFieldName)) {
                path = parser.text();
            } else {
                throw new SearchParseException(context, "Unknown key for a " + token + " in [" + aggregationName + "]: ["
                        + currentFieldName + "].", parser.getTokenLocation());
            }
        } else {
            throw new SearchParseException(context, "Unexpected token " + token + " in [" + aggregationName + "].",
                    parser.getTokenLocation());
        }
    }

    if (path == null) {
        // "field" doesn't exist, so we fall back to the context of the ancestors
        throw new SearchParseException(context, "Missing [path] field for nested aggregation [" + aggregationName + "]",
                parser.getTokenLocation());
    }

    return new NestedAggregator.Factory(aggregationName, path);
}
 
Example #14
Source File: SizeParseElement.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void parse(XContentParser parser, SearchContext context) throws Exception {
    XContentParser.Token token = parser.currentToken();
    if (token.isValue()) {
        int size = parser.intValue();
        if (size < 0) {
            throw new SearchParseException(context, "size is set to [" + size + "] and is expected to be higher or equal to 0",
                    parser.getTokenLocation());
        }
        context.size(size);
    }
}
 
Example #15
Source File: FromParseElement.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void parse(XContentParser parser, SearchContext context) throws Exception {
    XContentParser.Token token = parser.currentToken();
    if (token.isValue()) {
        int from = parser.intValue();
        if (from < 0) {
            throw new SearchParseException(context, "from is set to [" + from + "] and is expected to be higher or equal to 0",
                    parser.getTokenLocation());
        }
        context.from(from);
    }
}
 
Example #16
Source File: TermsParametersParser.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void parseSpecial(String aggregationName, XContentParser parser, SearchContext context, XContentParser.Token token, String currentFieldName) throws IOException {
    if (token == XContentParser.Token.START_OBJECT) {
        if ("order".equals(currentFieldName)) {
            this.orderElements = Collections.singletonList(parseOrderParam(aggregationName, parser, context));
        } else {
            throw new SearchParseException(context, "Unknown key for a " + token + " in [" + aggregationName + "]: ["
                    + currentFieldName + "].", parser.getTokenLocation());
        }
    } else if (token == XContentParser.Token.START_ARRAY) {
        if ("order".equals(currentFieldName)) {
            orderElements = new ArrayList<>();
            while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
                if (token == XContentParser.Token.START_OBJECT) {
                    OrderElement orderParam = parseOrderParam(aggregationName, parser, context);
                    orderElements.add(orderParam);
                } else {
                    throw new SearchParseException(context, "Order elements must be of type object in [" + aggregationName + "].",
                            parser.getTokenLocation());
                }
            }
        } else {
            throw new SearchParseException(context, "Unknown key for a " + token + " in [" + aggregationName + "]: ["
                    + currentFieldName + "].", parser.getTokenLocation());
        }
    } else if (token == XContentParser.Token.VALUE_BOOLEAN) {
        if (context.parseFieldMatcher().match(currentFieldName, SHOW_TERM_DOC_COUNT_ERROR)) {
            showTermDocCountError = parser.booleanValue();
        }
    } else {
        throw new SearchParseException(context, "Unknown key for a " + token + " in [" + aggregationName + "]: [" + currentFieldName
                + "].", parser.getTokenLocation());
    }
}
 
Example #17
Source File: ReindexParser.java    From elasticsearch-inout-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public void parseSource(SearchIntoContext context, BytesReference source)
        throws SearchParseException {
    context.fieldNames().add("_id");
    context.fieldNames().add("_source");
    context.outputNames().put("_id", "_id");
    context.outputNames().put("_source", "_source");
    super.parseSource(context, source);
}
 
Example #18
Source File: AbstractSearchIntoParser.java    From elasticsearch-inout-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * Validate the pay load of the search-into context.
 * @param context
 */
protected void validate(SearchIntoContext context) {
    if (context.hasFieldNames() && context.fieldNames().contains("_source")) {
        String index = context.mapperService().index().getName();
        for (String type : context.mapperService().types()) {
            if (!context.mapperService().documentMapper(type).sourceMapper().enabled()) {
                throw new SearchParseException(context,
                        "The _source field of index " + index + " and type " + type + " is not stored.");
            }
        }
    }
}
 
Example #19
Source File: ExportCompressionParseElement.java    From elasticsearch-inout-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public void parse(XContentParser parser, SearchContext context)
        throws Exception {
    XContentParser.Token token = parser.currentToken();
    if (token.isValue()) {
        String lower = parser.text().toLowerCase();
        if (lower.equals("gzip")) {
            ((ExportContext) context).compression(true);
        } else if (!lower.isEmpty()) {
            throw new SearchParseException(context,
                    "Compression format '" + lower + "' unknown or not supported.");
        }
    }
}
 
Example #20
Source File: ExportParser.java    From elasticsearch-inout-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * Main method of this class to parse given payload of _export action
 *
 * @param context
 * @param source
 * @throws SearchParseException
 */
public void parseSource(ExportContext context, BytesReference source) throws SearchParseException {
    XContentParser parser = null;
    try {
        if (source != null && source.length() != 0) {
            parser = XContentFactory.xContent(source).createParser(source);
            XContentParser.Token token;
            while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                if (token == XContentParser.Token.FIELD_NAME) {
                    String fieldName = parser.currentName();
                    parser.nextToken();
                    SearchParseElement element = elementParsers.get(fieldName);
                    if (element == null) {
                        throw new SearchParseException(context, "No parser for element [" + fieldName + "]");
                    }
                    element.parse(parser, context);
                } else if (token == null) {
                    break;
                }
            }
        }
        validate(context);
    } catch (Exception e) {
        String sSource = "_na_";
        try {
            sSource = XContentHelper.convertToJson(source, false);
        } catch (Throwable e1) {
            // ignore
        }
        throw new SearchParseException(context, "Failed to parse source [" + sSource + "]", e);
    } finally {
        if (parser != null) {
            parser.close();
        }
    }
}
 
Example #21
Source File: GeoBoundsParser.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public AggregatorFactory parse(String aggregationName, XContentParser parser, SearchContext context) throws IOException {
    ValuesSourceParser<GeoPoint> vsParser = ValuesSourceParser.geoPoint(aggregationName, InternalGeoBounds.TYPE, context)
            .targetValueType(ValueType.GEOPOINT)
            .formattable(true)
            .build();
    boolean wrapLongitude = true;
    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 (vsParser.token(currentFieldName, token, parser)) {
            continue;
            
        } else if (token == XContentParser.Token.VALUE_BOOLEAN) {
            if ("wrap_longitude".equals(currentFieldName) || "wrapLongitude".equals(currentFieldName)) {
                wrapLongitude = parser.booleanValue();
            } else {
                throw new SearchParseException(context, "Unknown key for a " + token + " in aggregation [" + aggregationName + "]: ["
                        + currentFieldName + "].", parser.getTokenLocation());
            }
        } else {
            throw new SearchParseException(context, "Unknown key for a " + token + " in aggregation [" + aggregationName + "]: ["
                    + currentFieldName + "].", parser.getTokenLocation());
        }
    }
    return new GeoBoundsAggregator.Factory(aggregationName, vsParser.config(), wrapLongitude);
}
 
Example #22
Source File: IpRangeParser.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private static void parseMaskRange(String cidr, RangeAggregator.Range range, String aggregationName, SearchContext ctx) {
    long[] fromTo = IpFieldMapper.cidrMaskToMinMax(cidr);
    if (fromTo == null) {
        throw new SearchParseException(ctx, "invalid CIDR mask [" + cidr + "] in aggregation [" + aggregationName + "]",
                null);
    }
    range.from = fromTo[0] < 0 ? Double.NEGATIVE_INFINITY : fromTo[0];
    range.to = fromTo[1] < 0 ? Double.POSITIVE_INFINITY : fromTo[1];
    if (range.key == null) {
        range.key = cidr;
    }
}
 
Example #23
Source File: PercentileRanksParser.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
protected AggregatorFactory buildFactory(SearchContext context, String aggregationName, ValuesSourceConfig<Numeric> valuesSourceConfig,
        double[] keys, PercentilesMethod method, Double compression, Integer numberOfSignificantValueDigits, boolean keyed) {
    if (keys == null) {
        throw new SearchParseException(context, "Missing token values in [" + aggregationName + "].", null);
    }
    if (method == PercentilesMethod.TDIGEST) {
        return new TDigestPercentileRanksAggregator.Factory(aggregationName, valuesSourceConfig, keys, compression, keyed);
    } else if (method == PercentilesMethod.HDR) {
        return new HDRPercentileRanksAggregator.Factory(aggregationName, valuesSourceConfig, keys, numberOfSignificantValueDigits,
                keyed);
    } else {
        throw new AssertionError();
    }
}
 
Example #24
Source File: CardinalityParser.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public AggregatorFactory parse(String name, XContentParser parser, SearchContext context) throws IOException {

    ValuesSourceParser<?> vsParser = ValuesSourceParser.any(name, InternalCardinality.TYPE, context).formattable(false).build();

    long precisionThreshold = -1;

    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 (vsParser.token(currentFieldName, token, parser)) {
            continue;
        } else if (token.isValue()) {
            if (context.parseFieldMatcher().match(currentFieldName, REHASH)) {
                // ignore
            } else if (context.parseFieldMatcher().match(currentFieldName, PRECISION_THRESHOLD)) {
                precisionThreshold = parser.longValue();
            } else {
                throw new SearchParseException(context, "Unknown key for a " + token + " in [" + name + "]: [" + currentFieldName
                        + "].", parser.getTokenLocation());
            }
        } else {
            throw new SearchParseException(context, "Unexpected token " + token + " in [" + name + "].", parser.getTokenLocation());
        }
    }

    return new CardinalityAggregatorFactory(name, vsParser.config(), precisionThreshold);

}
 
Example #25
Source File: ExtendedStatsParser.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public AggregatorFactory parse(String aggregationName, XContentParser parser, SearchContext context) throws IOException {

    ValuesSourceParser<ValuesSource.Numeric> vsParser = ValuesSourceParser.numeric(aggregationName, InternalExtendedStats.TYPE, context).formattable(true)
            .build();

    XContentParser.Token token;
    String currentFieldName = null;
    double sigma = 2.0;

    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (vsParser.token(currentFieldName, token, parser)) {
            continue;
        } else if (token == XContentParser.Token.VALUE_NUMBER) {
            if (context.parseFieldMatcher().match(currentFieldName, SIGMA)) {
                sigma = parser.doubleValue();
            } else {
                throw new SearchParseException(context, "Unknown key for a " + token + " in [" + aggregationName + "]: ["
                        + currentFieldName + "].", parser.getTokenLocation());
            }
        } else {
            throw new SearchParseException(context, "Unexpected token " + token + " in [" + aggregationName + "].",
                    parser.getTokenLocation());
        }
    }

    if (sigma < 0) {
        throw new SearchParseException(context, "[sigma] must not be negative. Value provided was" + sigma, parser.getTokenLocation());
    }

    return createFactory(aggregationName, vsParser.config(), sigma);
}
 
Example #26
Source File: SerialDiffParser.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public PipelineAggregatorFactory parse(String reducerName, XContentParser parser, SearchContext context) throws IOException {
    XContentParser.Token token;
    String currentFieldName = null;
    String[] bucketsPaths = null;
    String format = null;
    GapPolicy gapPolicy = GapPolicy.SKIP;
    int lag = 1;

    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (token == XContentParser.Token.VALUE_STRING) {
            if (context.parseFieldMatcher().match(currentFieldName, FORMAT)) {
                format = parser.text();
            } else if (context.parseFieldMatcher().match(currentFieldName, BUCKETS_PATH)) {
                bucketsPaths = new String[] { parser.text() };
            } else if (context.parseFieldMatcher().match(currentFieldName, GAP_POLICY)) {
                gapPolicy = GapPolicy.parse(context, parser.text(), parser.getTokenLocation());
            } else {
                throw new SearchParseException(context, "Unknown key for a " + token + " in [" + reducerName + "]: ["
                        + currentFieldName + "].", parser.getTokenLocation());
            }
        } else if (token == XContentParser.Token.VALUE_NUMBER) {
            if (context.parseFieldMatcher().match(currentFieldName, LAG)) {
                lag = parser.intValue(true);
                if (lag <= 0) {
                    throw new SearchParseException(context, "Lag must be a positive, non-zero integer.  Value supplied was" +
                            lag + " in [" + reducerName + "]: ["
                            + currentFieldName + "].", parser.getTokenLocation());
                }
            }  else {
                throw new SearchParseException(context, "Unknown key for a " + token + " in [" + reducerName + "]: ["
                        + currentFieldName + "].", parser.getTokenLocation());
            }
        } else if (token == XContentParser.Token.START_ARRAY) {
            if (context.parseFieldMatcher().match(currentFieldName, BUCKETS_PATH)) {
                List<String> paths = new ArrayList<>();
                while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
                    String path = parser.text();
                    paths.add(path);
                }
                bucketsPaths = paths.toArray(new String[paths.size()]);
            } else {
                throw new SearchParseException(context, "Unknown key for a " + token + " in [" + reducerName + "]: ["
                        + currentFieldName + "].", parser.getTokenLocation());
            }
        } else {
            throw new SearchParseException(context, "Unexpected token " + token + " in [" + reducerName + "].",
                    parser.getTokenLocation());
        }
    }

    if (bucketsPaths == null) {
        throw new SearchParseException(context, "Missing required field [" + BUCKETS_PATH.getPreferredName()
                + "] for derivative aggregation [" + reducerName + "]", parser.getTokenLocation());
    }

    ValueFormatter formatter;
    if (format != null) {
        formatter = ValueFormat.Patternable.Number.format(format).formatter();
    }  else {
        formatter = ValueFormatter.RAW;
    }

    return new SerialDiffPipelineAggregator.Factory(reducerName, bucketsPaths, formatter, gapPolicy, lag);
}
 
Example #27
Source File: TopHitsParser.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public AggregatorFactory parse(String aggregationName, XContentParser parser, SearchContext context) throws IOException {
    SubSearchContext subSearchContext = new SubSearchContext(context);
    XContentParser.Token token;
    String currentFieldName = null;
    try {
        while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
            if (token == XContentParser.Token.FIELD_NAME) {
                currentFieldName = parser.currentName();
            } else if ("sort".equals(currentFieldName)) {
                sortParseElement.parse(parser, subSearchContext);
            } else if ("_source".equals(currentFieldName)) {
                sourceParseElement.parse(parser, subSearchContext);
            } else if ("fields".equals(currentFieldName)) {
                fieldsParseElement.parse(parser, subSearchContext);
            } else if (token.isValue()) {
                switch (currentFieldName) {
                    case "from":
                        subSearchContext.from(parser.intValue());
                        break;
                    case "size":
                        subSearchContext.size(parser.intValue());
                        break;
                    case "track_scores":
                    case "trackScores":
                        subSearchContext.trackScores(parser.booleanValue());
                        break;
                    case "version":
                        subSearchContext.version(parser.booleanValue());
                        break;
                    case "explain":
                        subSearchContext.explain(parser.booleanValue());
                        break;
                    default:
                    throw new SearchParseException(context, "Unknown key for a " + token + " in [" + aggregationName + "]: ["
                            + currentFieldName + "].", parser.getTokenLocation());
                }
            } else if (token == XContentParser.Token.START_OBJECT) {
                switch (currentFieldName) {
                    case "highlight":
                        highlighterParseElement.parse(parser, subSearchContext);
                        break;
                    case "scriptFields":
                    case "script_fields":
                        scriptFieldsParseElement.parse(parser, subSearchContext);
                        break;
                    default:
                    throw new SearchParseException(context, "Unknown key for a " + token + " in [" + aggregationName + "]: ["
                            + currentFieldName + "].", parser.getTokenLocation());
                }
            } else if (token == XContentParser.Token.START_ARRAY) {
                switch (currentFieldName) {
                    case "fielddataFields":
                    case "fielddata_fields":
                        fieldDataFieldsParseElement.parse(parser, subSearchContext);
                        break;
                    default:
                    throw new SearchParseException(context, "Unknown key for a " + token + " in [" + aggregationName + "]: ["
                            + currentFieldName + "].", parser.getTokenLocation());
                }
            } else {
                throw new SearchParseException(context, "Unexpected token " + token + " in [" + aggregationName + "].",
                        parser.getTokenLocation());
            }
        }
    } catch (Exception e) {
        throw ExceptionsHelper.convertToElastic(e);
    }
    return new TopHitsAggregator.Factory(aggregationName, fetchPhase, subSearchContext);
}
 
Example #28
Source File: AbstractSearchIntoParser.java    From elasticsearch-inout-plugin with Apache License 2.0 4 votes vote down vote up
/**
 * Main method of this class to parse given payload of _search_into action
 *
 * @param context
 * @param source
 * @throws org.elasticsearch.search.SearchParseException
 *
 */
public void parseSource(SearchIntoContext context,
        BytesReference source) throws SearchParseException {
    XContentParser parser = null;
    try {
        if (source != null) {
            parser = XContentFactory.xContent(source).createParser(source);
            XContentParser.Token token;
            while ((token = parser.nextToken()) != XContentParser.Token
                    .END_OBJECT) {
                if (token == XContentParser.Token.FIELD_NAME) {
                    String fieldName = parser.currentName();
                    parser.nextToken();
                    SearchParseElement element = getElementParsers().get(
                            fieldName);
                    if (element == null) {
                        throw new SearchParseException(context,
                                "No parser for element [" + fieldName +
                                        "]");
                    }
                    element.parse(parser, context);
                } else if (token == null) {
                    break;
                }
            }
        }
        validate(context);
    } catch (Exception e) {
        String sSource = "_na_";
        try {
            sSource = XContentHelper.convertToJson(source, false);
        } catch (Throwable e1) {
            // ignore
        }
        throw new SearchParseException(context,
                "Failed to parse source [" + sSource + "]", e);
    } finally {
        if (parser != null) {
            parser.close();
        }
    }
}
 
Example #29
Source File: CumulativeSumParser.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public PipelineAggregatorFactory parse(String pipelineAggregatorName, XContentParser parser, SearchContext context) throws IOException {
    XContentParser.Token token;
    String currentFieldName = null;
    String[] bucketsPaths = null;
    String format = null;

    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (token == XContentParser.Token.VALUE_STRING) {
            if (context.parseFieldMatcher().match(currentFieldName, FORMAT)) {
                format = parser.text();
            } else if (context.parseFieldMatcher().match(currentFieldName, BUCKETS_PATH)) {
                bucketsPaths = new String[] { parser.text() };
            } else {
                throw new SearchParseException(context, "Unknown key for a " + token + " in [" + pipelineAggregatorName + "]: ["
                        + currentFieldName + "].", parser.getTokenLocation());
            }
        } else if (token == XContentParser.Token.START_ARRAY) {
            if (context.parseFieldMatcher().match(currentFieldName, BUCKETS_PATH)) {
                List<String> paths = new ArrayList<>();
                while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
                    String path = parser.text();
                    paths.add(path);
                }
                bucketsPaths = paths.toArray(new String[paths.size()]);
            } else {
                throw new SearchParseException(context, "Unknown key for a " + token + " in [" + pipelineAggregatorName + "]: ["
                        + currentFieldName + "].", parser.getTokenLocation());
            }
        } else {
            throw new SearchParseException(context, "Unexpected token " + token + " in [" + pipelineAggregatorName + "].",
                    parser.getTokenLocation());
        }
    }

    if (bucketsPaths == null) {
        throw new SearchParseException(context, "Missing required field [" + BUCKETS_PATH.getPreferredName()
                + "] for derivative aggregation [" + pipelineAggregatorName + "]", parser.getTokenLocation());
    }

    ValueFormatter formatter = null;
    if (format != null) {
        formatter = ValueFormat.Patternable.Number.format(format).formatter();
    } else {
        formatter = ValueFormatter.RAW;
    }

    return new CumulativeSumPipelineAggregator.Factory(pipelineAggregatorName, bucketsPaths, formatter);
}
 
Example #30
Source File: DumpParser.java    From elasticsearch-inout-plugin with Apache License 2.0 4 votes vote down vote up
/**
 * Main method of this class to parse given payload of _dump action
 *
 * @param context
 * @param source
 * @throws SearchParseException
 */
public void parseSource(ExportContext context, BytesReference source) throws SearchParseException {
    XContentParser parser = null;
    this.setDefaults(context);
    try {
        if (source != null && source.length() != 0) {
            parser = XContentFactory.xContent(source).createParser(source);
            XContentParser.Token token;
            while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                if (token == XContentParser.Token.FIELD_NAME) {
                    String fieldName = parser.currentName();
                    parser.nextToken();
                    SearchParseElement element = elementParsers.get(fieldName);
                    if (element == null) {
                        throw new SearchParseException(context, "No parser for element [" + fieldName + "]");
                    }
                    element.parse(parser, context);
                } else if (token == null) {
                    break;
                }
            }
        }
        if (context.outputFile() == null) {
            directoryParseElement.setOutPutFile(context, DEFAULT_DIR);
            this.ensureDefaultDirectory(context);
        }
        context.mappings(true);
        context.settings(true);
    } catch (Exception e) {
        String sSource = "_na_";
        try {
            sSource = XContentHelper.convertToJson(source, false);
        } catch (Throwable e1) {
            // ignore
        }
        throw new SearchParseException(context, "Failed to parse source [" + sSource + "]", e);
    } finally {
        if (parser != null) {
            parser.close();
        }
    }
}