1
votes

I'm currently toying around with Neo4j and cypher, I've been looking around for quite some but I can't find a solution. So here I go :

I have a project where I parse collection of books and I use neo4j to store relations between words, sentences and chapters.

word1-[:NEXTWORD]->word2 -[:NEXTWORD]->word3...
word-[:COMPOSESENTENCE]->sentence-[:COMPOSECHAPTER]->chapter->[:COMPOSEBOOK]->book

Instead of having several relationship between two word I added a counter which I increase every time the words are consecutive

my goal is to write a Cypher query to match a user query like : "once upon" a time ("once upon" being consecutive words)

START 
    word1=node:node_auto_index(WORD='once'), 
    word2=node:node_auto_index(WORD='upon') 
    word3=node:node_auto_index(WORD='a') 
    word4=node:node_auto_index(WORD='time') 
MATCH 
    word1-[:COMPOSESENTENCE]->sentence-[:COMPOSECHAPTER]->chapter-[:COMPOSEBOOK]->book
    word2-[:COMPOSESENTENCE]->sentence, 
    word3-[:COMPOSESENTENCE]->sentence,
    word4-[:COMPOSESENTENCE]->sentence,  
    word1-[:NEXTWORD]->word2

RETURN 
    version.VERSIONNAME, book.BOOKNAME, chapter.CHAPTERNUMBER, sentence.SENTENCESTRING;

But when doing so I don't get any result, when checking with neoclipse I can see that such a result exist. So please if anyone has an answer I would be glad to give more information and to try whathever solution you may have. Thanks Matt.

1

1 Answers

1
votes

After some extra search it seems I runned into the same issue as describded here : https://groups.google.com/forum/#!topic/neo4j/7ePLU8y93h8

I had to split my query in two MATCH clause with WITH in between where I describe the identifier used previously and needed for the next MATCH clause

START 
    word1=node:node_auto_index(WORD='once'), 
    word2=node:node_auto_index(WORD='upon') 
    word3=node:node_auto_index(WORD='a') 
    word4=node:node_auto_index(WORD='time') 
MATCH 

    word2-[:COMPOSESENTENCE]->sentence, 
    word3-[:COMPOSESENTENCE]->sentence,
    word4-[:COMPOSESENTENCE]->sentence,  
    word1-[:NEXTWORD]->word2
WITH 
    sentence
MATCH
    sentence-[:COMPOSECHAPTER]->chapter-[:COMPOSEBOOK]->book

RETURN 
    version.VERSIONNAME, book.BOOKNAME, chapter.CHAPTERNUMBER, sentence.SENTENCESTRING;