How to inject node object in elasticsearch 5.1.1 native script plugin

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!!!

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.

//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
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());
}

}

@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

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