# How to inject node object in elasticsearch 5.1.1 native script plugin

**URL:** <https://discuss.elastic.co/t/how-to-inject-node-object-in-elasticsearch-5-1-1-native-script-plugin/99393>\
**Category:** Elasticsearch\
**Created:** [September 5, 2017, 9:22am UTC](https://discuss.elastic.co/t/how-to-inject-node-object-in-elasticsearch-5-1-1-native-script-plugin/99393 "2017-09-05T09:22:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chandan\_Kar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chandan_kar/32/81051_2.png) [@Chandan\_Kar](https://discuss.elastic.co/u/Chandan_Kar)\
**Post date:** [September 5, 2017, 9:22am UTC](https://discuss.elastic.co/t/how-to-inject-node-object-in-elasticsearch-5-1-1-native-script-plugin/99393/1 "2017-09-05T09:22:05Z")

</div>

i am trying to create a elasticsearch native script plugin. The plugin should be able to execute some search query, something like "GetResponse response = client.prepareGet(lookupIndex, lookupType, fieldValue).setFields(value\_field).setPreference("\_local").execute().actionGet();"

But i am not able to get or create Client object inside that script.

Please help!!!

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [September 5, 2017, 7:27pm UTC](https://discuss.elastic.co/t/how-to-inject-node-object-in-elasticsearch-5-1-1-native-script-plugin/99393/2 "2017-09-05T19:27:28Z")

</div>

Can you explain more what you have tried? You will need to create the client yourself, either a REST client or a transport client. A link to actual code of your plugin would be most helpful.

---

<div class="post-metadata">

**Author:** ![Chandan\_Kar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chandan_kar/32/81051_2.png) [@Chandan\_Kar](https://discuss.elastic.co/u/Chandan_Kar)\
**Post date:** [September 13, 2017, 6:38am UTC](https://discuss.elastic.co/t/how-to-inject-node-object-in-elasticsearch-5-1-1-native-script-plugin/99393/3 "2017-09-13T06:38:57Z")

</div>

[//LookupNew.java](https://LookupNew.java)  
package com.chandan.es.nativescript.script;

import java.util.Map;

import org.apache.logging.log4j.LogManager;  
import org.apache.logging.log4j.Logger;  
import org.elasticsearch.common.component.AbstractComponent;  
import org.elasticsearch.common.inject.Inject;  
import org.elasticsearch.common.settings.Settings;  
import org.elasticsearch.common.xcontent.support.XContentMapValues;  
import org.elasticsearch.index.fielddata.ScriptDocValues;  
import org.elasticsearch.node.Node;  
import org.elasticsearch.script.AbstractSearchScript;  
import org.elasticsearch.script.ExecutableScript;  
import org.elasticsearch.script.NativeScriptFactory;

public class LookupNew extends AbstractSearchScript implements ExecutableScript {

```
private final static Logger log = LogManager.getLogger(LookupNew.class);

private final Node node;
private final Map<String, Object> params;
private final Settings settings;

@Inject
public LookupNew(Node node, Map<String, Object> params, Settings settings) {
	this.params = params;
	this.settings = settings;
	this.node = node;

	log.info("LookupScript constructor");
}

@SuppressWarnings("unchecked")
@Override
public Object run() {

	ScriptDocValues<String> docValue = (ScriptDocValues<String>) doc().get(params.get("lookup_field"));
	final String fieldValue = ((ScriptDocValues.Strings) docValue).getValue();

	try {
		log.info("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
		String lookupIndex = XContentMapValues.nodeStringValue(params.get("lookup_index"), null);
		String lookupType = XContentMapValues.nodeStringValue(params.get("lookup_type"), null);
		String lookup_field = XContentMapValues.nodeStringValue(params.get("lookup_field"), null);
		String value_field = XContentMapValues.nodeStringValue(params.get("value_field"), null);

		//############################################################################################
		/*
		 Here i want have code like this
		 Client client = node.client();
		  
		 Depending on that 'client' object i wanto perform some operation. 
		  
		 
		 */
		//############################################################################################
	

	} catch (Exception e) {
		e.printStackTrace();
	} 

	return docValue;
}

public static class Factory extends AbstractComponent implements NativeScriptFactory {

	private final Logger log = LogManager.getLogger(Factory.class);
	private Node node;

	@Inject
	public Factory(Settings settings) {
		super(settings);

	}

	@Override
	public ExecutableScript newScript(Map<String, Object> params) {
		return new LookupNew(node, params, settings);
	}

	@Override
	public boolean needsScores() {
		return false;
	}

	@Override
	public String getName() {
		return "lookup";
	}

}

```

}

[//NativeScriptExamplesPlugin.java](https://NativeScriptExamplesPlugin.java)  
package com.chandan.es.nativescript.plugin;

import static com.chandan.es.nativescript.script.IsPrimeSearchScriptFactory.PRIME\_SCRIPT\_DEFAULT\_FIELD\_NAME;

import java.util.Arrays;  
import java.util.Collection;  
import java.util.Collections;  
import java.util.List;

import org.elasticsearch.common.inject.Inject;  
import org.elasticsearch.common.inject.Module;  
import org.elasticsearch.common.settings.Setting;  
import org.elasticsearch.common.settings.Settings;  
import org.elasticsearch.plugins.Plugin;  
import org.elasticsearch.plugins.ScriptPlugin;  
import org.elasticsearch.script.NativeScriptFactory;

import com.chandan.es.nativescript.script.IsPrimeSearchScriptFactory;  
import com.chandan.es.nativescript.script.LookupScript;  
import com.chandan.es.nativescript.script.stockaggs.CombineScriptFactory;  
import com.chandan.es.nativescript.script.stockaggs.InitScriptFactory;  
import com.chandan.es.nativescript.script.stockaggs.MapScriptFactory;  
import com.chandan.es.nativescript.script.stockaggs.ReduceScriptFactory;

public class NativeScriptExamplesPlugin extends Plugin implements ScriptPlugin {  
private final Settings settings;

```
@Inject
public NativeScriptExamplesPlugin(Settings settings) {
	this.settings = settings;
}

@Override
public List<Setting<?>> getSettings() {
	return Collections.singletonList(PRIME_SCRIPT_DEFAULT_FIELD_NAME);
}

public Collection<Module> createGuiceModules() {
	return Collections.emptyList();
}

@Override
public List<NativeScriptFactory> getNativeScripts() {
	return Arrays.asList(new IsPrimeSearchScriptFactory(settings), new LookupScript.Factory(settings), new InitScriptFactory(), new MapScriptFactory(),
			new CombineScriptFactory(), new ReduceScriptFactory());
}

```

}

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [September 18, 2017, 7:43pm UTC](https://discuss.elastic.co/t/how-to-inject-node-object-in-elasticsearch-5-1-1-native-script-plugin/99393/4 "2017-09-18T19:43:08Z")

</div>

@Chandan_Kar You should create the client in the native script constructor. You will need to do this on your own, however, as scripts are no longer injected with guice. See [https://www.elastic.co/guide/en/elasticsearch/reference/master/modules-scripting-engine.html](https://www.elastic.co/guide/en/elasticsearch/reference/master/modules-scripting-engine.html)

---

<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:** [October 16, 2017, 7:43pm UTC](https://discuss.elastic.co/t/how-to-inject-node-object-in-elasticsearch-5-1-1-native-script-plugin/99393/5 "2017-10-16T19:43:37Z")

</div>

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