Pierre-Louis Gottfrois February 2016

Find possible paths in a graph using neo4j

Disclaimer: I'm a beginner with graph data modeling, any help, tips or guidance will be appreciated. Forgive my lack of knowledge.

10 thousand feat idea is to be able to translate texts from one language to another in a certain quality level.

I have the following domain models:

  • An activity (aka. activity)
  • A source language (aka. language_from)
  • A destination language (aka. language_to)
  • A quality level (aka. level)

I have modeled the following graph to allow a user to ask for a translation from X to Y with a quality level Z.

enter image description here

Now let's say a user wants to translate from en-us to de-de. There are no links between the two nodes because business does not allow this path for domain reasons.

However it is possible to achieve such translation going through en-us->fr-fr then fr-fr->de-de.

My questions are:

  1. Is the above graph model appropriate for such query?
  2. What would the query look like using neo4j cyphers?

Any tips to get me on the right track would be appreciated.

Answers


Martin Preusse February 2016

I think you can model your data in a more straight-forward way.

You could use Language nodes directly connected by TRANSLATE relationships with the level as a property on the relationship. It's simpler, directly maps to your domain problem and you don't need other nodes for your query.

(Language {name: 'de-de'})-[:TRANSLATE {level: 7}]->(Language {name: 'fr-fr'})

A variable length query finds translations through mulitple languages:

MATCH path=(lang:Languag {name: 'de-de'})-[:TRANSLATE*1..3]-(other_lang:Language {name: 'en-us'})
RETURN nodes(path)

Here you could also filter for a certain level:

MATCH path=(lang:Languag {name: 'de-de'})-[:TRANSLATE*1..3]-(other_lang:Language {name: 'en-us'})
WHERE all(x IN relationships(path) WHERE x.level > 5)
RETURN nodes(path)

Post Status

Asked in February 2016
Viewed 2,191 times
Voted 11
Answered 1 times

Search




Leave an answer