未验证 提交 b0b6aefc 编写于 作者: D del-zhenwu 提交者: GitHub

[skip ci] Java sdk test: add testIndex.java (#3409)

* [skip ci] add constants.java
Signed-off-by: Nzw <zw@milvus.io>

* [skip ci] add testIndex.java
Signed-off-by: Nzw <zw@milvus.io>

* [skip ci] add TestDeleteEntities.java
Signed-off-by: Nzw <zw@milvus.io>
Co-authored-by: Nzw <zw@milvus.io>
上级 4460757f
package com;
import io.milvus.client.*;
import org.testng.Assert;
import org.testng.annotations.Test;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.LongStream;
public class TestDeleteEntities {
@Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
public void testDeleteVectors(MilvusClient client, String collectionName) {
InsertParam insertParam = new InsertParam.Builder(collectionName).withFields(Constants.defaultEntities).build();
InsertResponse res = client.insert(insertParam);
assert(res.getResponse().ok());
List<Long> ids = res.getEntityIds();
client.flush(collectionName);
Response res_delete = client.deleteEntityByID(collectionName, ids);
assert(res_delete.ok());
client.flush(collectionName);
// Assert collection row count
Assert.assertEquals(client.countEntities(collectionName).getCollectionEntityCount(), 0);
}
@Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
public void testDeleteSingleVector(MilvusClient client, String collectionName) {
List<List<Float>> del_vector = new ArrayList<>();
del_vector.add(Constants.vectors.get(0));
List<Long> del_ids = new ArrayList<>();
InsertParam insertParam = new InsertParam.Builder(collectionName).withFields(Constants.defaultEntities).build();
InsertResponse res = client.insert(insertParam);
assert(res.getResponse().ok());
List<Long> ids = res.getEntityIds();
del_ids.add(ids.get(0));
client.flush(collectionName);
Response res_delete = client.deleteEntityByID(collectionName, Collections.singletonList(ids.get(0)));
assert(res_delete.ok());
client.flush(collectionName);
// Assert collection row count
Assert.assertEquals(client.countEntities(collectionName).getCollectionEntityCount(), Constants.nb - 1);
// Assert getEntityByID
GetEntityByIDResponse res_get = client.getEntityByID(collectionName, del_ids);
assert(res_get.getResponse().ok());
Assert.assertEquals(res_get.getValidIds().size(), 0);
}
@Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
public void testDeleteVectorsCollectionNotExisted(MilvusClient client, String collectionName) {
InsertParam insertParam = new InsertParam.Builder(collectionName).withFields(Constants.defaultEntities).build();
InsertResponse res = client.insert(insertParam);
assert(res.getResponse().ok());
client.flush(collectionName);
List<Long> ids = res.getEntityIds();
String collectionNameNew = Utils.genUniqueStr(collectionName);
Response res_delete = client.deleteEntityByID(collectionNameNew, ids);
assert(!res_delete.ok());
}
@Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
public void testDeleteVectorsEmptyCollection(MilvusClient client, String collectionName) {
String collectionNameNew = Utils.genUniqueStr(collectionName);
List<Long> entityIds = LongStream.range(0, Constants.nb).boxed().collect(Collectors.toList());
Response res_delete = client.deleteEntityByID(collectionNameNew, entityIds);
assert(!res_delete.ok());
}
@Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
public void test_delete_vector_id_not_existed(MilvusClient client, String collectionName) {
InsertParam insertParam = new InsertParam.Builder(collectionName).withFields(Constants.defaultEntities).build();
InsertResponse res = client.insert(insertParam);
assert(res.getResponse().ok());
List<Long> ids = new ArrayList<Long>();
ids.add((long)123456);
ids.add((long)1234561);
client.flush(collectionName);
Response res_delete = client.deleteEntityByID(collectionName, ids);
assert(res_delete.ok());
client.flush(collectionName);
// Assert collection row count
Assert.assertEquals(client.countEntities(collectionName).getCollectionEntityCount(), Constants.nb);
}
// Below tests binary vectors
@Test(dataProvider = "BinaryCollection", dataProviderClass = MainClass.class)
public void test_delete_vectors_binary(MilvusClient client, String collectionName) {
InsertParam insertParam = new InsertParam.Builder(collectionName).withFields(Constants.defaultBinaryEntities).build();
InsertResponse res = client.insert(insertParam);
assert(res.getResponse().ok());
List<Long> ids = res.getEntityIds();
client.flush(collectionName);
Response res_delete = client.deleteEntityByID(collectionName, ids);
assert(res_delete.ok());
client.flush(collectionName);
// Assert collection row count
Assert.assertEquals(client.countEntities(collectionName).getCollectionEntityCount(), 0);
}
}
//package com;
//
//import io.milvus.client.*;
//import org.testng.Assert;
//import org.testng.annotations.Test;
//
//import java.nio.ByteBuffer;
//import java.util.ArrayList;
//import java.util.Collections;
//import java.util.List;
//
//public class TestDeleteVectors {
// int dimension = 128;
// int nb = 8000;
//
// List<List<Float>> vectors = Utils.genVectors(nb, dimension, true);
// List<ByteBuffer> vectorsBinary = Utils.genBinaryVectors(nb, dimension);
//
// @Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
// public void test_delete_vectors(MilvusClient client, String collectionName) {
// // Add vectors
// InsertParam insertParam = new InsertParam.Builder(collectionName).withFloatVectors(vectors).build();
// InsertResponse res = client.insert(insertParam);
// assert(res.getResponse().ok());
// List<Long> ids = res.getVectorIds();
// client.flush(collectionName);
// Response res_delete = client.deleteEntityByID(collectionName, ids);
// assert(res_delete.ok());
// client.flush(collectionName);
// // Assert collection row count
// Assert.assertEquals(client.countEntities(collectionName).getCollectionEntityCount(), 0);
// }
//
// @Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
// public void test_delete_single_vector(MilvusClient client, String collectionName) {
// List<List<Float>> del_vector = new ArrayList<>();
// del_vector.add(vectors.get(0));
// List<Long> del_ids = new ArrayList<>();
// InsertParam insertParam = new InsertParam.Builder(collectionName).withFloatVectors(vectors).build();
// InsertResponse res = client.insert(insertParam);
// assert(res.getResponse().ok());
// List<Long> ids = res.getVectorIds();
// del_ids.add(ids.get(0));
// client.flush(collectionName);
// Response res_delete = client.deleteEntityByID(collectionName, Collections.singletonList(ids.get(0)));
// assert(res_delete.ok());
// client.flush(collectionName);
// // Assert collection row count
// Assert.assertEquals(client.countEntities(collectionName).getCollectionEntityCount(), nb - 1);
// GetEntityByIDResponse res_get = client.getEntityByID(collectionName, del_ids);
// assert(res_get.getResponse().ok());
// assert(res_get.getFloatVectors().get(0).size() == 0);
// }
//
// @Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
// public void test_delete_vectors_collection_not_existed(MilvusClient client, String collectionName) {
// InsertParam insertParam = new InsertParam.Builder(collectionName).withFloatVectors(vectors).build();
// InsertResponse res = client.insert(insertParam);
// assert(res.getResponse().ok());
// client.flush(collectionName);
// List<Long> ids = res.getVectorIds();
// Response res_delete = client.deleteEntityByID(collectionName + "_not_existed", ids);
// assert(!res_delete.ok());
// }
//
// @Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
// public void test_delete_vector_id_not_existed(MilvusClient client, String collectionName) {
// InsertParam insertParam = new InsertParam.Builder(collectionName).withFloatVectors(vectors).build();
// InsertResponse res = client.insert(insertParam);
// assert(res.getResponse().ok());
// List<Long> ids = new ArrayList<Long>();
// ids.add((long)123456);
// ids.add((long)1234561);
// client.flush(collectionName);
// Response res_delete = client.deleteEntityByID(collectionName, ids);
// assert(res_delete.ok());
// client.flush(collectionName);
// // Assert collection row count
// Assert.assertEquals(client.countEntities(collectionName).getCollectionEntityCount(), nb);
// }
//
//
// // Below tests binary vectors
// @Test(dataProvider = "BinaryCollection", dataProviderClass = MainClass.class)
// public void test_delete_vectors_binary(MilvusClient client, String collectionName) {
// // Add vectors
// InsertParam insertParam = new InsertParam.Builder(collectionName).withBinaryVectors(vectorsBinary).build();
// InsertResponse res = client.insert(insertParam);
// assert(res.getResponse().ok());
// List<Long> ids = res.getVectorIds();
// client.flush(collectionName);
// Response res_delete = client.deleteEntityByID(collectionName, ids);
// assert(res_delete.ok());
// client.flush(collectionName);
// // Assert collection row count
// Assert.assertEquals(client.countEntities(collectionName).getCollectionEntityCount(), 0);
// }
//
// @Test(dataProvider = "BinaryCollection", dataProviderClass = MainClass.class)
// public void test_delete_single_vector_binary(MilvusClient client, String collectionName) {
// List<ByteBuffer> del_vector = new ArrayList<>();
// del_vector.add(vectorsBinary.get(0));
// InsertParam insertParam = new InsertParam.Builder(collectionName).withBinaryVectors(vectorsBinary).build();
// InsertResponse res = client.insert(insertParam);
// assert(res.getResponse().ok());
// List<Long> ids = res.getVectorIds();
// client.flush(collectionName);
// Response res_delete = client.deleteEntityByID(collectionName, Collections.singletonList(ids.get(0)));
// assert(res_delete.ok());
// client.flush(collectionName);
// // Assert collection row count
// Assert.assertEquals(client.countEntities(collectionName).getCollectionEntityCount(), nb - 1);
// // Cannot search for the vector
// SearchParam searchParam = new SearchParam.Builder(collectionName)
// .withBinaryVectors(del_vector)
// .withTopK(1)
// .withParamsInJson("{\"nprobe\": 20}")
// .build();
// SearchResponse res_search = client.search(searchParam);
// assert(res_search.getResultIdsList().size() == 1);
// }
//
// @Test(dataProvider = "BinaryCollection", dataProviderClass = MainClass.class)
// public void test_delete_vectors_collection_not_existed_binary(MilvusClient client, String collectionName) {
// InsertParam insertParam = new InsertParam.Builder(collectionName).withBinaryVectors(vectorsBinary).build();
// InsertResponse res = client.insert(insertParam);
// assert(res.getResponse().ok());
// List<Long> ids = res.getVectorIds();
// client.flush(collectionName);
// Response res_delete = client.deleteEntityByID(collectionName + "_not_existed", ids);
// assert(!res_delete.ok());
// }
//
// @Test(dataProvider = "BinaryCollection", dataProviderClass = MainClass.class)
// public void test_delete_vector_id_not_existed_binary(MilvusClient client, String collectionName) {
// InsertParam insertParam = new InsertParam.Builder(collectionName).withBinaryVectors(vectorsBinary).build();
// InsertResponse res = client.insert(insertParam);
// assert(res.getResponse().ok());
// List<Long> ids = new ArrayList<Long>();
// ids.add((long)123456);
// ids.add((long)1234561);
// client.flush(collectionName);
// Response res_delete = client.deleteEntityByID(collectionName, ids);
// assert(res_delete.ok());
// client.flush(collectionName);
// // Assert collection row count
// Assert.assertEquals(client.countEntities(collectionName).getCollectionEntityCount(), nb);
// }
//
//}
......@@ -16,11 +16,13 @@ public class Utils {
w2v = w2v.stream().map(x -> x / norm).collect(Collectors.toList());
return w2v;
}
public static String genUniqueStr(String str_value){
String prefix = "_"+RandomStringUtils.randomAlphabetic(10);
String str = str_value == null || str_value.trim().isEmpty() ? "test" : str_value;
return str.trim()+prefix;
}
public static List<List<Float>> genVectors(int vectorCount, int dimension, boolean norm) {
List<List<Float>> vectors = new ArrayList<>();
Random random = new Random();
......@@ -48,6 +50,7 @@ public class Utils {
}
return vectors;
}
private static List<Map<String, Object>> genBaseFieldsWithoutVector(){
List<Map<String,Object>> fieldsList = new ArrayList<>();
Map<String, Object> intFields = new HashMap<>();
......@@ -61,6 +64,7 @@ public class Utils {
return fieldsList;
}
public static List<Map<String, Object>> genDefaultFields(int dimension, boolean isBinary){
List<Map<String, Object>> defaultFieldList = genBaseFieldsWithoutVector();
Map<String, Object> vectorField = new HashMap<>();
......@@ -174,6 +178,7 @@ public class Utils {
Integer value = jsonObject.getInteger(key);
return value;
}
public static List<Float> getVector(List<Map<String,Object>> entities, int i){
List<Float> vector = new ArrayList<>();
entities.forEach(entity -> {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册