|
9 | 9 |
|
10 | 10 | package org.elasticsearch.test.errorquery; |
11 | 11 |
|
| 12 | +import org.elasticsearch.common.settings.Settings; |
| 13 | +import org.elasticsearch.index.mapper.OnScriptError; |
| 14 | +import org.elasticsearch.plugins.Plugin; |
| 15 | +import org.elasticsearch.plugins.ScriptPlugin; |
12 | 16 | import org.elasticsearch.plugins.SearchPlugin; |
13 | | -import org.elasticsearch.test.FailingFieldPlugin; |
| 17 | +import org.elasticsearch.script.LongFieldScript; |
| 18 | +import org.elasticsearch.script.ScriptContext; |
| 19 | +import org.elasticsearch.script.ScriptEngine; |
| 20 | +import org.elasticsearch.search.lookup.SearchLookup; |
14 | 21 |
|
| 22 | +import java.util.Collection; |
15 | 23 | import java.util.List; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.Set; |
16 | 26 |
|
17 | 27 | import static java.util.Collections.singletonList; |
18 | 28 |
|
19 | 29 | /** |
20 | 30 | * Test plugin that exposes a way to simulate search shard failures and warnings. |
21 | 31 | */ |
22 | | -public class ErrorQueryPlugin extends FailingFieldPlugin implements SearchPlugin { |
| 32 | +public class ErrorQueryPlugin extends Plugin implements SearchPlugin, ScriptPlugin { |
23 | 33 | public ErrorQueryPlugin() {} |
24 | 34 |
|
25 | 35 | @Override |
26 | 36 | public List<QuerySpec<?>> getQueries() { |
27 | 37 | return singletonList(new QuerySpec<>(ErrorQueryBuilder.NAME, ErrorQueryBuilder::new, p -> ErrorQueryBuilder.PARSER.parse(p, null))); |
28 | 38 | } |
| 39 | + |
| 40 | + public static final String FAILING_FIELD_LANG = "failing_field"; |
| 41 | + |
| 42 | + @Override |
| 43 | + public ScriptEngine getScriptEngine(Settings settings, Collection<ScriptContext<?>> contexts) { |
| 44 | + return new ScriptEngine() { |
| 45 | + @Override |
| 46 | + public String getType() { |
| 47 | + return FAILING_FIELD_LANG; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + @SuppressWarnings("unchecked") |
| 52 | + public <FactoryType> FactoryType compile( |
| 53 | + String name, |
| 54 | + String code, |
| 55 | + ScriptContext<FactoryType> context, |
| 56 | + Map<String, String> params |
| 57 | + ) { |
| 58 | + return (FactoryType) new LongFieldScript.Factory() { |
| 59 | + @Override |
| 60 | + public LongFieldScript.LeafFactory newFactory( |
| 61 | + String fieldName, |
| 62 | + Map<String, Object> params, |
| 63 | + SearchLookup searchLookup, |
| 64 | + OnScriptError onScriptError |
| 65 | + ) { |
| 66 | + return ctx -> new LongFieldScript(fieldName, params, searchLookup, onScriptError, ctx) { |
| 67 | + @Override |
| 68 | + public void execute() { |
| 69 | + throw new IllegalStateException("Accessing failing field"); |
| 70 | + } |
| 71 | + }; |
| 72 | + } |
| 73 | + }; |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public Set<ScriptContext<?>> getSupportedContexts() { |
| 78 | + return Set.of(LongFieldScript.CONTEXT); |
| 79 | + } |
| 80 | + }; |
| 81 | + } |
29 | 82 | } |
0 commit comments