Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit cdcaa41292edf71342ec1979d26fabe9f01c65e7 @fcheung committed May 13, 2012
@@ -0,0 +1,16 @@
+require 'fileutils'
+task :compile do
+ source = FileList['**/*.java']
+ sh "javac #{source.collect {|s| "'#{s}'" }.join(' ')} -classpath ../lib/elasticsearch-*.jar"
+end
+
+task :clean do
+ FileUtils::Verbose.rm Dir.glob("**/*.class")
+end
+
+task :package do
+ objects = FileList['**/*.class']
+ sh "jar cf MyNativeScript.jar #{objects.collect {|s| "'#{s}'" }.join(' ')}"
+end
+
+task :build => [:clean, :compile, :package]
@@ -0,0 +1,26 @@
+package org.spacevatican.elasticsearchexample;
+
+import org.elasticsearch.common.Nullable;
+import org.elasticsearch.script.ExecutableScript;
+import org.elasticsearch.script.NativeScriptFactory;
+import org.elasticsearch.script.AbstractDoubleSearchScript;
+import java.util.Map;
+import java.lang.Math;
+
+public class CustomScript extends AbstractDoubleSearchScript {
+
+ double base;
+ double exp;
+
+ public CustomScript(@Nullable Map<String,Object> params){
+ base = ((Double)params.get("base")).doubleValue();
+ exp = ((Double)params.get("exp")).doubleValue();
+ }
+
+ @Override
+ public double runAsDouble() {
+ double a = doc().numeric("a").getDoubleValue();
+
+ return a / Math.pow(base, exp);
+ }
+}
@@ -0,0 +1,16 @@
+package org.spacevatican.elasticsearchexample;
+
+import org.elasticsearch.common.Nullable;
+import org.elasticsearch.script.ExecutableScript;
+import org.elasticsearch.script.NativeScriptFactory;
+
+import org.spacevatican.elasticsearchexample.CustomScript;
+import java.util.Map;
+
+public class CustomScriptFactory implements NativeScriptFactory {
+
+ @Override public ExecutableScript newScript(@Nullable Map<String,Object> params){
+ return new CustomScript(params);
+ }
+
+}

3 comments on commit cdcaa41

Hello Frederick,

I couldn't find your contact elsewhere so I write it here, hope you don't mind. I'm trying to follow your example but I got some errors. First, rake build complains about double a = doc().numeric("a").getDoubleValue(); so I edited to double a = (Double) doc().get("a"); and was able to compile. However when I do query on Elasticsearch, it just show error. I guess the Elasticsearch version you used when you wrote this was some earlier version. I'm using 1.7 currently. I would really appreciate it if you could help to make this work. Thanks a lot!

PS: You can write me at cuongvt101(at)gmail if you prefer to communicate via email.

Owner

fcheung replied Dec 31, 2015

These days you need something like

  ScriptDocValues.Doubles docval=(ScriptDocValues.Doubles)doc().get("foo");
  double foo = docval.getValue()

Thanks!

Please sign in to comment.