Skip to content
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ give it a try.


## Elasticsearch version
* Currently designed for Elasticsearch 5.6.0.
* Currently designed for Elasticsearch 6.0.0.
* for Elasticsearch 5.2.2 use branch `es-5.2.2`
* for Elasticsearch 2.4.4 use branch `es-2.4.4`

Expand Down Expand Up @@ -146,7 +146,7 @@ func convertBase64ToArray(base64Str string) ([]float64, error) {
"boost_mode": "replace",
"script_score": {
"script": {
"inline": "binary_vector_score",
"source": "binary_vector_score",
"lang": "knn",
"params": {
"cosine": false,
Expand Down
17 changes: 5 additions & 12 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<name>elasticsearch-binary-vector-scoring</name>
<groupId>com.liorkn.elasticsearch</groupId>
<artifactId>elasticsearch-binary-vector-scoring</artifactId>
<version>5.6.0</version>
<version>7.0.0</version>
<description>ElasticSearch Plugin for Binary Vector Scoring</description>

<licenses>
Expand All @@ -27,7 +27,7 @@
<elasticsearch.license.headerDefinition>${project.basedir}/src/main/resources/license-check/license_header_definition.xml</elasticsearch.license.headerDefinition>

<tests.ifNoTests>warn</tests.ifNoTests>
<elasticsearch.version>5.6.0</elasticsearch.version>
<elasticsearch.version>7.0.0</elasticsearch.version>
<commons-io.version>2.4</commons-io.version>
<httpcore.version>4.4.8</httpcore.version>
<junit.version>4.12</junit.version>
Expand All @@ -39,13 +39,13 @@
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.7</version>
<version>2.11.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.7</version>
<version>2.11.1</version>
<scope>test</scope>
</dependency>

Expand All @@ -65,7 +65,7 @@

<dependency>
<groupId>org.elasticsearch.plugin</groupId>
<artifactId>transport-netty3-client</artifactId>
<artifactId>transport-netty4-client</artifactId>
<version>${elasticsearch.version}</version>
<scope>test</scope>
</dependency>
Expand All @@ -86,12 +86,6 @@
</exclusion>
</exclusions>
</dependency>
<!--<dependency>-->
<!--<groupId>org.elasticsearch.plugin</groupId>-->
<!--<artifactId>transport-netty3-client</artifactId>-->
<!--<version>${elasticsearch.version}</version>-->
<!--<scope>test</scope>-->
<!--</dependency>-->
<dependency>
<groupId>org.codelibs.elasticsearch.module</groupId>
<artifactId>lang-painless</artifactId>
Expand All @@ -114,7 +108,6 @@
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>${elasticsearch.version}</version>
<!--<version>5.6.8</version>-->
<exclusions>
<exclusion>
<groupId>org.apache.httpcomponents</groupId>
Expand Down
2 changes: 1 addition & 1 deletion src/main/assemblies/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>true</includeBaseDirectory>
<includeBaseDirectory>false</includeBaseDirectory>
<baseDirectory>elasticsearch</baseDirectory>
<dependencySets>
<dependencySet>
Expand Down
35 changes: 17 additions & 18 deletions src/main/java/com/liorkn/elasticsearch/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,22 @@
*/
public class Util {

public static final double[] convertBase64ToArray(String base64Str) {
final byte[] decode = Base64.getDecoder().decode(base64Str.getBytes());
final DoubleBuffer doubleBuffer = ByteBuffer.wrap(decode).asDoubleBuffer();
public static double[] convertBase64ToArray(String base64Str) {
final byte[] decode = Base64.getDecoder().decode(base64Str.getBytes());
final DoubleBuffer doubleBuffer = ByteBuffer.wrap(decode).asDoubleBuffer();
final double[] dims = new double[doubleBuffer.capacity()];
doubleBuffer.get(dims);
return dims;
}

final double[] dims = new double[doubleBuffer.capacity()];
doubleBuffer.get(dims);
return dims;
}

public static final String convertArrayToBase64(double[] array) {
final int capacity = 8 * array.length;
final ByteBuffer bb = ByteBuffer.allocate(capacity);
for (int i = 0; i < array.length; i++) {
bb.putDouble(array[i]);
}
bb.rewind();
final ByteBuffer encodedBB = Base64.getEncoder().encode(bb);
return new String(encodedBB.array());
}
public static String convertArrayToBase64(double[] array) {
final int capacity = Double.BYTES * array.length;
final ByteBuffer bb = ByteBuffer.allocate(capacity);
for (double v : array) {
bb.putDouble((double) v);
}
bb.rewind();
final ByteBuffer encodedBB = Base64.getEncoder().encode(bb);
return new String(encodedBB.array());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.liorkn.elasticsearch.engine;

import com.liorkn.elasticsearch.script.VectorScoreScript;

import java.util.Map;

import org.elasticsearch.script.ScriptContext;
import org.elasticsearch.script.ScriptEngine;
import org.elasticsearch.script.ScoreScript;

/** This {@link ScriptEngine} uses Lucene segment details to implement document scoring based on their similarity with submitted document. */
public class VectorScoringScriptEngine implements ScriptEngine {

public static final String NAME = "knn";
private static final String SCRIPT_SOURCE = "binary_vector_score";

@Override
public String getType() {
return NAME;
}

@Override
public <T> T compile(String scriptName, String scriptSource, ScriptContext<T> context, Map<String, String> params) {
if (context.equals(ScoreScript.CONTEXT) == false) {
throw new IllegalArgumentException(getType() + " scripts cannot be used for context [" + context.name + "]");
}

// we use the script "source" as the script identifier
if (!SCRIPT_SOURCE.equals(scriptSource)) {
throw new IllegalArgumentException("Unknown script name " + scriptSource);
}

ScoreScript.Factory factory = VectorScoreScript.VectorScoreScriptFactory::new;
return context.factoryClazz.cast(factory);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,24 @@
*/
package com.liorkn.elasticsearch.plugin;

import com.liorkn.elasticsearch.service.VectorScoringScriptEngineService;
import com.liorkn.elasticsearch.engine.VectorScoringScriptEngine;

import java.util.Collection;

import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.plugins.ScriptPlugin;
import org.elasticsearch.script.ScriptEngineService;

import org.elasticsearch.script.ScriptContext;
import org.elasticsearch.script.ScriptEngine;
/**
* This class is instantiated when Elasticsearch loads the plugin for the
* first time. If you change the name of this plugin, make sure to update
* src/main/resources/es-plugin.properties file that points to this class.
*/
public final class VectorScoringPlugin extends Plugin implements ScriptPlugin {

public final ScriptEngineService getScriptEngineService(Settings settings) {
return new VectorScoringScriptEngineService(settings);
@Override
public ScriptEngine getScriptEngine(Settings settings, Collection<ScriptContext<?>> contexts) {
return new VectorScoringScriptEngine();
}


}
Loading