NationalSecurityAgency/ghidra · error · IllegalArgumentException

{getType()}scripts cannot be used for context [{context.name

Error message

{getType()}scripts cannot be used for context [{context.name}]

What it means

BSimScriptEngine is an Elasticsearch ScriptEngine that supports only the ScoreScript.CONTEXT (per-document scoring). compile checks the requested ScriptContext and rejects anything else with IllegalArgumentException, because BSim's vector-comparison logic only makes sense during scoring. getSupportedContexts confirms this by advertising just ScoreScript.CONTEXT.

Source

Thrown at Ghidra/Extensions/BSimElasticPlugin/src/org/elasticsearch/plugin/analysis/lsh/BSimScriptEngine.java:29

 * 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.plugin.analysis.lsh;

import java.util.*;

import org.elasticsearch.script.*;

public class BSimScriptEngine implements ScriptEngine {
	private final static String ENGINE_NAME = "bsim_scripts";

	@Override
	public <FactoryType> FactoryType compile(String scriptName, String scriptSource,
			ScriptContext<FactoryType> context, Map<String, String> params) {
		if (context.equals(ScoreScript.CONTEXT) == false) {
			throw new IllegalArgumentException(
				getType() + "scripts cannot be used for context [" + context.name + "]");
		}
		if (VectorCompareScriptFactory.SCRIPT_NAME.equals(scriptSource)) {
			ScoreScript.Factory factory = new VectorCompareScriptFactory();
			return context.factoryClazz.cast(factory);
		}
		throw new IllegalArgumentException("Unknown script name " + scriptSource);
	}

	@Override
	public void close() {
		// Can free up resources
	}

	@Override
	public Set<ScriptContext<?>> getSupportedContexts() {
		return Collections.singleton(ScoreScript.CONTEXT);
	}

View on GitHub (pinned to d5f144c24d)

Solutions

  1. Use the BSim script only within a score-script query (ScoreScript.CONTEXT).
  2. Verify getSupportedContexts() returns ScoreScript.CONTEXT and your query type uses that context.
  3. If you need non-scoring behavior, write a separate ScriptEngine for that context rather than reusing BSim's.

Example fix

// before: script compiled into an aggregation context -> throws
// after: issue the script via a score-script query (ScoreScript.CONTEXT)
ScriptScoreQueryBuilder q = ScriptScoreQuery.builder()
    .query(matchAllQuery())
    .script(new Script(ScriptType.INDEXED, "bsim_scripts", VectorCompareScriptFactory.SCRIPT_NAME, params))
    .build();
Defensive patterns

Strategy: validation

Validate before calling

// Only compile for the supported context:
if (!ScoreScript.CONTEXT.equals(requestedContext)) {
    throw new IllegalArgumentException("BSim scripts require ScoreScript.CONTEXT");
}

Prevention

When it happens

Trigger: Compiling/registering a BSim script under a non-Score context - e.g. an aggregation, ingest, or update context, or a query that dispatches the script to the wrong context.

Common situations: A search query hitting the wrong script context; a misconfigured scripted query; an Elasticsearch version change that routes the script through a different context; using the BSim script outside a score-script query.

Related errors


AI-assisted analysis of NationalSecurityAgency/ghidra@d5f144c24d (2026-08-14). Data as JSON: /api/errors/60a7e3106b130bc8. Report an issue: GitHub.