Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
fcheung committed May 13, 2012
0 parents commit cdcaa41
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Rakefile
@@ -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]
26 changes: 26 additions & 0 deletions org/spacevatican/elasticsearchexample/CustomScript.java
@@ -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);
}
}
16 changes: 16 additions & 0 deletions org/spacevatican/elasticsearchexample/CustomScriptFactory.java
@@ -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

@cuongvt101
Copy link

Choose a reason for hiding this comment

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

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.

@fcheung
Copy link
Owner Author

Choose a reason for hiding this comment

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

These days you need something like

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

@cuongvt101
Copy link

Choose a reason for hiding this comment

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

Thanks!

Please sign in to comment.