Skip to content

Commit 8c86b46

Browse files
authored
Merge pull request #2 from Zakkery/es-5
Fixed plugin-descriptor and changed code to work with ElasticSearch 5…
2 parents 3b3aaeb + d40e637 commit 8c86b46

File tree

7 files changed

+120
-99
lines changed

7 files changed

+120
-99
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ give it a try.
1010

1111

1212
## Elasticsearch version
13-
* Currently designed for elasticsearch 2.44. stay tuned for ES 5.5
13+
* Currently designed for elasticsearch 5.2.2. stay tuned for ES 5.5
1414

1515

1616
## Maven configuration
1717
* Clone the project
18-
* `mvn clean install` to compile the plugin jar file
18+
* `mvn package` to compile the plugin as a zip file
19+
* In Elasticsearch run `elasticsearch-plugin install file:/PATH_TO_ZIP` to install plugin
1920

2021

2122

@@ -41,7 +42,7 @@ give it a try.
4142
* The vector can be of any dimension
4243

4344
### Querying
44-
* For querying the 100 KNN docuemnts use this POST message on your ES index:
45+
* For querying the 100 KNN documents use this POST message on your ES index:
4546

4647
```{
4748
"query": {

pom.xml

100644100755
Lines changed: 47 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,21 @@
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
44
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
55
<modelVersion>4.0.0</modelVersion>
6-
<dependencies>
7-
<dependency>
8-
<groupId>org.apache.lucene</groupId>
9-
<artifactId>lucene-core</artifactId>
10-
<scope>provided</scope>
11-
</dependency>
12-
</dependencies>
13-
14-
<parent>
15-
<groupId>org.elasticsearch.plugin</groupId>
16-
<artifactId>plugins</artifactId>
17-
<version>2.4.4</version>
18-
</parent>
19-
6+
207
<name>elasticsearch-binary-vector-scoring</name>
218
<groupId>com.liorkn.elasticsearch</groupId>
229
<artifactId>elasticsearch-binary-vector-scoring</artifactId>
23-
<version>2.4.4</version>
10+
<version>5.2.2</version>
2411
<description>ElasticSearch Plugin for Binary Vector Scoring</description>
25-
12+
2613
<licenses>
27-
<license>
14+
<license>
2815
<name>The Apache Software License, Version 2.0</name>
2916
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
3017
<distribution>repo</distribution>
3118
</license>
3219
</licenses>
33-
34-
<!-- ============================================================= -->
35-
<!-- Most of the build and assembly logic is defined in the parent -->
36-
<!-- pom. We just need to override a few settings here -->
37-
<!-- ============================================================= -->
38-
20+
3921
<properties>
4022
<!-- define class name for the descriptor file -->
4123
<elasticsearch.plugin.classname>com.liorkn.elasticsearch.plugin.VectorScoringPlugin</elasticsearch.plugin.classname>
@@ -44,15 +26,26 @@
4426
<elasticsearch.license.header>${project.basedir}/src/main/resources/license-check/vector_scoring_license_header.txt</elasticsearch.license.header>
4527
<elasticsearch.license.headerDefinition>${project.basedir}/src/main/resources/license-check/license_header_definition.xml</elasticsearch.license.headerDefinition>
4628

47-
<!-- define where to find integration rest tests -->
4829
<tests.ifNoTests>warn</tests.ifNoTests>
49-
<!--
50-
<tests.rest.suite>vectorscoring</tests.rest.suite>
51-
<tests.rest.load_packaged>false</tests.rest.load_packaged>
52-
-->
30+
<elasticsearch.version>5.2.2</elasticsearch.version>
5331
</properties>
54-
55-
<!-- needed for the snapshots of elasticsearch -->
32+
33+
34+
<dependencies>
35+
<dependency>
36+
<groupId>org.elasticsearch</groupId>
37+
<artifactId>elasticsearch</artifactId>
38+
<version>${elasticsearch.version}</version>
39+
<scope>provided</scope>
40+
</dependency>
41+
<dependency>
42+
<groupId>com.google.collections</groupId>
43+
<artifactId>google-collections</artifactId>
44+
<version>1.0</version>
45+
</dependency>
46+
</dependencies>
47+
48+
<!-- needed for the snapshots of elasticsearch -->
5649
<repositories>
5750
<repository>
5851
<id>oss-snapshots</id>
@@ -62,32 +55,41 @@
6255
</repositories>
6356

6457
<build>
58+
<resources>
59+
<resource>
60+
<directory>src/main/resources</directory>
61+
<filtering>true</filtering>
62+
<includes>
63+
<include>plugin-descriptor.properties</include>
64+
</includes>
65+
</resource>
66+
</resources>
6567
<plugins>
6668
<plugin>
6769
<groupId>org.apache.maven.plugins</groupId>
68-
<artifactId>maven-install-plugin</artifactId>
69-
<version>2.5.2</version>
70-
</plugin>
71-
<plugin>
72-
<groupId>org.apache.maven.plugins</groupId>
73-
<artifactId>maven-deploy-plugin</artifactId>
70+
<artifactId>maven-compiler-plugin</artifactId>
7471
<configuration>
75-
<skip>true</skip>
72+
<source>1.8</source>
73+
<target>1.8</target>
7674
</configuration>
7775
</plugin>
78-
<plugin>
76+
<plugin>
7977
<groupId>org.apache.maven.plugins</groupId>
8078
<artifactId>maven-assembly-plugin</artifactId>
81-
</plugin>
82-
<plugin>
83-
<groupId>org.apache.maven.plugins</groupId>
84-
<artifactId>maven-compiler-plugin</artifactId>
8579
<configuration>
86-
<source>1.8</source>
87-
<target>1.8</target>
80+
<appendAssemblyId>false</appendAssemblyId>
81+
<outputDirectory>${project.build.directory}/releases/</outputDirectory>
82+
<descriptor>${basedir}/src/main/assemblies/plugin.xml</descriptor>
8883
</configuration>
84+
<executions>
85+
<execution>
86+
<phase>package</phase>
87+
<goals>
88+
<goal>single</goal>
89+
</goals>
90+
</execution>
91+
</executions>
8992
</plugin>
90-
9193
</plugins>
9294
</build>
9395
</project>

src/main/assemblies/plugin.xml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0"?>
2+
<assembly>
3+
<id>plugin</id>
4+
<formats>
5+
<format>zip</format>
6+
</formats>
7+
<includeBaseDirectory>true</includeBaseDirectory>
8+
<baseDirectory>elasticsearch</baseDirectory>
9+
<dependencySets>
10+
<dependencySet>
11+
<outputDirectory>/</outputDirectory>
12+
<useProjectArtifact>true</useProjectArtifact>
13+
<useTransitiveFiltering>true</useTransitiveFiltering>
14+
<excludes>
15+
<exclude>org.elasticsearch:elasticsearch</exclude>
16+
<exclude>org.apache.lucene:*</exclude>
17+
</excludes>
18+
</dependencySet>
19+
</dependencySets>
20+
<fileSets>
21+
<fileSet>
22+
<directory>src/main/resources</directory>
23+
<outputDirectory>/</outputDirectory>
24+
<filtered>true</filtered>
25+
<includes>
26+
<include>plugin-descriptor.properties</include>
27+
</includes>
28+
</fileSet>
29+
</fileSets>
30+
</assembly>

src/main/java/com/liorkn/elasticsearch/plugin/VectorScoringPlugin.java

100644100755
Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,41 +15,30 @@
1515

1616
import com.liorkn.elasticsearch.script.VectorScoreScript;
1717
import com.liorkn.elasticsearch.service.VectorScoringScriptEngineService;
18+
19+
import java.util.Collections;
20+
import java.util.List;
21+
22+
import org.elasticsearch.common.settings.Settings;
1823
import org.elasticsearch.plugins.Plugin;
19-
import org.elasticsearch.script.ScriptModule;
24+
import org.elasticsearch.plugins.ScriptPlugin;
25+
import org.elasticsearch.script.NativeScriptFactory;
26+
import org.elasticsearch.script.ScriptEngineService;
2027

2128
/**
2229
* This class is instantiated when Elasticsearch loads the plugin for the
2330
* first time. If you change the name of this plugin, make sure to update
2431
* src/main/resources/es-plugin.properties file that points to this class.
2532
*/
26-
public final class VectorScoringPlugin extends Plugin {
27-
28-
/**
29-
* The name of the plugin.
30-
* <p>
31-
* This name will be used by elasticsearch in the log file to refer to this plugin.
32-
*
33-
* @return plugin name.
34-
*/
35-
@Override
36-
public final String name() {
37-
return "binary-vector-scoring";
33+
public final class VectorScoringPlugin extends Plugin implements ScriptPlugin {
34+
35+
@Override
36+
public List<NativeScriptFactory> getNativeScripts() {
37+
return Collections.singletonList(new VectorScoreScript.Factory());
3838
}
39-
40-
/**
41-
* The description of the plugin.
42-
*
43-
* @return plugin description
44-
*/
45-
@Override
46-
public final String description() {
47-
return "Binary vector scoring";
48-
}
49-
50-
public final void onModule(ScriptModule module) {
51-
// Register vector scoring script.
52-
module.registerScript(VectorScoreScript.SCRIPT_NAME, VectorScoreScript.Factory.class);
53-
module.addScriptEngine(VectorScoringScriptEngineService.class);
39+
40+
@Override
41+
public ScriptEngineService getScriptEngineService(Settings settings) {
42+
return new VectorScoringScriptEngineService(settings);
5443
}
5544
}

src/main/java/com/liorkn/elasticsearch/script/VectorScoreScript.java

100644100755
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,14 @@ public ExecutableScript newScript(@Nullable Map<String, Object> params) throws S
110110
public boolean needsScores() {
111111
return false;
112112
}
113+
114+
@Override
115+
public String getName() {
116+
return SCRIPT_NAME;
117+
}
113118
}
114-
119+
120+
115121
/**
116122
* Init
117123
* @param params index that a scored are placed in this parameter. Initialize them here.
@@ -125,7 +131,7 @@ public VectorScoreScript(Map<String, Object> params) throws ScriptException {
125131

126132
final Object field = params.get("field");
127133
if (field == null)
128-
throw new ScriptException("binary_vector_score script requires field input");
134+
throw new IllegalArgumentException("binary_vector_score script requires field input");
129135
this.field = field.toString();
130136

131137
// get query inputVector - convert to primitive

src/main/java/com/liorkn/elasticsearch/service/VectorScoringScriptEngineService.java

100644100755
Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.liorkn.elasticsearch.service;
22

3-
import com.google.common.collect.ImmutableMap;
43
import com.liorkn.elasticsearch.script.VectorScoreScript;
54
import org.apache.lucene.index.LeafReaderContext;
65
import org.elasticsearch.common.Nullable;
@@ -25,37 +24,24 @@ public class VectorScoringScriptEngineService extends AbstractComponent implemen
2524

2625
public static final String NAME = "knn";
2726

28-
private final ImmutableMap<String, NativeScriptFactory> scripts;
29-
30-
3127
@Inject
32-
public VectorScoringScriptEngineService(Settings settings, Map<String, NativeScriptFactory> scripts) {
28+
public VectorScoringScriptEngineService(Settings settings) {
3329
super(settings);
34-
this.scripts = ImmutableMap.copyOf(scripts);
35-
}
36-
37-
@Override
38-
public String[] types() {
39-
return new String[]{NAME};
4030
}
4131

4232
@Override
43-
public String[] extensions() {
44-
return new String[]{NAME};
33+
public String getType() {
34+
return NAME;
4535
}
4636

4737
@Override
48-
public boolean sandboxed() {
49-
return false;
38+
public String getExtension() {
39+
return NAME;
5040
}
5141

5242
@Override
53-
public Object compile(String script, Map<String, String> params) {
54-
NativeScriptFactory scriptFactory = scripts.get(script);
55-
if (scriptFactory != null) {
56-
return scriptFactory;
57-
}
58-
throw new IllegalArgumentException("knn script [" + script + "] not found");
43+
public Object compile(String script, String source, Map<String, String> params) {
44+
return new VectorScoreScript.Factory();
5945
}
6046

6147
@Override
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
name=${project.name}
2+
description=${project.description}
3+
version=${project.version}
4+
jvm=true
5+
classname=${elasticsearch.plugin.classname}
6+
java.version=1.8
7+
elasticsearch.version=${elasticsearch.version}

0 commit comments

Comments
 (0)