简体   繁体   中英

Neo4j Cypher Query: Match several relationship to one node

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.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM