简体   繁体   English

Neo4j Cypher Query:将多个关系匹配到一个节点

[英]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. 我目前正在玩Neo4j和cypher,我一直在寻找很多东西,但是找不到解决方案。 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. 我有一个项目,我在其中解析书籍集合,并使用neo4j存储单词,句子和章节之间的关系。

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) 我的目标是编写一个Cypher查询来匹配用户查询,例如:“一次”(“一次”是连续的单词)

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. 但是这样做时没有任何结果,当用neoclipse检查时,我可以看到存在这样的结果。 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 经过一些额外的搜索后,我似乎遇到了与此处描述相同的问题: 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 我不得不将查询分为两个MATCH子句,其中的WITH描述了先前使用的标识符以及下一个MATCH子句所需的标识符

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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