# NPE в AbstractSearchScript при использовании из ScriptEngineService

**URL:** <https://discuss.elastic.co/t/npe-abstractsearchscript-scriptengineservice/104740>\
**Category:** Вопросы на русском языке\
**Created:** [October 20, 2017, 4:51pm UTC](https://discuss.elastic.co/t/npe-abstractsearchscript-scriptengineservice/104740 "2017-10-20T16:51:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ponimas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ponimas/32/23247_2.png) [@ponimas](https://discuss.elastic.co/u/ponimas)\
**Post date:** [October 20, 2017, 4:51pm UTC](https://discuss.elastic.co/t/npe-abstractsearchscript-scriptengineservice/104740/1 "2017-10-20T16:51:21Z")

</div>

Здравствуйте!  
Пытаюсь разобраться со script engine.  
Написал такой вот минимальный пример.

```auto
    private static class MyScriptEngine implements ScriptEngineService {
        @Override
        public String getType() {
            return "scripts";
        }

        @Override
        public Function<Map<String,Object>, SearchScript> compile(String scriptName, String scriptSource, Map<String, String> params) {
            return p -> new SearchScript() {
                    @Override
                    public boolean needsScores() {
                        return false;
                    }

                    @Override
                    public LeafSearchScript getLeafSearchScript(LeafReaderContext context) throws IOException {
                        return new AbstractSearchScript() {
                            @Override
                            public Object run() {
                                System.out.println("Booo");
                                return 1;
                            }
                        };
                    }
                };
        }

        @Override
        @SuppressWarnings("unchecked")
        public SearchScript search(CompiledScript compiledScript, SearchLookup lookup, @Nullable Map<String, Object> params) {
            Function<Map<String,Object>, SearchScript> scriptFactory = (Function<Map<String,Object>,SearchScript>) compiledScript.compiled();
            return scriptFactory.apply(params);
        }

        @Override
        public ExecutableScript executable(CompiledScript compiledScript, @Nullable Map<String, Object> params) {
            throw new UnsupportedOperationException();

        }

        @Override
        public boolean isInlineScriptEnabled() {
            return true;
        }

        @Override
        public void close() {
        }
    }

```

пытаюсь получить скриптовое поле

```auto
{
    "_source": false,
    "query": {
    },
    "script_fields": {
        "test": {
            "script": {
                "source": "_",
                "lang": "scripts",
                "params": {
                }
            }
        }
    }
}

```

но получаю NPE.

[2017-10-20T19:47:42,171][DEBUG][o.e.a.s.TransportSearchAction] [8nIomLL] [4] Failed to execute fetch phase org.elasticsearch.transport.RemoteTransportException: [8nIomLL][127.0.0.1:9300][indices:data/read/search[phase/fetch/id]] Caused by: java.lang.NullPointerException at org.elasticsearch.script.AbstractSearchScript.setDocument(AbstractSearchScript.java:115) ~[elasticsearch-5.6.3.jar:5.6.3] at org.elasticsearch.search.fetch.subphase.ScriptFieldsFetchSubPhase.hitExecute(ScriptFieldsFetchSubPhase.java:47) ~[elasticsearch-5.6.3.jar:5.6.3] at org.elasticsearch.search.fetch.FetchPhase.execute(FetchPhase.java:165) ~[elasticsearch-5.6.3.jar:5.6.3] at org.elasticsearch.search.SearchService.executeFetchPhase(SearchService.java:426) ~[elasticsearch-5.6.3.jar:5.6.3] at org.elasticsearch.action.search.SearchTransportService$12.messageReceived(SearchTransportService.java:407) ~[elasticsearch-5.6.3.jar:5.6.3] at org.elasticsearch.action.search.SearchTransportService$12.messageReceived(SearchTransportService.java:404) ~[elasticsearch-5.6.3.jar:5.6.3] at org.elasticsearch.transport.RequestHandlerRegistry.processMessageReceived(RequestHandlerRegistry.java:69) ~[elasticsearch-5.6.3.jar:5.6.3] at org.elasticsearch.transport.TransportService$7.doRun(TransportService.java:644) [elasticsearch-5.6.3.jar:5.6.3] at org.elasticsearch.common.util.concurrent.ThreadContext$ContextPreservingAbstractRunnable.doRun(ThreadContext.java:638) [elasticsearch-5.6.3.jar:5.6.3] at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:37) [elasticsearch-5.6.3.jar:5.6.3] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [?:1.8.0\_112] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [?:1.8.0\_112] at java.lang.Thread.run(Thread.java:745) [?:1.8.0\_112]

Что я делаю не так?

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [October 21, 2017, 5:59pm UTC](https://discuss.elastic.co/t/npe-abstractsearchscript-scriptengineservice/104740/2 "2017-10-21T17:59:33Z")

</div>

У вас `leafLookup` не установелен после создания `AbstractSearchScript`. Посмотрите пример [тут](https://github.com/elastic/elasticsearch/blob/58f7357fc154017ee9b4fa3918d107cbae641a66/test/framework/src/main/java/org/elasticsearch/script/MockScriptEngine.java#L211).

---

<div class="post-metadata">

**Author:** ![ponimas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ponimas/32/23247_2.png) [@ponimas](https://discuss.elastic.co/u/ponimas)\
**Post date:** [October 23, 2017, 11:45am UTC](https://discuss.elastic.co/t/npe-abstractsearchscript-scriptengineservice/104740/3 "2017-10-23T11:45:40Z")

</div>

А`setLookup` ведь не публичный метод, как в этом случае поступить?

Возможно я глупости спрашиваю, прошу заранее извинить.

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [October 24, 2017, 1:35pm UTC](https://discuss.elastic.co/t/npe-abstractsearchscript-scriptengineservice/104740/4 "2017-10-24T13:35:43Z")

</div>

Вам не обязательно использовать `AbstractSearchScript`, главное реализовать `LeafSearchScript`. Например, как в [`JavaScriptSearchScript`](https://github.com/elastic/elasticsearch/blob/c7ccde351d61a081c10397c3c3aec61d2e3cd5fa/plugins/lang-javascript/src/main/java/org/elasticsearch/script/javascript/JavaScriptScriptEngineService.java#L272).

---

<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:** [November 21, 2017, 1:35pm UTC](https://discuss.elastic.co/t/npe-abstractsearchscript-scriptengineservice/104740/5 "2017-11-21T13:35:48Z")

</div>

Closed on November 21, 2017, 1:35pm UTC

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