How to use SearchLookup getSource(LeafReaderContext ctx, int doc)

In 8.7.0 the source() method was removed from SearchLookup:

How do I use the new getSource method in SearchLookup for a FilterScript given the following use case...

public class MyLeafFactory implements FilterScript.LeafFactory {

    private final Map<String, Object> params;
    private final SearchLookup lookup;

    public MyLeafFactory(Map<String, Object> params, SearchLookup lookup) {
        this.params = params;
        this.lookup = lookup;
        ...
    }

    @Override
    public FilterScript newInstance(DocReader docReader) {
        return new FilterScript(params, lookup, docReader) {
            @Override
            public boolean execute() {
                String someField = (String) lookup.source().get("someField");
                ...
                return true;
            }
        };
    }

For context this is a ScriptPlugin implemented as follows...

public class MyFilterPlugin extends Plugin implements ScriptPlugin {

    @Override
    public ScriptEngine getScriptEngine(Settings settings, Collection<ScriptContext<?>> contexts) {
        return new MyScriptEngine();
    }

    private static class MyScriptEngine implements ScriptEngine {
        ...
        @Override
        public <FactoryType> FactoryType compile(String name, String code, ScriptContext<FactoryType> context, Map<String, String> params) {
            if (context.equals(FilterScript.CONTEXT) == false) {
                throw new IllegalArgumentException(getType() + " scripts cannot be used for context [" + context.name + "]");
            }
            // we use the script "code" as the script identifier
            if ("mysearch-filter".equals(code)) {
                FilterScript.Factory factory = new MyFilterPlugin.MyScriptEngine.MyFactory();
                return context.factoryClazz.cast(factory);
            }
            throw new IllegalArgumentException("Unknown script name " + code);
        }
    
        private static class MyFactory implements FilterScript.Factory {
            @Override
            public FilterScript.LeafFactory newFactory(Map<String, Object> params, SearchLookup lookup) {
                return new MyLeafFactory(params, lookup);
            }
        }
    }

Kind regards,
Paul

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.