How can I find the neighbor nodes when the nodes are not from __Entity__ type?

I am a beginner in learning about Neo4J and Cypher queries, and following this GitHub for building a Hybrid RAG using knowledge graphs. I have added a graph to my Neo4j workspace using Cypher query. The example of commands is shown below:

MERGE (nizhny_novgorod:birthPlace {id: "Nizhny Novgorod"})
MERGE (large_language_models_like_chatgpt:characteristics {id: "large language models like ChatGPT"})
MERGE (richard_zemel:person {id: "Richard Zemel"})
MERGE (mixtures_of_experts:invention {id: "mixtures of experts"})
MERGE (december_6_1947:date {id: "December 6, 1947"})
MERGE (nobel_prize_in_physics:award {id: "Nobel Prize in Physics"})

MERGE (geoffrey_hinton)-[:bornIn]->(london)
MERGE (geoffrey_hinton)-[:bornAt]->(december_6_1947)
MERGE (geoffrey_hinton)-[:worksAt]->(university_of_toronto)
MERGE (geoffrey_hinton)-[:hasNationality]->(britishcanadian)
MERGE (geoffrey_hinton)-[:hasRole]->(cognitive_psychologist_and_computer_scientist)
MERGE (geoffrey_hinton)-[:coAuthored]->(david_rumelhart)
MERGE (geoffrey_hinton)-[:coAuthored]->(ronald_j_williams)

And the problem is that the __Entity__ is empty, and it does not return any node, when I run the MATCH (n:__Entity__) RETURN n LIMIT 25; command.

I faced with this problem when trying to find neighbor nodes while retrieving the knowledge graph using the below function.

# Fulltext index query
def structured_retriever(question: str) -> str:
    """
    Or Graph Retriever
    Collects the neighborhood of entities mentioned in the question
    """
    result = ""
    entities = entity_chain.invoke({"question": question})
    print(f"Extracted entities: {entities.names}")
    for entity in entities.names:
        query = generate_full_text_query(entity)
        print(f"Generated query: {query}")
        response = graph.query(
            """CALL db.index.fulltext.queryNodes('entity', $query, {limit:2})
            YIELD node, score
            CALL {
              WITH node
              MATCH (node)-[r]->(neighbor)
              RETURN node.id + ' - ' + type(r) + ' -> ' + neighbor.id AS output
              UNION ALL
              WITH node
              MATCH (node)<-[r]-(neighbor)
              RETURN neighbor.id + ' - ' + type(r) + ' -> ' +  node.id AS output
            }
            RETURN output LIMIT 50
            """,
            {"query": query},
        )
        print(f"Query response: {response}")
        if response:
            result += "\n".join([el['output'] for el in response])
        else:
            print(f"No results found for entity: {entity}")
    return result

The above query does not return anything. For example, for the below question:

print(structured_retriever("Where was Geoffrey Hinton born"))

Output:

Extracted entities: ['Geoffrey Hinton']
Generated query: Geoffrey~2 AND Hinton~2
Query response: []
No results found for entity: Geoffrey Hinton

To find neighbor nodes in Neo4j when the nodes are not of the __Entity__ type, you can use Cypher queries to traverse relationships between nodes of any type. Here’s how you can do it:

Step 1: Identify the Node

First, determine how you want to identify the starting node. This could be based on a property or a specific label.

Step 2: Write the Cypher Query

You can use a query like the following to find neighbor nodes:

cypher

Copy code

MATCH (n:YourNodeLabel {yourProperty: 'yourValue'})-[:YOUR_RELATIONSHIP_TYPE]-(neighbor)
RETURN neighbor

Explanation:

  • Replace YourNodeLabel with the label of the node you are starting from.
  • Replace yourProperty and yourValue with the property and value you are using to identify the node.
  • Replace YOUR_RELATIONSHIP_TYPE with the type of relationship connecting to neighbor nodes. If you want to find all neighbors regardless of the relationship type, you can use -[]- instead.

Example Query

If you have a node labeled Person with a property name and you want to find all its neighbors, the query would look like this:

cypher

Copy code

MATCH (p:Person {name: 'Alice'})-[]-(neighbor)
RETURN neighbor

Additional Considerations

  • If you want to limit the depth of the traversal, you can specify a maximum number of relationship hops, like this:

cypher

Copy code

MATCH (n:YourNodeLabel {yourProperty: 'yourValue'})-[:YOUR_RELATIONSHIP_TYPE*1..2]-(neighbor)
RETURN neighbor
  • You can also filter neighbors based on specific criteria by adding WHERE clauses.

By using these Cypher queries, you can effectively find neighboring nodes regardless of their type.

Your query looks fine. Does the full text search return results? Execute just it to see.

To find the neighbor nodes when the nodes are not of the __Entity__ type in Neo4j, you can use Cypher queries that match nodes based on their properties or labels, and then traverse the relationships to find neighbors. Here's a step-by-step approach:

  1. Identify the Starting Node: First, determine which node you want to start from. This could be based on a property, label, or ID.Example: If you're looking for a node with a specific ID, you can match it like this:

cypher

Copy code

MATCH (n {id: "Geoffrey Hinton"})
  1. Match Neighboring Nodes: Use Cypher's relationship patterns to match outgoing or incoming relationships to neighboring nodes.For outgoing relationships:

cypher

Copy code

MATCH (n {id: "Geoffrey Hinton"})-[r]->(neighbor)
RETURN neighbor
LIMIT 25;

For incoming relationships:

cypher

Copy code

MATCH (n {id: "Geoffrey Hinton"})<-[r]-(neighbor)
RETURN neighbor
LIMIT 25;
  1. Use Pattern Matching: You can also match nodes based on their label or property to find neighbors of any type of node, not just __Entity__ nodes. Here's an example that matches any node with a relationship to the starting node:

cypher

Copy code

MATCH (n {id: "Geoffrey Hinton"})-[r]-(neighbor)
RETURN neighbor
LIMIT 25;

This approach ensures you can find neighbor nodes even if they are not labeled as __Entity__, as long as there is a relationship connecting them.