Package org.elasticsearch.script

Examples of org.elasticsearch.script.ScriptParameterParser


        return NAMES;
    }

    @Override
    public ScoreFunction parse(QueryParseContext parseContext, XContentParser parser) throws IOException, QueryParsingException {
        ScriptParameterParser scriptParameterParser = new ScriptParameterParser();
        String script = null;
        String scriptLang = null;
        Map<String, Object> vars = null;
        ScriptService.ScriptType scriptType = null;
        String currentFieldName = null;
        XContentParser.Token token;
        while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
            if (token == XContentParser.Token.FIELD_NAME) {
                currentFieldName = parser.currentName();
            } else if (token == XContentParser.Token.START_OBJECT) {
                if ("params".equals(currentFieldName)) {
                    vars = parser.map();
                } else {
                    throw new QueryParsingException(parseContext.index(), NAMES[0] + " query does not support [" + currentFieldName + "]");
                }
            } else if (token.isValue()) {
                if (!scriptParameterParser.token(currentFieldName, token, parser)) {
                    throw new QueryParsingException(parseContext.index(), NAMES[0] + " query does not support [" + currentFieldName + "]");
                }
            }
        }

        ScriptParameterValue scriptValue = scriptParameterParser.getDefaultScriptParameterValue();
        if (scriptValue != null) {
            script = scriptValue.script();
            scriptType = scriptValue.scriptType();
        }
        scriptLang = scriptParameterParser.lang();

        if (script == null) {
            throw new QueryParsingException(parseContext.index(), NAMES[0] + " requires 'script' field");
        }
View Full Code Here


        return remainingFields.toString();
    }

    @SuppressWarnings("unchecked")
    private void parseTransform(DocumentMapper.Builder docBuilder, Map<String, Object> transformConfig) {
        ScriptParameterParser scriptParameterParser = new ScriptParameterParser();
        scriptParameterParser.parseConfig(transformConfig, true);
       
        String script = null;
        ScriptType scriptType = null;
        ScriptParameterValue scriptValue = scriptParameterParser.getDefaultScriptParameterValue();
        if (scriptValue != null) {
            script = scriptValue.script();
            scriptType = scriptValue.scriptType();
        }
       
        if (script != null) {
            String scriptLang = scriptParameterParser.lang();
            Map<String, Object> params = (Map<String, Object>)transformConfig.remove("params");
            docBuilder.transform(scriptService, script, scriptType, scriptLang, params);
        }
        if (!transformConfig.isEmpty()) {
            throw new MapperParsingException("Unrecognized parameter in transform config:  " + getRemainingFields(transformConfig));
View Full Code Here

    }

    @Override
    public Filter parse(QueryParseContext parseContext) throws IOException, QueryParsingException {
        XContentParser parser = parseContext.parser();
        ScriptParameterParser scriptParameterParser = new ScriptParameterParser();

        XContentParser.Token token;

        boolean cache = false; // no need to cache it by default, changes a lot?
        CacheKeyFilter.Key cacheKey = null;
        // also, when caching, since its isCacheable is false, will result in loading all bit set...
        String script = null;
        String scriptLang = null;
        Map<String, Object> params = null;

        String filterName = null;
        String currentFieldName = null;
        ScriptService.ScriptType scriptType = null;

        while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
            if (token == XContentParser.Token.FIELD_NAME) {
                currentFieldName = parser.currentName();
            } else if (token == XContentParser.Token.START_OBJECT) {
                if ("params".equals(currentFieldName)) {
                    params = parser.map();
                } else {
                    throw new QueryParsingException(parseContext.index(), "[script] filter does not support [" + currentFieldName + "]");
                }
            } else if (token.isValue()) {
                if ("_name".equals(currentFieldName)) {
                    filterName = parser.text();
                } else if ("_cache".equals(currentFieldName)) {
                    cache = parser.booleanValue();
                } else if ("_cache_key".equals(currentFieldName) || "_cacheKey".equals(currentFieldName)) {
                    cacheKey = new CacheKeyFilter.Key(parser.text());
                } else if (!scriptParameterParser.token(currentFieldName, token, parser)){
                    throw new QueryParsingException(parseContext.index(), "[script] filter does not support [" + currentFieldName + "]");
                }
            }
        }

        ScriptParameterValue scriptValue = scriptParameterParser.getDefaultScriptParameterValue();
        if (scriptValue != null) {
            script = scriptValue.script();
            scriptType = scriptValue.scriptType();
        }
        scriptLang = scriptParameterParser.lang();

        if (script == null) {
            throw new QueryParsingException(parseContext.index(), "script must be provided with a [script] filter");
        }
        if (params == null) {
View Full Code Here

        String consistencyLevel = request.param("consistency");
        if (consistencyLevel != null) {
            updateRequest.consistencyLevel(WriteConsistencyLevel.fromString(consistencyLevel));
        }
        updateRequest.docAsUpsert(request.paramAsBoolean("doc_as_upsert", updateRequest.docAsUpsert()));
        ScriptParameterParser scriptParameterParser = new ScriptParameterParser();
        scriptParameterParser.parseParams(request);
        ScriptParameterValue scriptValue = scriptParameterParser.getDefaultScriptParameterValue();
        if (scriptValue != null) {
            updateRequest.script(scriptValue.script(), scriptValue.scriptType());
        }
        String scriptLang = scriptParameterParser.lang();
        if (scriptLang != null) {
            updateRequest.scriptLang(scriptLang);
        }
        for (Map.Entry<String, String> entry : request.params().entrySet()) {
            if (entry.getKey().startsWith("sp_")) {
View Full Code Here

        Set<String> scriptParameters = new HashSet<>();
        scriptParameters.add(INIT_SCRIPT);
        scriptParameters.add(MAP_SCRIPT);
        scriptParameters.add(COMBINE_SCRIPT);
        scriptParameters.add(REDUCE_SCRIPT);
        ScriptParameterParser scriptParameterParser = new ScriptParameterParser(scriptParameters);

        while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
            if (token == XContentParser.Token.FIELD_NAME) {
                currentFieldName = parser.currentName();
            } else if (token == XContentParser.Token.START_OBJECT) {
                if (PARAMS_FIELD.match(currentFieldName)) {
                    params = parser.map();
                } else if (REDUCE_PARAMS_FIELD.match(currentFieldName)) {
                  reduceParams = parser.map();
                } else {
                    throw new SearchParseException(context, "Unknown key for a " + token + " in [" + aggregationName + "]: [" + currentFieldName + "].");
                }
            } else if (token.isValue()) {
                if (!scriptParameterParser.token(currentFieldName, token, parser)) {
                    throw new SearchParseException(context, "Unknown key for a " + token + " in [" + aggregationName + "]: [" + currentFieldName + "].");
                }
            } else {
                throw new SearchParseException(context, "Unexpected token " + token + " in [" + aggregationName + "].");
            }
        }
       
        ScriptParameterValue initScriptValue = scriptParameterParser.getScriptParameterValue(INIT_SCRIPT);
        String initScript = null;
        ScriptType initScriptType = null;
        if (initScriptValue != null) {
            initScript = initScriptValue.script();
            initScriptType = initScriptValue.scriptType();
        }
        ScriptParameterValue mapScriptValue = scriptParameterParser.getScriptParameterValue(MAP_SCRIPT);
        String mapScript = null;
        ScriptType mapScriptType = null;
        if (mapScriptValue != null) {
            mapScript = mapScriptValue.script();
            mapScriptType = mapScriptValue.scriptType();
        }
        ScriptParameterValue combineScriptValue = scriptParameterParser.getScriptParameterValue(COMBINE_SCRIPT);
        String combineScript = null;
        ScriptType combineScriptType = null;
        if (combineScriptValue != null) {
            combineScript = combineScriptValue.script();
            combineScriptType = combineScriptValue.scriptType();
        }
        ScriptParameterValue reduceScriptValue = scriptParameterParser.getScriptParameterValue(REDUCE_SCRIPT);
        String reduceScript = null;
        ScriptType reduceScriptType = null;
        if (reduceScriptValue != null) {
            reduceScript = reduceScriptValue.script();
            reduceScriptType = reduceScriptValue.scriptType();
        }
        scriptLang = scriptParameterParser.lang();
       
        if (mapScript == null) {
            throw new SearchParseException(context, "map_script field is required in [" + aggregationName + "].");
        }
        return new ScriptedMetricAggregator.Factory(aggregationName, scriptLang, initScriptType, initScript, mapScriptType, mapScript,
View Full Code Here

        while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
            if (token == XContentParser.Token.FIELD_NAME) {
                currentFieldName = parser.currentName();
            } else if (token == XContentParser.Token.START_OBJECT) {
                String fieldName = currentFieldName;
                ScriptParameterParser scriptParameterParser = new ScriptParameterParser();
                String script = null;
                String scriptLang = null;
                ScriptService.ScriptType scriptType = null;
                Map<String, Object> params = null;
                boolean ignoreException = false;
                while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                    if (token == XContentParser.Token.FIELD_NAME) {
                        currentFieldName = parser.currentName();
                    } else if (token == XContentParser.Token.START_OBJECT) {
                        params = parser.map();
                    } else if (token.isValue()) {
                        if ("ignore_failure".equals(currentFieldName)) {
                            ignoreException = parser.booleanValue();
                        } else {
                            scriptParameterParser.token(currentFieldName, token, parser);
                        }
                    }
                }

                ScriptParameterValue scriptValue = scriptParameterParser.getDefaultScriptParameterValue();
                if (scriptValue != null) {
                    script = scriptValue.script();
                    scriptType = scriptValue.scriptType();
                }
                scriptLang = scriptParameterParser.lang();
               
                SearchScript searchScript = context.scriptService().search(context.lookup(), scriptLang, script, scriptType, params);
                context.scriptFields().add(new ScriptFieldsContext.ScriptField(fieldName, searchScript, ignoreException));
            }
        }
View Full Code Here

    public boolean detectNoop() {
        return detectNoop;
    }

    public UpdateRequest source(BytesReference source) throws Exception {
        ScriptParameterParser scriptParameterParser = new ScriptParameterParser();
        XContentType xContentType = XContentFactory.xContentType(source);
        try (XContentParser parser = XContentFactory.xContent(xContentType).createParser(source)) {
            XContentParser.Token token = parser.nextToken();
            if (token == null) {
                return this;
            }
            String currentFieldName = null;
            while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                if (token == XContentParser.Token.FIELD_NAME) {
                    currentFieldName = parser.currentName();
                } else if ("params".equals(currentFieldName)) {
                    scriptParams = parser.map();
                } else if ("scripted_upsert".equals(currentFieldName)) {
                    scriptedUpsert = parser.booleanValue();
                } else if ("upsert".equals(currentFieldName)) {
                    XContentBuilder builder = XContentFactory.contentBuilder(xContentType);
                    builder.copyCurrentStructure(parser);
                    safeUpsertRequest().source(builder);
                } else if ("doc".equals(currentFieldName)) {
                    XContentBuilder docBuilder = XContentFactory.contentBuilder(xContentType);
                    docBuilder.copyCurrentStructure(parser);
                    safeDoc().source(docBuilder);
                } else if ("doc_as_upsert".equals(currentFieldName)) {
                    docAsUpsert(parser.booleanValue());
                } else if ("detect_noop".equals(currentFieldName)) {
                    detectNoop(parser.booleanValue());
                } else {
                    scriptParameterParser.token(currentFieldName, token, parser);
                }
            }
            ScriptParameterValue scriptValue = scriptParameterParser.getDefaultScriptParameterValue();
            if (scriptValue != null) {
                script = scriptValue.script();
                scriptType = scriptValue.scriptType();
            }
            scriptLang = scriptParameterParser.lang();
        }
        return this;
    }
View Full Code Here

TOP

Related Classes of org.elasticsearch.script.ScriptParameterParser

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.