Package org.elasticsearch.script

Examples of org.elasticsearch.script.ScriptException


                binding.getVariables().putAll(vars);
            }
            scriptObject.setBinding(binding);
            return new GroovyExecutableScript(scriptObject);
        } catch (Exception e) {
            throw new ScriptException("failed to build executable script", e);
        }
    }
View Full Code Here


                binding.getVariables().putAll(vars);
            }
            scriptObject.setBinding(binding);
            return new GroovySearchScript(scriptObject, lookup);
        } catch (Exception e) {
            throw new ScriptException("failed to build search script", e);
        }
    }
View Full Code Here

            Script scriptObject = (Script) scriptClass.newInstance();
            Binding binding = new Binding(vars);
            scriptObject.setBinding(binding);
            return scriptObject.run();
        } catch (Exception e) {
            throw new ScriptException("failed to execute script", e);
        }
    }
View Full Code Here

                if (type.equals(DataTypes.DOUBLE)) {
                    return ((ScriptDocValues.Doubles)docValues).getValue();
                } else if (type.equals(DataTypes.LONG)) {
                    return ((ScriptDocValues.Longs)docValues).getValue();
                } else {
                    throw new ScriptException(
                            String.format("Field data type not supported"));
                }
            }
        }
View Full Code Here

    }

    @Override
    protected Context prepare(@Nullable Map<String, Object> params) {
        if (params == null) {
            throw new ScriptException("Parameter required");
        }

        String operatorName = XContentMapValues.nodeStringValue(params.get("op"), null);
        if (operatorName == null) {
            throw new ScriptException("Missing the op parameter");
        }

        // extract operator arguments
        if (!params.containsKey("args") || !XContentMapValues.isArray(params.get("args"))) {
            throw new ScriptException(String.format(Locale.ENGLISH, "No parameters given for operator %s", operatorName));
        }
        List args = (List)params.get("args");
        List<WrappedArgument> operatorArgs = new ArrayList<>(args.size());
        List<DataType> operatorArgTypes = new ArrayList<>(args.size());
        for (Object arg : args) {
            assert arg instanceof Map;
            WrappedArgument argument = getArgument((Map<String, Object>)arg);
            operatorArgs.add(argument);
            // TODO: use real types from argument here
            operatorArgTypes.add(DataTypes.DOUBLE);
        }

        // first argument should be the scalar
        if (!(operatorArgs.get(0) instanceof ScalarArgument)) {
            throw new ScriptException("first argument in search script no scalar!");
        }
        ScalarArgument function = ((ScalarArgument) operatorArgs.get(0));

        // resolve operator
        FunctionIdent operatorIdent = new FunctionIdent(operatorName, operatorArgTypes);
        Operator operator = (Operator)functions.get(operatorIdent);
        if (operator == null) {
            throw new ScriptException(String.format("Cannot resolve operator with ident %s", operatorIdent));
        }

        return new SearchContext(function, operator, operatorArgs);
    }
View Full Code Here

        }

        @Override
        protected Context prepare(@Nullable Map<String, Object> params) {
            if (params == null) {
                throw new ScriptException("Parameter required");
            }
            Object scalar = params.get("scalar");
            if (scalar == null || !(scalar instanceof Map)) {
                throw new ScriptException("invalid sort scalar format");
            }
            WrappedArgument arg = getArgument((Map<String, Object>)scalar);
            // first argument should be the scalar
            if (!(arg instanceof ScalarArgument)) {
                throw new ScriptException("no scalar used in sort script!");
            }
            return new Context((ScalarArgument) arg);
        }
View Full Code Here

        try {
            return doRun();
        } catch (ScriptException e) {
            throw e; // rethrow
        } catch (Exception e) {
            throw new ScriptException(e.getMessage(), e);
        }
    }
View Full Code Here

     * @return
     */
    private static DataType extractDataType(Map<String, Object> params, String name) {
        Object type = params.get(name);
        if (type == null) {
            throw new ScriptException(String.format(Locale.ENGLISH, "Could not extract %s parameter", name));
        }
        DataType dataType = DataTypes.ofJsonObject(type);
        if (dataType.id() == ByteType.ID || (dataType.id() >= ShortType.ID && dataType.id() <= TimestampType.ID)) {
            dataType = LongType.INSTANCE;
        } else if (dataType.id() >= DoubleType.ID && dataType.id() <= FloatType.ID) {
View Full Code Here

            return new LiteralArgument(Literal.newLiteral(type, type.value(argSpec.get("value"))));
        } else if (argSpec.containsKey("scalar_name")) {

            String scalarName = XContentMapValues.nodeStringValue(argSpec.get("scalar_name"), null);
            if (scalarName == null) {
                throw new ScriptException(String.format("No scalar_name given"));
            }
            List<WrappedArgument> wrappedArgs = ImmutableList.of();
            List<DataType> argumentTypes = ImmutableList.of();
            if (argSpec.containsKey("args") && XContentMapValues.isArray(argSpec.get("args"))) {
                List args = (List)argSpec.get("args");
                argumentTypes = new ArrayList<>(args.size());
                wrappedArgs = new ArrayList<>(args.size());
                for (Object arg : args) {
                    assert arg instanceof Map;
                    WrappedArgument argument = getArgument((Map<String, Object>)arg);
                    argumentTypes.add(argument.getType());
                    wrappedArgs.add(argument);
                }
            }
            Scalar scalar = getScalar(scalarName, argumentTypes);
            if (scalar == null) {
                throw new ScriptException(String.format("Cannot resolve function %s", scalarName));
            }
            return new ScalarArgument(scalar, type, wrappedArgs);

        } else if (argSpec.containsKey("field_name")) {
            String fieldName = XContentMapValues.nodeStringValue(argSpec.get("field_name"), null);
            if (fieldName == null) {
                throw new ScriptException("Missing the field_name parameter");
            }
            return new ReferenceArgument(fieldName, type);
        } else {
            throw new ScriptException("invalid parameter format");
        }
    }
View Full Code Here

TOP

Related Classes of org.elasticsearch.script.ScriptException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.