Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit cdcaa41
Showing
3 changed files
with
58 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] |
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,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
16
org/spacevatican/elasticsearchexample/CustomScriptFactory.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,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); | ||
} | ||
|
||
} |
cdcaa41
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.
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 todouble 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.
cdcaa41
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.
These days you need something like
cdcaa41
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.
Thanks!