Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

Added action for adding and deleting indexed scripts #213

Merged
merged 12 commits into from
Jun 29, 2015
Merged
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ We also would like to thank the following people for their significant contribut
* [Igor Kupczyński](https://github.com/puszczyk)
* [Kristoffer Renholm](https://github.com/renholm)
* [Mark Woon](https://github.com/markwoon)
* [Martin W. Kirst](https://github.com/nitram509)
* [Matthew Bogner](https://github.com/matthewbogner)
* [Min Cha](https://github.com/MinCha)
* [Neil Gentleman](https://github.com/nigelzor)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package io.searchbox.indices.script;

import com.google.common.io.CharStreams;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import io.searchbox.action.AbstractAction;
import io.searchbox.action.GenericResultAbstractAction;

import java.io.*;
import java.nio.charset.Charset;

import static io.searchbox.indices.script.ScriptLanguage.GROOVY;
import static java.net.URLEncoder.encode;

public class CreateIndexedScript extends GenericResultAbstractAction {

private final String scriptName;
private final ScriptLanguage scriptLanguage;

protected CreateIndexedScript(Builder builder) {
super(builder);
this.payload = builder.payload;
this.scriptName = builder.scriptName;
this.scriptLanguage = builder.scriptLanguage;
setURI(buildURI());
}

protected String buildURI() {
String finalUri = super.buildURI() + "/_scripts/" + scriptLanguage.pathParameterName + "/";
try {
finalUri += encode(scriptName, CHARSET);
} catch (UnsupportedEncodingException e) {
// unless CHARSET is overridden with a wrong value in a subclass,
// this exception won't be thrown.
log.error("Error occurred while adding parameters to uri.", e);
}
return finalUri;
}

@Override
public String getRestMethodName() {
return "POST";
}

public String getScriptName() {
return scriptName;
}

public ScriptLanguage getScriptLanguage() {
return scriptLanguage;
}

public static class Builder extends AbstractAction.Builder<CreateIndexedScript, Builder> {

private String scriptName;
private JsonElement payload;
private ScriptLanguage scriptLanguage = GROOVY;

public Builder(String scriptName) {
this.scriptName = scriptName;
}

@Override
public CreateIndexedScript build() {
return new CreateIndexedScript(this);
}

public Builder setSource(String source) {
createPayload(source);
return this;
}

public Builder loadSource(File srcFile) throws IOException {
return loadSource(srcFile, Charset.forName(AbstractAction.CHARSET));
}

public Builder loadSource(File srcFile, Charset encoding) throws IOException {
return loadSource(new FileInputStream(srcFile), encoding);
}

public Builder loadSource(InputStream srcStream) throws IOException {
return loadSource(srcStream, Charset.forName(AbstractAction.CHARSET));
}

public Builder loadSource(InputStream srcStream, Charset encoding) throws IOException {
String src = CharStreams.toString(new InputStreamReader(srcStream, encoding));
createPayload(src);
return this;
}

public Builder setLanguage(ScriptLanguage language) {
this.scriptLanguage = language;
return this;
}

private void createPayload(String source) {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("script", source);
payload = jsonObject;
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package io.searchbox.indices.script;

import io.searchbox.action.AbstractAction;
import io.searchbox.action.GenericResultAbstractAction;

import java.io.UnsupportedEncodingException;

import static io.searchbox.indices.script.ScriptLanguage.GROOVY;
import static java.net.URLEncoder.encode;

public class DeleteIndexedScript extends GenericResultAbstractAction {

private final String scriptName;
private final ScriptLanguage scriptLanguage;

protected DeleteIndexedScript(Builder builder) {
super(builder);
this.scriptName = builder.scriptName;
this.scriptLanguage = builder.scriptLanguage;
setURI(buildURI());
}

protected String buildURI() {
String finalUri = super.buildURI() + "/_scripts/" + scriptLanguage.pathParameterName + "/";
try {
finalUri += encode(scriptName, CHARSET);
} catch (UnsupportedEncodingException e) {
// unless CHARSET is overridden with a wrong value in a subclass,
// this exception won't be thrown.
log.error("Error occurred while adding parameters to uri.", e);
}
return finalUri;
}

@Override
public String getRestMethodName() {
return "DELETE";
}

public String getScriptName() {
return scriptName;
}

public ScriptLanguage getScriptLanguage() {
return scriptLanguage;
}

public static class Builder extends AbstractAction.Builder<DeleteIndexedScript, Builder> {

private String scriptName;
private ScriptLanguage scriptLanguage = GROOVY;

public Builder(String scriptName) {
this.scriptName = scriptName;
}

public Builder setLanguage(ScriptLanguage scriptLanguage) {
this.scriptLanguage = scriptLanguage;
return this;
}

@Override
public DeleteIndexedScript build() {
return new DeleteIndexedScript(this);
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.searchbox.indices.script;

/**
* As described in Elasticsearch Reference
* <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting.html">
* https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting.html
* </a>
*/
public enum ScriptLanguage {
GROOVY("groovy"),
EXPRESSION("expression"),
MUSTACHE("mustache"),
MVEL("mvel"),
JAVASCRIPT("javascript"),
PYTHON("python");

public final String pathParameterName;

ScriptLanguage(String pathParameterName) {
this.pathParameterName = pathParameterName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package io.searchbox.indices.script;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import static io.searchbox.indices.script.ScriptLanguage.GROOVY;
import static io.searchbox.indices.script.ScriptLanguage.JAVASCRIPT;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.*;

public class CreateIndexedScriptTest {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No integration test(s)? :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I overlooked that.
Good point - I will add one - thanks.


private static final String A_NAME = "a_name";
private CreateIndexedScript script;
private CreateIndexedScript.Builder builder;
private String groovysnippet;

@Before
public void setUp() throws Exception {
builder = new CreateIndexedScript.Builder(A_NAME).setLanguage(JAVASCRIPT);
script = builder.build();
groovysnippet = "def test_a=123\n" +
"def test_b=\"$test_a\"\n";
}

@Test
public void defaultScriptingLanguageIsGroovy() throws Exception {
CreateIndexedScript script = new CreateIndexedScript.Builder(A_NAME).build();

assertEquals(GROOVY, script.getScriptLanguage());
assertThat(script.buildURI(), containsString(GROOVY.pathParameterName));
}

@Test
public void scriptingLanguageIsSetIntoPath() throws Exception {

assertThat(script.buildURI(), containsString("/_scripts/" + JAVASCRIPT.pathParameterName + "/"));
}

@Test
public void nameOfTheScriptIsSetIntoPath() throws Exception {

assertThat(script.buildURI(), containsString("/_scripts/" + JAVASCRIPT.pathParameterName + "/" + A_NAME));
}

@Test
public void scriptSourceIsValidJsonString() throws Exception {
builder.setSource(groovysnippet);

script = builder.build();

JsonObject jsonPayload = parseAsGson(script.getData(new Gson()));
assertNotNull(jsonPayload.get("script"));
assertEquals(groovysnippet, jsonPayload.get("script").getAsString());
}

@Test
public void fileSourceIsValidJsonString() throws Exception {
builder.loadSource(createTempGroovySnippetFile());

script = builder.build();

JsonObject jsonPayload = parseAsGson(script.getData(new Gson()));
assertNotNull(jsonPayload.get("script"));
assertEquals(groovysnippet, jsonPayload.get("script").getAsString());
}

private File createTempGroovySnippetFile() throws IOException {
File file = File.createTempFile("test", ".groovy");
file.deleteOnExit();
FileWriter writer = new FileWriter(file);
writer.write(groovysnippet);
writer.close();
return file;
}

private JsonObject parseAsGson(String data) {
return new JsonParser().parse(data).getAsJsonObject();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package io.searchbox.indices.script;

import org.junit.Before;
import org.junit.Test;

import static io.searchbox.indices.script.ScriptLanguage.GROOVY;
import static io.searchbox.indices.script.ScriptLanguage.JAVASCRIPT;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;

public class DeleteIndexedScriptTest {

private static final String A_NAME = "a_name";
private DeleteIndexedScript script;

@Before
public void setUp() throws Exception {
DeleteIndexedScript.Builder builder = new DeleteIndexedScript.Builder(A_NAME).setLanguage(JAVASCRIPT);
script = builder.build();
}

@Test
public void defaultScriptingLanguageIsGroovy() throws Exception {
DeleteIndexedScript script = new DeleteIndexedScript.Builder(A_NAME).build();

assertEquals(GROOVY, script.getScriptLanguage());
assertThat(script.buildURI(), containsString(GROOVY.pathParameterName));
}

@Test
public void scriptingLanguageIsSetIntoPath() throws Exception {

assertThat(script.buildURI(), containsString("/_scripts/" + JAVASCRIPT.pathParameterName + "/"));
}

@Test
public void nameOfTheScriptIsSetIntoPath() throws Exception {

assertThat(script.buildURI(), containsString("/_scripts/" + JAVASCRIPT.pathParameterName + "/" + A_NAME));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.searchbox.indices.script;

import io.searchbox.client.JestResult;
import io.searchbox.common.AbstractIntegrationTest;
import org.elasticsearch.test.ElasticsearchIntegrationTest;
import org.junit.Test;

import java.io.IOException;

import static io.searchbox.indices.script.ScriptLanguage.GROOVY;

@ElasticsearchIntegrationTest.ClusterScope(scope = ElasticsearchIntegrationTest.Scope.TEST, numDataNodes = 1)
public class CreateIndexedScriptIntegrationTest extends AbstractIntegrationTest {

@Test
public void create_an_indexed_script_for_Groovy() throws IOException {
CreateIndexedScript createIndexedScript = new CreateIndexedScript.Builder("script-test")
.setLanguage(GROOVY)
.setSource("def aVariable = 1\n" +
"return aVariable")
.build();
JestResult result = client.execute(createIndexedScript);
assertTrue(result.isSucceeded());
}

}

Loading