
org.elasticsearch.script.MockScriptEngine Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of framework Show documentation
Show all versions of framework Show documentation
Elasticsearch subproject :test:framework
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.script;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.search.Scorer;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.search.lookup.LeafSearchLookup;
import org.elasticsearch.search.lookup.SearchLookup;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
/**
* A mocked script engine that can be used for testing purpose.
*
* This script engine allows to define a set of predefined scripts that basically a combination of a key and a
* function:
*
* The key can be anything as long as it is a {@link String} and is used to resolve the scripts
* at compilation time. For inline scripts, the key can be a description of the script. For stored and file scripts,
* the source must match a key in the predefined set of scripts.
*
* The function is used to provide the result of the script execution and can return anything.
*/
public class MockScriptEngine implements ScriptEngineService {
public static final String NAME = "mockscript";
private final String type;
private final Map, Object>> scripts;
public MockScriptEngine(String type, Map, Object>> scripts) {
this.type = type;
this.scripts = Collections.unmodifiableMap(scripts);
}
public MockScriptEngine() {
this(NAME, Collections.emptyMap());
}
@Override
public String getType() {
return type;
}
@Override
public String getExtension() {
return getType();
}
@Override
public Object compile(String name, String source, Map params) {
// Scripts are always resolved using the script's source. For inline scripts, it's easy because they don't have names and the
// source is always provided. For stored and file scripts, the source of the script must match the key of a predefined script.
Function
© 2015 - 2025 Weber Informatics LLC | Privacy Policy