Skip to content

Instantly share code, notes, and snippets.

@gkorland
Last active December 3, 2023 12:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gkorland/f442f629e33cc81619dc9a813074e28d to your computer and use it in GitHub Desktop.
Save gkorland/f442f629e33cc81619dc9a813074e28d to your computer and use it in GitHub Desktop.
package com.falkordb;
import redis.clients.jedis.UnifiedJedis;
import redis.clients.jedis.graph.ResultSet;
import redis.clients.jedis.graph.Record;
public class FalkorDBVectorDemo {
public static void main(String args[]) {
try (UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379")) {
// Create Vector index on field description in Character
jedis.graphQuery("Books", "CREATE VECTOR INDEX FOR (c:Character) ON (c.description) OPTIONS {dimension:5, similarityFunction:'euclidean'}");
// Fill in the Graph with some data on books and characters
jedis.graphQuery("Books", "CREATE "
+ "(:Character {name:'Bastian Balthazar Bux', description:vecf32([0.1, 0.3, 0.3, 0.4, 0.7])})-[:in]->(book1:Book {name:'The Neverending Story'}), "
+ "(:Character {name:'Atreyu', description:vecf32([0.3, 0.6, 0.2, 0.1, 0.4])})-[:in]->(book1), "
+ "(:Character {name:'Jareth', description:vecf32([0.1, 0.3, 0.1, 0.2, 0.9])})-[:in]->(book2:Book {name:'Labyrinth'}), "
+ "(:Character {name:'Hoggle', description:vecf32([0.3, 0.2, 0.5, 0.7, 0.9])})-[:in]->(book2)");
// Find the book with the character description that is most similar (k=1) to the user's query
ResultSet result = jedis.graphQuery("Books", "CALL db.idx.vector.queryNodes("
+ "'Character', 'description', 1, vecf32([0.1, 0.4, 0.3, 0.2, 0.7])) "
+ "YIELD entity "
+ "MATCH (entity)-[]->(b:Book) "
+ "RETURN b.name AS name");
// Print out the name
for (Record record : result) {
System.out.println(record.getString("name"));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment