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

**URL:** <https://discuss.elastic.co/t/how-to-use-searchlookup-getsource-leafreadercontext-ctx-int-doc/331072>\
**Category:** Elasticsearch\
**Created:** [April 28, 2023, 3:57pm UTC](https://discuss.elastic.co/t/how-to-use-searchlookup-getsource-leafreadercontext-ctx-int-doc/331072 "2023-04-28T15:57:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![p4paul](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/p4paul/32/65034_2.png) [@p4paul](https://discuss.elastic.co/u/p4paul)\
**Post date:** [April 28, 2023, 3:57pm UTC](https://discuss.elastic.co/t/how-to-use-searchlookup-getsource-leafreadercontext-ctx-int-doc/331072/1 "2023-04-28T15:57:41Z")

</div>

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

> <https://github.com/elastic/elasticsearch/commit/c720cdbbbf175b0dc07a70a8332038e0d1751eb6#r111001100>
>
> SourceLookup mixes up several concerns - lazy loading, map access to scripts,
> d…ifferent access providers - and duplicates logic (such as that choosing how to
> apply filtering) that is better handled directly in the Source interface.
> 
> This commit removes SourceLookup entirely and replaces it with a new
> SourceProvider interface, with a simple stored fields reader implementation.
> SearchLookup implements this interface directly, and the fetch phase uses
> a custom implementation to provide its separately loaded source to fetch-time
> scripts.

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

```auto
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...

```auto
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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 26, 2023, 3:58pm UTC](https://discuss.elastic.co/t/how-to-use-searchlookup-getsource-leafreadercontext-ctx-int-doc/331072/2 "2023-05-26T15:58:08Z")

</div>

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