2

I'm pretty new to Neo4j; I've only gotten as far as writing a hello world. Before I proceed, I want to make sure I have the right idea about how Neo4j works and what it can do for me.

As an example, say you wanted to write a Neo4j back end for a site like this. Questions would be nodes. Naïvely, tags would be represented by an array property on the question node. If you wanted to find questions with a certain tag, you'd have to scan every question in the database.

I think a better approach would to represent tags as nodes. If you wanted to find all questions with a certain tag, you'd start at the tag node and follow the relationships to the questions. If you wanted to find questions with all of a set of tags, you'd start at one of the tag nodes (preferably the least common/most specific one, if you know which one that is), follow its relationships to questions, and then select the questions with relationships to the other tags. I don't know how to express that in Cypher yet, but is that the right idea?

In my real application, I'm going to have entities with a potentially long list of tags, and I'm going to want to find entities that have all of the requested tags. Is this something where Neo4j would have significant advantages over SQL?

Kevin Krumwiede
  • 9,868
  • 4
  • 34
  • 82

1 Answers1

1

Kevin, correct.

You'd do it like that.

I even created a model some time ago for stackoverflow that does this.

For Cypher you can imagine queries like these

Find the User who was most active

MATCH (u:User)
OPTIONAL MATCH (u)-[:AUTHORED|ASKED|COMMENTED]->()
RETURN u,count(*)
ORDER BY count(*) DESC
LIMIT 5

Find co-used Tags

MATCH (t:Tag)
OPTIONAL MATCH (t)<-[:TAGGED]-(question)-[:TAGGED]->(t2)
RETURN t.name,t2.name,count(distinct question) as questions
ORDER BY questions DESC


MATCH (t:Tag)<-[r:TAGGED]->(question)
RETURN t,r,question

enter image description here

Michael Hunger
  • 41,339
  • 3
  • 57
  • 80
  • I also found a similar question where yours was the accepted answer. I can't parse that answer yet, but I'm linking it here because it seems related. http://stackoverflow.com/a/15980742/1953590 – Kevin Krumwiede Aug 08 '14 at 18:46