Skip to content

Commit

Permalink
use lucene for xor distance
Browse files Browse the repository at this point in the history
  • Loading branch information
benwtrent committed Jun 17, 2024
1 parent a928a99 commit d639369
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

package org.elasticsearch.script.field.vectors;

import org.apache.lucene.util.BitUtil;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.VectorUtil;
import org.elasticsearch.core.SuppressForbidden;
Expand Down Expand Up @@ -103,17 +102,7 @@ public double l1Norm(List<Number> queryVector) {

@Override
public int hamming(byte[] queryVector) {
int distance = 0, i = 0;
for (final int upperBound = queryVector.length & ~(Long.BYTES - 1); i < upperBound; i += Long.BYTES) {
distance += Long.bitCount(
(long) BitUtil.VH_NATIVE_LONG.get(queryVector, i) ^ (long) BitUtil.VH_NATIVE_LONG.get(vectorValue, i)
);
}
// tail:
for (; i < queryVector.length; i++) {
distance += Integer.bitCount((queryVector[i] ^ vectorValue[i]) & 0xFF);
}
return distance;
return VectorUtil.xorBitCount(queryVector, vectorValue);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

package org.elasticsearch.script.field.vectors;

import org.apache.lucene.util.BitUtil;
import org.apache.lucene.util.VectorUtil;
import org.elasticsearch.core.SuppressForbidden;

Expand Down Expand Up @@ -104,15 +103,7 @@ public double l1Norm(List<Number> queryVector) {

@Override
public int hamming(byte[] queryVector) {
int distance = 0, i = 0;
for (final int upperBound = queryVector.length & ~(Long.BYTES - 1); i < upperBound; i += Long.BYTES) {
distance += Long.bitCount((long) BitUtil.VH_NATIVE_LONG.get(queryVector, i) ^ (long) BitUtil.VH_NATIVE_LONG.get(docVector, i));
}
// tail:
for (; i < queryVector.length; i++) {
distance += Integer.bitCount((queryVector[i] ^ docVector[i]) & 0xFF);
}
return distance;
return VectorUtil.xorBitCount(queryVector, docVector);
}

@Override
Expand Down

0 comments on commit d639369

Please sign in to comment.