neo4j / neo4j-python-driver

Neo4j Bolt driver for Python
https://neo4j.com/docs/api/python-driver/current/
Other
893 stars 187 forks source link

How can I get additional relationships among nodes? #1014

Closed Starfruit007 closed 7 months ago

Starfruit007 commented 7 months ago

Feature Request

Hello,

I have a problem when running the follow query that I cannot get the additional relationships among node m. Here are the details.

Query: MATCH (n)-[r]-(m) RETURN n,r,m LIMIT 300

Issue: When I run the above queries both in neo4j-browser and neo4j.GraphDatabase in python, the first one will return more relationships. I find that neo4j-browser will return the additional relationships among node m.

Failed Self-Tries:

code:

from neo4j import GraphDatabase
self.driver = GraphDatabase.driver(uri, auth=(username, password))
with self.driver.session() as session:
    results = session.run(query)

Question: Do you know how to get the additional relationships among nodes m?

Best regards.

Pitch

Generality explanation: In my opinion, this is a very general request, because not all the relationships are returned, the users may misunderstand that these not displayed relationships are not existing.

robsdedude commented 7 months ago

Hi and thanks for reaching out.

Firstly, this isn't really a feature request as it's not really about adding functionality to the driver. If you are unsure about the usage of the driver, this issue tracker is not the right place to ask. Please use one of the following channels instead:

or check the documentation:

With that said, and now that I'm here anyway, there's a config option in Neo4j browser (enabled by default), that will automatically, in the background fetch relationships between nodes you've queried. Try disabling it to see if you then get the same results.

image

For further questions, please use one of the resources linked above.

Starfruit007 commented 7 months ago

Thank you very much for your quick and detailed response. According to your guide I understand now that this is down by a second query, and you are right, this is then not a new feature request.

Starfruit007 commented 7 months ago

Thanks to @robsdedude's response, I have found how to run a second query.

Although I don't know how neo4j browser realized the "connect_result_nodes" function, with the help of LLMs I found the following function works fine.

  def connect_result_nodes(self, results):
      graph = results.graph()
      ids = [int(ky) for ky in graph._nodes]
      query =  f" MATCH (n)WHERE id(n) IN {ids} WITH n OPTIONAL MATCH (n)-[r]-(m) WHERE id(m) IN {ids} RETURN n,r,m"
      return query

Thanks very much again @robsdedude .