I need to create a vector index to a DB in ArcadeDB

58 Views Asked by At

I am sorry normally I avoid duplicate questions in different forums, but I asked in the discussions on GitHub, but I got no answer there yet, sorry for the duplicate.

My problem is that I need to instantiate a vector index in a project I am working on, and I don't know how to do it. Ideally, I would like to know if there is a way to do it via SQL or the GUI.

If I understand correctly, the documentation gives examples in Java or reading a dump in SQL.

My main question is if there is an example, complete one, to create and insert, retrieve the information using the vector capacity of arcadedb. Idealy I wish to know if I can create and handle it via sql, cypher....

As I understood Java is the preferred language, I tried to create a Java program to use the example on the documentation, but... I am ashamed to admit I didn't manage to go too far. I managed to connect to the database and add an element, but when I tried to create the index... I got different errors. The code I am trying in Java is the following


import java.util.List;

import com.github.jelmerk.knn.Item;
import com.arcadedb.database.Database;
import com.arcadedb.database.DatabaseFactory;
import com.arcadedb.database.MutableDocument;
import com.arcadedb.index.vector.HnswVectorIndex;
import com.arcadedb.index.vector.HnswVectorIndexRAM;
import com.arcadedb.remote.RemoteDatabase;
import com.arcadedb.schema.VectorIndexBuilder;
import com.github.jelmerk.knn.DistanceFunctions;
import com.github.jelmerk.knn.SearchResult;
import com.arcadedb.graph.Vertex;
import Word;

public class RemoteDatabaseQueries{

  public static void main(String[] args) {

        RemoteDatabase database = new RemoteDatabase("localhost", 2480, "mydb_test", "root", "rootroot");
// Error functions do not exists
//      if (!database.exists()) {
//          database.create();  
//      }
        


        String schema =
         """
            create vertex type Word if not exists;
            create property Word.word if not exists string;
            create index if not exists on Word (word) unique;
         """;
         database.command("sqlscript", schema);


        MutableDocument customer = database.newDocument("Word");
        customer.set("word", "Jayson");
        customer.save(); 
        
        customer = database.newDocument("Word");
        customer.set("word", "Jayson B.");
        customer.save();
        

            // different errors, I am not even sure the way do define is correct
        HnswVectorIndexRAM<String, float[], Word, Float> hnswIndex = HnswVectorIndexRAM.newBuilder(300, DistanceFunctions.FLOAT_COSINE_DISTANCE , 300)
          .withM(16).withEf(200).withEfConstruction(200).build();


        VectorIndexBuilder persistentIndex = hnswIndex.createPersistentIndex(database)//
        .withVertexType("Word").withEdgeType("Proximity").withVectorPropertyName("vector").withIdProperty("name").create();
        
        persistentIndex.save();



        HnswVectorIndex persistentIndex = hnswIndex.createPersistentIndex((Database) database)//
        .withVertexType("Word").withEdgeType("Proximity").withVectorPropertyName("vector").withIdProperty("name").create();
        
        persistentIndex.save();
        


        List<SearchResult<Vertex, Float>> approximateResults = persistentIndex.findNeighbors("Jayson", 3);

        System.out.println("End");
        }
  
       
  
}

Word.java

import com.github.jelmerk.knn.Item;
public class Word implements Item<Object, float[]> {

    @Override
    public int dimensions() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public String id() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public float[] vector() {
        // TODO Auto-generated method stub
        return null;
    }
    // Your implementation details here
}

Apologies if these questions seem basic or naive. If you have a comprehensive example encompassing the creation of a database, addition of an index, node insertion, and relationship assessment, it would be immensely helpful. It does not import the language, but a fully consistent and with minimum external dependencies example... would help many people, I guess.

0

There are 0 best solutions below