All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.codelibs.elasticsearch.sstmpl.chain.SearchTemplateChain Maven / Gradle / Ivy

There is a newer version: 7.16.0
Show newest version
package org.codelibs.elasticsearch.sstmpl.chain;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.codelibs.elasticsearch.sstmpl.ScriptTemplateException;
import org.codelibs.elasticsearch.sstmpl.filter.SearchTemplateFilter;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.script.CompiledScript;
import org.elasticsearch.script.ExecutableScript;
import org.elasticsearch.script.Script;
import org.elasticsearch.script.ScriptContext;
import org.elasticsearch.script.ScriptService;
import org.elasticsearch.script.ScriptService.ScriptType;
import org.elasticsearch.search.internal.SearchContext;

public class SearchTemplateChain {

    private ScriptService scriptService;

    private SearchTemplateFilter[] filters;

    int position = 0;

    public SearchTemplateChain(final ScriptService scriptService,
            final SearchTemplateFilter[] filters) {
        this.scriptService = scriptService;
        this.filters = filters;
    }

    public String doCreate(final String lang, final String script,
            final ScriptType scriptType, final Map paramMap) {
        if (position < filters.length) {
            final SearchTemplateFilter filter = filters[position];
            position++;
            return filter.doCreate(lang, script, scriptType, paramMap, this);
        } else {
            final CompiledScript compiledScript = scriptService.compile(
                    new Script(script, scriptType, lang,
                            new HashMap()),
                    ScriptContext.Standard.SEARCH, SearchContext.current(),
                    Collections. emptyMap());
            final Map vars;
            if (paramMap != null) {
                vars = paramMap;
            } else {
                vars = Collections.emptyMap();
            }
            final ExecutableScript executable = scriptService.executable(
                    compiledScript, vars);
            final Object result = executable.run();
            if (result instanceof String) {
                return (String) result;
            } else if (result instanceof BytesReference) {
                return ((BytesReference) result).toUtf8();
            } else {
                throw new ScriptTemplateException("Query DSL is null.");
            }
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy