Search in sources :

Example 1 with ScriptContext

use of org.opensearch.script.ScriptContext in project OpenSearch by opensearch-project.

the class NeedsScoreTests method testNeedsScores.

public void testNeedsScores() {
    IndexService index = createIndex("test", Settings.EMPTY, "type", "d", "type=double");
    Map<ScriptContext<?>, List<Whitelist>> contexts = new HashMap<>();
    contexts.put(NumberSortScript.CONTEXT, Whitelist.BASE_WHITELISTS);
    PainlessScriptEngine service = new PainlessScriptEngine(Settings.EMPTY, contexts);
    QueryShardContext shardContext = index.newQueryShardContext(0, null, () -> 0, null);
    NumberSortScript.Factory factory = service.compile(null, "1.2", NumberSortScript.CONTEXT, Collections.emptyMap());
    NumberSortScript.LeafFactory ss = factory.newFactory(Collections.emptyMap(), shardContext.lookup());
    assertFalse(ss.needs_score());
    factory = service.compile(null, "doc['d'].value", NumberSortScript.CONTEXT, Collections.emptyMap());
    ss = factory.newFactory(Collections.emptyMap(), shardContext.lookup());
    assertFalse(ss.needs_score());
    factory = service.compile(null, "1/_score", NumberSortScript.CONTEXT, Collections.emptyMap());
    ss = factory.newFactory(Collections.emptyMap(), shardContext.lookup());
    assertTrue(ss.needs_score());
    factory = service.compile(null, "doc['d'].value * _score", NumberSortScript.CONTEXT, Collections.emptyMap());
    ss = factory.newFactory(Collections.emptyMap(), shardContext.lookup());
    assertTrue(ss.needs_score());
}
Also used : IndexService(org.opensearch.index.IndexService) HashMap(java.util.HashMap) ScriptContext(org.opensearch.script.ScriptContext) QueryShardContext(org.opensearch.index.query.QueryShardContext) List(java.util.List) NumberSortScript(org.opensearch.script.NumberSortScript)

Example 2 with ScriptContext

use of org.opensearch.script.ScriptContext in project OpenSearch by opensearch-project.

the class IntervalQueryBuilderTests method testScriptFilter.

public void testScriptFilter() throws IOException {
    IntervalFilterScript.Factory factory = () -> new IntervalFilterScript() {

        @Override
        public boolean execute(Interval interval) {
            return interval.getStart() > 3;
        }
    };
    ScriptService scriptService = new ScriptService(Settings.EMPTY, Collections.emptyMap(), Collections.emptyMap()) {

        @Override
        @SuppressWarnings("unchecked")
        public <FactoryType> FactoryType compile(Script script, ScriptContext<FactoryType> context) {
            assertEquals(IntervalFilterScript.CONTEXT, context);
            assertEquals(new Script("interval.start > 3"), script);
            return (FactoryType) factory;
        }
    };
    QueryShardContext baseContext = createShardContext();
    QueryShardContext context = new QueryShardContext(baseContext.getShardId(), baseContext.getIndexSettings(), BigArrays.NON_RECYCLING_INSTANCE, null, null, baseContext.getMapperService(), null, scriptService, null, null, null, null, null, null, null, () -> true, null);
    String json = "{ \"intervals\" : { \"" + TEXT_FIELD_NAME + "\": { " + "\"match\" : { " + "   \"query\" : \"term1\"," + "   \"filter\" : { " + "       \"script\" : { " + "            \"source\" : \"interval.start > 3\" } } } } } }";
    IntervalQueryBuilder builder = (IntervalQueryBuilder) parseQuery(json);
    Query q = builder.toQuery(context);
    IntervalQuery expected = new IntervalQuery(TEXT_FIELD_NAME, new IntervalsSourceProvider.ScriptFilterSource(Intervals.term("term1"), "interval.start > 3", null));
    assertEquals(expected, q);
}
Also used : Script(org.opensearch.script.Script) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) IntervalQuery(org.apache.lucene.queries.intervals.IntervalQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) IntervalQuery(org.apache.lucene.queries.intervals.IntervalQuery) ScriptContext(org.opensearch.script.ScriptContext) ScriptService(org.opensearch.script.ScriptService)

Example 3 with ScriptContext

use of org.opensearch.script.ScriptContext in project OpenSearch by opensearch-project.

the class BindingsTests method beforeClass.

@BeforeClass
public static void beforeClass() {
    Map<ScriptContext<?>, List<Whitelist>> contexts = newDefaultContexts();
    List<Whitelist> allowlists = new ArrayList<>(Whitelist.BASE_WHITELISTS);
    allowlists.add(WhitelistLoader.loadFromResourceFiles(Whitelist.class, "org.opensearch.painless.test"));
    InstanceBindingTestClass instanceBindingTestClass = new InstanceBindingTestClass(1);
    WhitelistInstanceBinding getter = new WhitelistInstanceBinding("test", instanceBindingTestClass, "setInstanceBindingValue", "void", Collections.singletonList("int"), Collections.emptyList());
    WhitelistInstanceBinding setter = new WhitelistInstanceBinding("test", instanceBindingTestClass, "getInstanceBindingValue", "int", Collections.emptyList(), Collections.emptyList());
    List<WhitelistInstanceBinding> instanceBindingsList = new ArrayList<>();
    instanceBindingsList.add(getter);
    instanceBindingsList.add(setter);
    Whitelist instanceBindingsAllowlist = new Whitelist(instanceBindingTestClass.getClass().getClassLoader(), Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), instanceBindingsList);
    allowlists.add(instanceBindingsAllowlist);
    contexts.put(BindingsTestScript.CONTEXT, allowlists);
    SCRIPT_ENGINE = new PainlessScriptEngine(Settings.EMPTY, contexts);
}
Also used : ArrayList(java.util.ArrayList) ScriptContext(org.opensearch.script.ScriptContext) Whitelist(org.opensearch.painless.spi.Whitelist) WhitelistInstanceBinding(org.opensearch.painless.spi.WhitelistInstanceBinding) List(java.util.List) ArrayList(java.util.ArrayList) BeforeClass(org.junit.BeforeClass)

Example 4 with ScriptContext

use of org.opensearch.script.ScriptContext in project OpenSearch by opensearch-project.

the class PredicateTokenScriptFilterTests method testSimpleFilter.

public void testSimpleFilter() throws IOException {
    Settings settings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).build();
    Settings indexSettings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).put("index.analysis.filter.f.type", "predicate_token_filter").put("index.analysis.filter.f.script.source", "my_script").put("index.analysis.analyzer.myAnalyzer.type", "custom").put("index.analysis.analyzer.myAnalyzer.tokenizer", "standard").putList("index.analysis.analyzer.myAnalyzer.filter", "f").build();
    IndexSettings idxSettings = IndexSettingsModule.newIndexSettings("index", indexSettings);
    AnalysisPredicateScript.Factory factory = () -> new AnalysisPredicateScript() {

        @Override
        public boolean execute(Token token) {
            return token.getPosition() < 2 || token.getPosition() > 4;
        }
    };
    @SuppressWarnings("unchecked") ScriptService scriptService = new ScriptService(indexSettings, Collections.emptyMap(), Collections.emptyMap()) {

        @Override
        public <FactoryType> FactoryType compile(Script script, ScriptContext<FactoryType> context) {
            assertEquals(context, AnalysisPredicateScript.CONTEXT);
            assertEquals(new Script("my_script"), script);
            return (FactoryType) factory;
        }
    };
    CommonAnalysisPlugin plugin = new CommonAnalysisPlugin();
    plugin.createComponents(null, null, null, null, scriptService, null, null, null, null, null, null);
    AnalysisModule module = new AnalysisModule(TestEnvironment.newEnvironment(settings), Collections.singletonList(plugin));
    IndexAnalyzers analyzers = module.getAnalysisRegistry().build(idxSettings);
    try (NamedAnalyzer analyzer = analyzers.get("myAnalyzer")) {
        assertNotNull(analyzer);
        assertAnalyzesTo(analyzer, "Oh what a wonderful thing to be", new String[] { "Oh", "what", "to", "be" });
    }
}
Also used : Script(org.opensearch.script.Script) NamedAnalyzer(org.opensearch.index.analysis.NamedAnalyzer) IndexSettings(org.opensearch.index.IndexSettings) ScriptContext(org.opensearch.script.ScriptContext) ScriptService(org.opensearch.script.ScriptService) IndexAnalyzers(org.opensearch.index.analysis.IndexAnalyzers) AnalysisModule(org.opensearch.indices.analysis.AnalysisModule) Settings(org.opensearch.common.settings.Settings) IndexSettings(org.opensearch.index.IndexSettings)

Example 5 with ScriptContext

use of org.opensearch.script.ScriptContext in project OpenSearch by opensearch-project.

the class ScriptedConditionTokenFilterTests method testSimpleCondition.

public void testSimpleCondition() throws Exception {
    Settings settings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).build();
    Settings indexSettings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).put("index.analysis.filter.cond.type", "condition").put("index.analysis.filter.cond.script.source", "token.getPosition() > 1").putList("index.analysis.filter.cond.filter", "uppercase").put("index.analysis.analyzer.myAnalyzer.type", "custom").put("index.analysis.analyzer.myAnalyzer.tokenizer", "standard").putList("index.analysis.analyzer.myAnalyzer.filter", "cond").build();
    IndexSettings idxSettings = IndexSettingsModule.newIndexSettings("index", indexSettings);
    AnalysisPredicateScript.Factory factory = () -> new AnalysisPredicateScript() {

        @Override
        public boolean execute(Token token) {
            return token.getPosition() > 1;
        }
    };
    @SuppressWarnings("unchecked") ScriptService scriptService = new ScriptService(indexSettings, Collections.emptyMap(), Collections.emptyMap()) {

        @Override
        public <FactoryType> FactoryType compile(Script script, ScriptContext<FactoryType> context) {
            assertEquals(context, AnalysisPredicateScript.CONTEXT);
            assertEquals(new Script("token.getPosition() > 1"), script);
            return (FactoryType) factory;
        }
    };
    CommonAnalysisPlugin plugin = new CommonAnalysisPlugin();
    plugin.createComponents(null, null, null, null, scriptService, null, null, null, null, null, null);
    AnalysisModule module = new AnalysisModule(TestEnvironment.newEnvironment(settings), Collections.singletonList(plugin));
    IndexAnalyzers analyzers = module.getAnalysisRegistry().build(idxSettings);
    try (NamedAnalyzer analyzer = analyzers.get("myAnalyzer")) {
        assertNotNull(analyzer);
        assertAnalyzesTo(analyzer, "Vorsprung Durch Technik", new String[] { "Vorsprung", "Durch", "TECHNIK" });
    }
}
Also used : Script(org.opensearch.script.Script) NamedAnalyzer(org.opensearch.index.analysis.NamedAnalyzer) IndexSettings(org.opensearch.index.IndexSettings) ScriptContext(org.opensearch.script.ScriptContext) ScriptService(org.opensearch.script.ScriptService) IndexAnalyzers(org.opensearch.index.analysis.IndexAnalyzers) AnalysisModule(org.opensearch.indices.analysis.AnalysisModule) Settings(org.opensearch.common.settings.Settings) IndexSettings(org.opensearch.index.IndexSettings)

Aggregations

ScriptContext (org.opensearch.script.ScriptContext)6 Script (org.opensearch.script.Script)4 ScriptService (org.opensearch.script.ScriptService)3 List (java.util.List)2 Settings (org.opensearch.common.settings.Settings)2 IndexSettings (org.opensearch.index.IndexSettings)2 IndexAnalyzers (org.opensearch.index.analysis.IndexAnalyzers)2 NamedAnalyzer (org.opensearch.index.analysis.NamedAnalyzer)2 AnalysisModule (org.opensearch.indices.analysis.AnalysisModule)2 Mustache (com.github.mustachejava.Mustache)1 MustacheException (com.github.mustachejava.MustacheException)1 MustacheFactory (com.github.mustachejava.MustacheFactory)1 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 AccessController (java.security.AccessController)1 PrivilegedAction (java.security.PrivilegedAction)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1