Skip to content

Commit e48dcdb

Browse files
committed
Updated to work with elasticsearch v2.x
2 parents ef44d77 + 59b8ea2 commit e48dcdb

File tree

14 files changed

+69
-38
lines changed

14 files changed

+69
-38
lines changed

README.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
# Introduction
66
This repository contains native scoring scripts for use with elasticsearch.
77

8-
**Disclaimer:** They have only been tested with elasticsearch v1.7.3, and even then against a very strict document set.
8+
**Disclaimer:** They have only been tested with elasticsearch v2.3.1, and even then against a very strict document set.
99

1010
## Hamming Distance
1111
This script will calculate the hamming distance between two hex-encoded bit-strings (ie: strings made up of 1’s and 0’s and then hexidecimally encoded), one hash being passed in as a parameter: `param_hash`, and the other being stored in a field also identified by a parameter: `param_field`.
1212

1313
It will then return your search results scored accordingly, where the smallest distance (ie: most similar strings) appear nearer the top of the results list.
1414

15+
**Note:** If the parameter hash and the document hash are not the same length, the result will be a score of `0.0f`.
16+
1517
### Caveats
1618
The hashes must be the same length in every document, or the relative scoring won’t be accurate.
1719

@@ -32,8 +34,8 @@ The first hash is sent in as a parameter: `param_hash`, the other is expected to
3234
Include the following in your elasticsearch.yml config file:
3335

3436
script.native:
35-
hamming_distance.type: com.example.elasticsearch.plugins.HammingDistanceScriptFactory
36-
euclidean_distance.type: com.example.elasticsearch.plugins.EuclideanDistanceScriptFactory
37+
hamming_distance.type: com.cameraforensics.elasticsearch.plugins.HammingDistanceScriptFactory
38+
euclidean_distance.type: com.cameraforensics.elasticsearch.plugins.EuclideanDistanceScriptFactory
3739

3840
**Note:** If you don’t do this, they still show up on the plugins list (see later) but you’ll get errors when you try to use either of them saying that elasticsearch can’t find the plugin.
3941

@@ -44,8 +46,8 @@ The results will be located in: `PLUGIN_NAME/build/libs/`
4446

4547
For example:
4648

47-
* `hammingdistance/build/libs/hammingdistance-0.1.0.jar`
48-
* `euclideandistance/build/libs/euclideandistance-0.1.0.jar`
49+
* `hammingdistance/build/libs/hammingdistance-2.3.1.0.jar`
50+
* `euclideandistance/build/libs/euclideandistance-2.3.1.0.jar`
4951

5052
**Note:** This has been built using Gradle v2.7.
5153

@@ -54,6 +56,8 @@ Don't bother using the elasticsearch plugin script to install it - it's just a p
5456

5557
Instead put the .jar file in `%ELASTICSEARCH_HOME%/plugins/hamming_distance` for hamming distance, and `%ELASTICSEARCH_HOME%/plugins/euclidean_distance` for euclidean distance, then restart elasticsearch.
5658

59+
**Note:** If you installed elasticsearch according to docs, this `%ELASTICSEARCH_HOME%` will default to `/usr/share/elasticsearch/`
60+
5761
If all has gone well, you'll see them being loaded on elasticsearch startup:
5862

5963
[1982-07-06 12:02:43,765][INFO ][plugins ] [Junta] loaded [marvel, hamming_distance, euclidean_distance], sites [marvel]
@@ -64,9 +68,9 @@ AND when you call the list of plugins they’ll be there:
6468

6569
produces something like:
6670

67-
name component version type url
68-
Junta hamming_distance 0.1.0 j
69-
Junta euclidean_distance 0.1.0 j
71+
name component version type url
72+
Junta hamming_distance 2.3.1.0 j
73+
Junta euclidean_distance 2.3.1.0 j
7074

7175

7276
# Usage Examples

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
group 'com.cameraforensics'
2-
version '0.1.0'
2+
version '2.3.1.0'
33

44
apply plugin: 'java'
55
apply plugin: 'java'

euclideandistance/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
group 'com.cameraforensics'
2-
version '0.1.0'
2+
version '2.3.1.0'
33

44
sourceCompatibility = JavaVersion.VERSION_1_7
55
targetCompatibility = JavaVersion.VERSION_1_7
66

77
jar {
88
baseName = 'euclideandistance'
9-
version = '0.1.0'
9+
version = '2.3.1.0'
1010
}
1111

1212
dependencies {
13-
compile 'org.elasticsearch:elasticsearch:1.7.3'
13+
compile 'org.elasticsearch:elasticsearch:2.3.1'
1414
}
Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
package com.cameraforensics.elasticsearch.plugins;
22

3-
import org.elasticsearch.plugins.AbstractPlugin;
3+
import org.elasticsearch.plugins.Plugin;
4+
import org.elasticsearch.script.ScriptModule;
5+
6+
public class EuclideanDistancePlugin extends Plugin {
7+
8+
private static final String PLUGIN_NAME = "euclidean_distance";
49

5-
public class EuclideanDistancePlugin extends AbstractPlugin {
610
@Override
711
public String name() {
8-
return "euclidean_distance";
12+
return PLUGIN_NAME;
913
}
1014

1115
@Override
1216
public String description() {
1317
return "A scoring function to calculate the euclidean distance between two hex encoded strings.";
1418
}
19+
20+
public void onModule(ScriptModule scriptModule) {
21+
scriptModule.registerScript(PLUGIN_NAME, EuclideanDistanceScriptFactory.class);
22+
}
23+
1524
}

euclideandistance/src/main/java/com/cameraforensics/elasticsearch/plugins/EuclideanDistanceScriptFactory.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,9 @@ public class EuclideanDistanceScriptFactory implements NativeScriptFactory {
1111
public ExecutableScript newScript(@Nullable Map<String, Object> params) {
1212
return new EuclideanDistanceScript(params);
1313
}
14+
15+
@Override
16+
public boolean needsScores() {
17+
return false;
18+
}
1419
}

euclideandistance/src/main/resources/es-plugin.properties

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
name=euclidean_distance
2+
description=A scoring function to calculate the euclidean distance between two hex encoded strings.
3+
version=2.3.1.0
4+
jvm=true
5+
classname=com.cameraforensics.elasticsearch.plugins.EuclideanDistancePlugin
6+
java.version=1.7
7+
elasticsearch.version=2.3.1

gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#Fri Sep 16 10:09:19 BST 2016
1+
#Fri Sep 16 10:12:38 BST 2016
22
distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME

hammingdistance/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
group 'com.cameraforensics'
2-
version '0.1.0'
2+
version '2.3.1.0'
33

44
sourceCompatibility = JavaVersion.VERSION_1_7
55
targetCompatibility = JavaVersion.VERSION_1_7
66

77
jar {
88
baseName = 'hammingdistance'
9-
version = '0.1.0'
9+
version = '2.3.1.0'
1010
}
1111

1212
dependencies {
13-
compile 'org.elasticsearch:elasticsearch:1.7.3'
13+
compile 'org.elasticsearch:elasticsearch:2.3.1'
1414
}
Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
package com.cameraforensics.elasticsearch.plugins;
22

3-
import org.elasticsearch.plugins.AbstractPlugin;
3+
import org.elasticsearch.plugins.Plugin;
4+
import org.elasticsearch.script.ScriptModule;
5+
6+
public class HammingDistancePlugin extends Plugin {
7+
8+
private final String PLUGIN_NAME = "hamming_distance";
49

5-
public class HammingDistancePlugin extends AbstractPlugin {
610
@Override
711
public String name() {
8-
return "hamming_distance";
12+
return PLUGIN_NAME;
913
}
1014

1115
@Override
1216
public String description() {
1317
return "A scoring function to calculate hamming distance between two hex encoded strings.";
1418
}
19+
20+
public void onModule(ScriptModule scriptModule) {
21+
scriptModule.registerScript(PLUGIN_NAME, HammingDistanceScriptFactory.class);
22+
}
1523
}

0 commit comments

Comments
 (0)