@ -44,57 +44,58 @@ import org.bson.Document;
@@ -44,57 +44,58 @@ import org.bson.Document;
*
* @author Christoph Strobl
* /
public class Vector Index implements IndexDefinition {
public class Search Index implements Search IndexDefinition {
private final String name ;
private String path ;
private int dimensions ;
private String similarity ;
private List < Filter > filters ;
private String quantization = Quantization . NONE . name ( ) ;
/ * *
* Create a new { @link Vector Index} instance .
* Create a new { @link Search Index} instance .
*
* @param name The name of the index .
* /
public Vector Index( String name ) {
public Search Index( String name ) {
this . name = name ;
}
/ * *
* Create a new { @link Vector Index} instance using similarity based on the angle between vectors .
* Create a new { @link Search Index} instance using similarity based on the angle between vectors .
*
* @param name The name of the index .
* @return new instance of { @link Vector Index} .
* @return new instance of { @link Search Index} .
* /
public static Vector Index cosine ( String name ) {
public static Search Index cosine ( String name ) {
Vector Index idx = new Vector Index( name ) ;
Search Index idx = new Search Index( name ) ;
return idx . similarity ( SimilarityFunction . COSINE ) ;
}
/ * *
* Create a new { @link Vector Index} instance using similarity based the distance between vector ends .
* Create a new { @link Search Index} instance using similarity based the distance between vector ends .
*
* @param name The name of the index .
* @return new instance of { @link Vector Index} .
* @return new instance of { @link Search Index} .
* /
public static Vector Index euclidean ( String name ) {
public static Search Index euclidean ( String name ) {
Vector Index idx = new Vector Index( name ) ;
Search Index idx = new Search Index( name ) ;
return idx . similarity ( SimilarityFunction . EUCLIDEAN ) ;
}
/ * *
* Create a new { @link Vector Index} instance using similarity based on based on both angle and magnitude of the
* Create a new { @link Search Index} instance using similarity based on based on both angle and magnitude of the
* vectors .
*
* @param name The name of the index .
* @return new instance of { @link Vector Index} .
* @return new instance of { @link Search Index} .
* /
public static Vector Index dotProduct ( String name ) {
public static Search Index dotProduct ( String name ) {
Vector Index idx = new Vector Index( name ) ;
Search Index idx = new Search Index( name ) ;
return idx . similarity ( SimilarityFunction . DOT_PRODUCT ) ;
}
@ -104,7 +105,7 @@ public class VectorIndex implements IndexDefinition {
@@ -104,7 +105,7 @@ public class VectorIndex implements IndexDefinition {
* @param path The path using dot notation .
* @return this .
* /
public Vector Index path ( String path ) {
public Search Index path ( String path ) {
this . path = path ;
return this ;
@ -116,7 +117,7 @@ public class VectorIndex implements IndexDefinition {
@@ -116,7 +117,7 @@ public class VectorIndex implements IndexDefinition {
* @param dimensions value between { @code 0 } and { @code 4096 } .
* @return this .
* /
public Vector Index dimensions ( int dimensions ) {
public Search Index dimensions ( int dimensions ) {
this . dimensions = dimensions ;
return this ;
}
@ -129,7 +130,7 @@ public class VectorIndex implements IndexDefinition {
@@ -129,7 +130,7 @@ public class VectorIndex implements IndexDefinition {
* @see SimilarityFunction
* @see # similarity ( SimilarityFunction )
* /
public Vector Index similarity ( String similarity ) {
public Search Index similarity ( String similarity ) {
this . similarity = similarity ;
return this ;
}
@ -140,17 +141,41 @@ public class VectorIndex implements IndexDefinition {
@@ -140,17 +141,41 @@ public class VectorIndex implements IndexDefinition {
* @param similarity must not be { @literal null } .
* @return this .
* /
public Vector Index similarity ( SimilarityFunction similarity ) {
public Search Index similarity ( SimilarityFunction similarity ) {
return similarity ( similarity . getFunctionName ( ) ) ;
}
/ * *
* Quantization used .
*
* @param quantization should be one of { @literal none | scalar | binary } .
* @return this .
* @see Quantization
* @see # quantization ( Quantization )
* /
public SearchIndex quantization ( String quantization ) {
this . quantization = quantization ;
return this ;
}
/ * *
* Quntization used .
*
* @param quantization must not be { @literal null } .
* @return this .
* /
public SearchIndex quantization ( Quantization quantization ) {
return similarity ( quantization . getQuantizationName ( ) ) ;
}
/ * *
* Add a { @link Filter } that can be used to narrow search scope .
*
* @param filter must not be { @literal null } .
* @return this .
* /
public VectorIndex filter ( Filter filter ) {
public Search Index filter ( Filter filter ) {
if ( this . filters = = null ) {
this . filters = new ArrayList < > ( 3 ) ;
@ -167,21 +192,10 @@ public class VectorIndex implements IndexDefinition {
@@ -167,21 +192,10 @@ public class VectorIndex implements IndexDefinition {
* @return this .
* @see # filter ( Filter )
* /
public Vector Index filter ( String path ) {
public Search Index filter ( String path ) {
return filter ( new Filter ( path ) ) ;
}
@Override
public Document getIndexKeys ( ) {
// List<Document> fields = new ArrayList<>(filters.size()+1);
// fields.
// needs to be wrapped in new Document("definition", before sending to server
// return new Document("fields", fields);
return new Document ( ) ;
}
@Override
public Document getIndexOptions ( ) {
return new Document ( "name" , name ) . append ( "type" , "vectorSearch" ) ;
@ -224,4 +238,18 @@ public class VectorIndex implements IndexDefinition {
@@ -224,4 +238,18 @@ public class VectorIndex implements IndexDefinition {
return functionName ;
}
}
public enum Quantization {
NONE ( "none" ) , SCALAR ( "scalar" ) , BINARY ( "binary" ) ;
String quantizationName ;
Quantization ( String quantizationName ) {
this . quantizationName = quantizationName ;
}
public String getQuantizationName ( ) {
return quantizationName ;
}
}
}