Starting Neo4j Graph Database in Docker

Prerequisite:

  • Docker Engine
  • Docker Compose

Preparing your docker-compose.yml script

services:
  neo4j:
    container_name: neo4j
    image: neo4j:latest
    ports:
      - 7474:7474
      - 7687:7687
    environment:
      - NEO4J_AUTH=neo4j/${NEO4J_PASSWORD}
      - NEO4J_apoc_export_file_enabled=true
      - NEO4J_apoc_import_file_enabled=true
      - NEO4J_apoc_import_file_use__neo4j__config=true
      - NEO4J_PLUGINS=["apoc", "graph-data-science"]
    volumes:
      - ./neo4j_db/data:/data
      - ./neo4j_db/logs:/logs
      - ./neo4j_db/import:/var/lib/neo4j/import
      - ./neo4j_db/plugins:/plugins

The variable ${NEO4J_PASSWORD} will be loaded from environment variable only if its set, otherwise te default password = ‘password’ will be used.

Start with docker-compose up -d command

The neo4j database UI would be up and running at http://localhost:7474

Your Neo4j server is up and running at port 7687

The deafult username and password for the server is

username : neo4j
password : password

I will be soon writing a blog on how to load neo4j with your data. Stay tuned! 🙂

Semantic Similarity Search with graph DB

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.

Exit mobile version