Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add clienv-java module with ElastiknnNearestNeighborsQueryBuilder for…
… running queries via Java REST client (#260)
- Loading branch information
1 parent
878e9ab
commit c28cc6b
Showing
10 changed files
with
445 additions
and
5 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
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
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
86 changes: 86 additions & 0 deletions
86
...lient-java/src/main/java/com/klibisz/elastiknn/ElastiknnNearestNeighborsQueryBuilder.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,86 @@ | ||
package com.klibisz.elastiknn; | ||
|
||
import com.klibisz.elastiknn.api4j.ElastiknnNearestNeighborsQuery; | ||
import com.klibisz.elastiknn.api4j.Vector; | ||
import org.apache.lucene.search.Query; | ||
import org.elasticsearch.common.io.stream.StreamOutput; | ||
import org.elasticsearch.common.xcontent.XContentBuilder; | ||
import org.elasticsearch.index.query.AbstractQueryBuilder; | ||
import org.elasticsearch.index.query.SearchExecutionContext; | ||
|
||
import java.io.IOException; | ||
import java.util.Objects; | ||
|
||
public class ElastiknnNearestNeighborsQueryBuilder extends AbstractQueryBuilder<ElastiknnNearestNeighborsQueryBuilder> { | ||
|
||
private final ElastiknnNearestNeighborsQuery query; | ||
private final String field; | ||
|
||
public ElastiknnNearestNeighborsQueryBuilder(ElastiknnNearestNeighborsQuery query, String field) { | ||
this.query = query; | ||
this.field = field; | ||
} | ||
|
||
@Override | ||
protected void doWriteTo(StreamOutput out) { | ||
throw new UnsupportedOperationException("doWriteTo is not implemented"); | ||
} | ||
|
||
@Override | ||
protected void doXContent(XContentBuilder builder, Params params) throws IOException { | ||
builder.startObject(getWriteableName()); | ||
builder.field("field", field); | ||
builder.field("similarity", query.getSimilarity().toString()); | ||
if (query instanceof ElastiknnNearestNeighborsQuery.Exact) { | ||
builder.field("model", "exact"); | ||
} else if (query instanceof ElastiknnNearestNeighborsQuery.AngularLsh) { | ||
ElastiknnNearestNeighborsQuery.AngularLsh q = (ElastiknnNearestNeighborsQuery.AngularLsh) query; | ||
builder.field("model", "lsh"); | ||
builder.field("candidates", q.getCandidates()); | ||
} else if (query instanceof ElastiknnNearestNeighborsQuery.L2Lsh) { | ||
ElastiknnNearestNeighborsQuery.L2Lsh q = (ElastiknnNearestNeighborsQuery.L2Lsh) query; | ||
builder.field("model", "lsh"); | ||
builder.field("candidates", q.getCandidates()); | ||
builder.field("probes", q.getProbes()); | ||
} else if (query instanceof ElastiknnNearestNeighborsQuery.PermutationLsh) { | ||
ElastiknnNearestNeighborsQuery.PermutationLsh q = (ElastiknnNearestNeighborsQuery.PermutationLsh) query; | ||
builder.field("model", "permutation_lsh"); | ||
builder.field("candidates", q.getCandidates()); | ||
} else { | ||
throw new RuntimeException(String.format("Unexpected query type [%s]", query.getClass().toString())); | ||
} | ||
if (query.getVector() instanceof Vector.DenseFloat) { | ||
Vector.DenseFloat dfv = (Vector.DenseFloat) query.getVector(); | ||
builder.field("vec", dfv.values); | ||
} else if (query.getVector() instanceof Vector.SparseBool) { | ||
Vector.SparseBool sbv = (Vector.SparseBool) query.getVector(); | ||
builder.startArray("vec"); | ||
builder.value(sbv.trueIndices); | ||
builder.value(sbv.totalIndices); | ||
builder.endArray(); | ||
} else { | ||
throw new RuntimeException(String.format("Unexpected vector type [%s]", query.getVector().getClass().toString())); | ||
} | ||
builder.endObject(); | ||
} | ||
|
||
@Override | ||
protected Query doToQuery(SearchExecutionContext context) { | ||
throw new UnsupportedOperationException("doToQuery is not implemented"); | ||
} | ||
|
||
@Override | ||
protected boolean doEquals(ElastiknnNearestNeighborsQueryBuilder other) { | ||
return other != null && ((this == other) || (query.equals(other.query) && field.equals(other.field))); | ||
} | ||
|
||
@Override | ||
protected int doHashCode() { | ||
return Objects.hash(query, field); | ||
} | ||
|
||
@Override | ||
public String getWriteableName() { | ||
return "elastiknn_nearest_neighbors"; | ||
} | ||
} |
159 changes: 159 additions & 0 deletions
159
...client-java/src/main/java/com/klibisz/elastiknn/api4j/ElastiknnNearestNeighborsQuery.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,159 @@ | ||
package com.klibisz.elastiknn.api4j; | ||
|
||
import java.util.Objects; | ||
|
||
public abstract class ElastiknnNearestNeighborsQuery { | ||
|
||
private ElastiknnNearestNeighborsQuery() {} | ||
|
||
public abstract Vector getVector(); | ||
public abstract Similarity getSimilarity(); | ||
|
||
public static final class Exact extends ElastiknnNearestNeighborsQuery { | ||
private final Similarity similarity; | ||
private final Vector vector; | ||
public Exact(Vector vector, Similarity similarity) { | ||
this.similarity = similarity; | ||
this.vector = vector; | ||
} | ||
|
||
@Override | ||
public Vector getVector() { | ||
return vector; | ||
} | ||
|
||
@Override | ||
public Similarity getSimilarity() { | ||
return similarity; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
Exact exact = (Exact) o; | ||
return getSimilarity() == exact.getSimilarity() && Objects.equals(getVector(), exact.getVector()); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(getSimilarity(), getVector()); | ||
} | ||
} | ||
|
||
public static final class AngularLsh extends ElastiknnNearestNeighborsQuery { | ||
private final Vector vector; | ||
private final Integer candidates; | ||
public AngularLsh(Vector vector, Integer candidates) { | ||
this.vector = vector; | ||
this.candidates = candidates; | ||
} | ||
|
||
public Integer getCandidates() { | ||
return candidates; | ||
} | ||
|
||
@Override | ||
public Vector getVector() { | ||
return vector; | ||
} | ||
|
||
@Override | ||
public Similarity getSimilarity() { | ||
return Similarity.ANGULAR; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
AngularLsh that = (AngularLsh) o; | ||
return Objects.equals(getVector(), that.getVector()) && Objects.equals(getCandidates(), that.getCandidates()) && getSimilarity() == that.getSimilarity(); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(getVector(), getCandidates(), getSimilarity()); | ||
} | ||
} | ||
|
||
public static final class L2Lsh extends ElastiknnNearestNeighborsQuery { | ||
private final Vector.DenseFloat vector; | ||
private final Integer candidates; | ||
private final Integer probes; | ||
public L2Lsh(Vector.DenseFloat vector, Integer candidates, Integer probes) { | ||
this.vector = vector; | ||
this.candidates = candidates; | ||
this.probes = probes; | ||
} | ||
|
||
public Integer getProbes() { | ||
return probes; | ||
} | ||
|
||
public Integer getCandidates() { | ||
return candidates; | ||
} | ||
|
||
@Override | ||
public Vector getVector() { | ||
return vector; | ||
} | ||
|
||
@Override | ||
public Similarity getSimilarity() { | ||
return Similarity.L2; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
L2Lsh l2Lsh = (L2Lsh) o; | ||
return Objects.equals(getVector(), l2Lsh.getVector()) && Objects.equals(getCandidates(), l2Lsh.getCandidates()) && Objects.equals(getProbes(), l2Lsh.getProbes()) && getSimilarity() == l2Lsh.getSimilarity(); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(getVector(), getCandidates(), getProbes(), getSimilarity()); | ||
} | ||
} | ||
|
||
public final static class PermutationLsh extends ElastiknnNearestNeighborsQuery { | ||
private final Vector.DenseFloat vector; | ||
private final Similarity similarity; | ||
private final Integer candidates; | ||
public PermutationLsh(Vector.DenseFloat vector, Similarity similarity, Integer candidates) { | ||
this.vector = vector; | ||
this.similarity = similarity; | ||
this.candidates = candidates; | ||
} | ||
|
||
public Integer getCandidates() { | ||
return candidates; | ||
} | ||
|
||
@Override | ||
public Vector getVector() { | ||
return vector; | ||
} | ||
|
||
@Override | ||
public Similarity getSimilarity() { | ||
return similarity; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
PermutationLsh that = (PermutationLsh) o; | ||
return Objects.equals(getVector(), that.getVector()) && getSimilarity() == that.getSimilarity() && Objects.equals(getCandidates(), that.getCandidates()); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(getVector(), getSimilarity(), getCandidates()); | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
elastiknn-client-java/src/main/java/com/klibisz/elastiknn/api4j/Similarity.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,9 @@ | ||
package com.klibisz.elastiknn.api4j; | ||
|
||
public enum Similarity { | ||
JACCARD, | ||
HAMMING, | ||
L1, | ||
L2, | ||
ANGULAR | ||
} |
Oops, something went wrong.