Semantic Similarity Search with graph DB

Semantic similarity search with graphdb

GraphDB or Knowledge Graph uses Graph like structure to represent data. Each data element in a graphDB is represented by a Node. In this section we will be exploring how semantic search is carried for nodes in Neo4j Graph database.

This section assumes that

  • Your Neo4j instance is up and running
  • You have loaded your Neo4j with some data
  • Added embeddings field to each node where you have to perform a similarity search

 Lets assume We have following relationship in our graph database.
(auditEvent:AuditEvent)-[:HAS_FIELD_GROUPS]->(fieldGroup:FieldGroup)

Inorder to do a semantic search over fieldGroup for a particular auditEvent. The below given cypher would return all the nodes of fieldGroups which satisfies the condition.

Here we are using cosine similarity search to find the most similar nodes.

from langchain_community.graphs import Neo4jGraph

cypher = '''
MATCH (auditEvent:AuditEvent{ eventCode: $eventCode })-[:HAS_FIELD_GROUPS]->(fieldGroup:FieldGroup)  
        
            WITH auditEvent,fieldGroup,  

            vector.similarity.cosine(fieldGroup.embedding, $embedding) AS score WHERE score > 0.8  

            RETURN fieldGroup.fgCode, fieldGroup.name, score order by score desc limit 1
'''

#where objEmbedding is the embeddings data
objects = findObject('Request Save', objEmbedding) 
print(objects)

The variable $eventCode and $embedding must be provided at the runtime.

The python code to return the nodes would like this :

def findObjects(eventCode, objEmbedding)
  graph = Neo4jGraph(url="bolt://localhost:7687", username="neo4j", password="password", enhanced_schema=True,)

  response = graph.query (cypher, {'eventCode':eventCode, 'embedding' : objEmbedding})
  return response

Conclusion

We learned on how we can semantically search for values from graphdb and return the most meaningful matches form the graphDB.

Leave a Reply

Your email address will not be published. Required fields are marked *