Skip to content

Commit

Permalink
Integration test
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Farr <[email protected]>
  • Loading branch information
Xtansia committed Jul 3, 2023
1 parent 2f2cf7b commit 015fcd2
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ public final Builder parameters(@Nullable Map<String, JsonData> map) {
return this;
}

/**
* API name: {@code parameters}
*/
public final Builder parameters(String key, JsonData value) {
this.parameters = _mapPut(this.parameters, key, value);
return this;
}

/**
* Builds a {@link KnnVectorMethod}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ public final Builder method(@Nullable KnnVectorMethod value) {
return this;
}

/**
* API name: {@code method}
*/
public final Builder method(Function<KnnVectorMethod.Builder, ObjectBuilder<KnnVectorMethod>> fn) {
return this.method(fn.apply(new KnnVectorMethod.Builder()).build());
}

/**
* Builds a {@link KnnVectorProperty}.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package org.opensearch.client.opensearch.integTest;

import java.util.Arrays;
import java.util.stream.Collectors;
import org.junit.Test;
import org.opensearch.client.json.JsonData;
import org.opensearch.client.opensearch._types.Refresh;
import org.opensearch.client.opensearch.core.bulk.BulkOperation;

public abstract class AbstractKnnIT extends OpenSearchJavaClientTestCase {
@Test
public void testCanIndexAndSearchKnn() throws Exception {
final String indexName = "knn-index-and-search";

var createIndexResponse = javaClient()
.indices()
.create(c -> c
.index(indexName)
.settings(s -> s
.knn(true)
.knnAlgoParamEfSearch(100))
.mappings(m -> m
.properties("vector", p -> p
.knnVector(k -> k
.dimension(4)
.method(km -> km
.name("hnsw")
.spaceType("l2")
.engine("nmslib")
.parameters("ef_construction", JsonData.of(256))
.parameters("m", JsonData.of(16)))))));

assertEquals(true, createIndexResponse.acknowledged());

var docs = new Doc[] {
new Doc(new float[] { 1.5f, 5.5f, 4.5f, 6.4f }, 10.3f),
new Doc(new float[] { 2.5f, 3.5f, 5.6f, 6.7f }, 5.5f),
new Doc(new float[] { 4.5f, 5.5f, 6.7f, 3.7f }, 4.4f),
new Doc(new float[] { 1.5f, 5.5f, 4.5f, 6.4f }, 8.9f)
};
var bulkOps = Arrays.stream(docs)
.map(d -> BulkOperation.of(o -> o.index(i -> i.document(d))))
.collect(Collectors.toList());

var bulkResponse = javaClient()
.bulk(b -> b
.index(indexName)
.refresh(Refresh.WaitFor)
.operations(bulkOps));

assertFalse(bulkResponse.errors());

var searchResponse = javaClient()
.search(s -> s
.index(indexName)
.size(2)
.query(q -> q
.knn(k -> k
.field("vector")
.vector(new float[] { 2.0f, 3.0f, 5.0f, 6.0f })
.k(2))),
Doc.class);

var hits = searchResponse.hits().hits();

assertEquals(2, hits.size());
assertEquals(5.5f, hits.get(0).source().price, 0.01f);
assertEquals(4.4f, hits.get(1).source().price, 0.01f);
}

private static class Doc {
public float[] vector;
public float price;

public Doc(float[] vector, float price) {
this.vector = vector;
this.price = price;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.opensearch.client.opensearch.integTest.httpclient5;

import org.opensearch.client.opensearch.integTest.AbstractKnnIT;

public class KnnIT extends AbstractKnnIT implements HttpClient5TransportSupport {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.opensearch.client.opensearch.integTest.restclient;

import java.io.IOException;
import org.apache.hc.core5.http.HttpHost;
import org.opensearch.client.json.jackson.JacksonJsonpMapper;
import org.opensearch.client.opensearch.integTest.AbstractKnnIT;
import org.opensearch.client.transport.OpenSearchTransport;
import org.opensearch.client.transport.rest_client.RestClientTransport;
import org.opensearch.common.settings.Settings;

public class KnnIT extends AbstractKnnIT {
@Override
public OpenSearchTransport buildTransport(Settings settings, HttpHost[] hosts) throws IOException {
return new RestClientTransport(buildClient(settings, hosts), new JacksonJsonpMapper());
}
}

0 comments on commit 015fcd2

Please sign in to comment.