This repository has been archived by the owner on Jun 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 727
Added action for adding and deleting indexed scripts #213
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
f59cdab
created Action for posting indexed scripts to Elasticsearch
nitram509 3ccb6cd
created Action for deleting indexed scripts from ES
nitram509 04932d3
reworked tests o use JUnit instead of fest assertions, because its mo…
nitram509 8962c1d
Merge remote-tracking branch 'searchbox-io/master' into script-update…
nitram509 af5d943
made action constructor protected, as Kramer in f4c5abcc421d141452b3f…
nitram509 56cf150
reworked to use CharStreams.toString() utility to do more code re-use
nitram509 0569d0a
added myself to README.md
nitram509 e4bb419
to be more consistent, log the Exception and move on
nitram509 c249a47
fixed ident to 4 spaces, cause all other sources also do
nitram509 4bf1e04
added integration tests
nitram509 32f770f
removed fest-assertion deps
nitram509 a8ca714
alphabetical ordering
nitram509 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
jest-common/src/main/java/io/searchbox/indices/script/CreateIndexedScript.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
jest-common/src/main/java/io/searchbox/indices/script/DeleteIndexedScript.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
jest-common/src/main/java/io/searchbox/indices/script/ScriptLanguage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
jest-common/src/test/java/io/searchbox/indices/script/CreateIndexedScriptTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
|
||
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(); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
jest-common/src/test/java/io/searchbox/indices/script/DeleteIndexedScriptTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
jest/src/test/java/io/searchbox/indices/script/CreateIndexedScriptIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No integration test(s)? :)
There was a problem hiding this comment.
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.