|
@@ -34,12 +34,12 @@ import java.util.stream.DoubleStream;
|
|
|
public class MilvusClientExample {
|
|
|
|
|
|
// Helper function that generates random vectors
|
|
|
- static List<List<Float>> generateVectors(long vectorCount, long dimension) {
|
|
|
- SplittableRandom splitCollectionRandom = new SplittableRandom();
|
|
|
- List<List<Float>> vectors = new ArrayList<>();
|
|
|
- for (long i = 0; i < vectorCount; ++i) {
|
|
|
- splitCollectionRandom = splitCollectionRandom.split();
|
|
|
- DoubleStream doubleStream = splitCollectionRandom.doubles(dimension);
|
|
|
+ static List<List<Float>> generateVectors(int vectorCount, int dimension) {
|
|
|
+ SplittableRandom splitcollectionRandom = new SplittableRandom();
|
|
|
+ List<List<Float>> vectors = new ArrayList<>(vectorCount);
|
|
|
+ for (int i = 0; i < vectorCount; ++i) {
|
|
|
+ splitcollectionRandom = splitcollectionRandom.split();
|
|
|
+ DoubleStream doubleStream = splitcollectionRandom.doubles(dimension);
|
|
|
List<Float> vector =
|
|
|
doubleStream.boxed().map(Double::floatValue).collect(Collectors.toList());
|
|
|
vectors.add(vector);
|
|
@@ -47,7 +47,8 @@ public class MilvusClientExample {
|
|
|
return vectors;
|
|
|
}
|
|
|
|
|
|
- // Helper function that normalizes a vector if you are using Inner Product as your metric type
|
|
|
+ // Helper function that normalizes a vector if you are using IP (Inner Product) as your metric
|
|
|
+ // type
|
|
|
static List<Float> normalizeVector(List<Float> vector) {
|
|
|
float squareSum = vector.stream().map(x -> x * x).reduce((float) 0, Float::sum);
|
|
|
final float norm = (float) Math.sqrt(squareSum);
|
|
@@ -79,8 +80,8 @@ public class MilvusClientExample {
|
|
|
|
|
|
// Create a collection with the following collection mapping
|
|
|
final String collectionName = "example"; // collection name
|
|
|
- final long dimension = 128; // dimension of each vector
|
|
|
- final long indexFileSize = 1024; // maximum size (in MB) of each index file
|
|
|
+ final int dimension = 128; // dimension of each vector
|
|
|
+ final int indexFileSize = 1024; // maximum size (in MB) of each index file
|
|
|
final MetricType metricType = MetricType.IP; // we choose IP (Inner Product) as our metric type
|
|
|
CollectionMapping collectionMapping =
|
|
|
new CollectionMapping.Builder(collectionName, dimension)
|
|
@@ -223,4 +224,4 @@ public class MilvusClientExample {
|
|
|
throw e;
|
|
|
}
|
|
|
}
|
|
|
-}
|
|
|
+}
|