Neo4j Cypher: 寻找一组匹配节点之间的共同节点

您所在的位置:网站首页 magdalla港口 Neo4j Cypher: 寻找一组匹配节点之间的共同节点

Neo4j Cypher: 寻找一组匹配节点之间的共同节点

#Neo4j Cypher: 寻找一组匹配节点之间的共同节点| 来源: 网络整理| 查看: 265

百度翻译此文   有道翻译此文 问题描述

Very similar to the question posted here

I have the following nodes: Article and Words. Each word is connected to an article by a MENTIONED relationship.

I need to query all articles that have common words where the list of common words is dynamic. From the clients perspective, I am passing back a list of words and expecting back a results of articles that have those words in common.

The following query does the job

WITH ["orange", "apple"] as words MATCH (w:Word)(w2:Word) WHERE w.name IN words AND w2.name IN words RETURN a, w, w2

but does not work with word list of one. How can I make it handle any number of words? Is there a better way to do this?

推荐答案

Yes. There are two approaches I can think of:

Finding all articles that contain some subset of those words, and then returning only articles where the number of words mentioned is the number of words you supplied in your wordlist.

Getting the :Word nodes for the given list of words, and then getting articles where all words are mentioned in the article.

Here's an example graph to test this on:

MERGE (a1:Article {name:'a1'}), (a2:Article {name:'a2'}), (a3:Article {name:'a3'}) MERGE (w1:Word{name:'orange'}), (w2:Word{name:'apple'}), (w3:Word{name:'pineapple'}), (w4:Word{name:'banana'}) MERGE (a1)-[:MENTIONED]->(w1), (a1)-[:MENTIONED]->(w2), (a1)-[:MENTIONED]->(w3), (a1)-[:MENTIONED]->(w4), (a2)-[:MENTIONED]->(w1), (a2)-[:MENTIONED]->(w4), (a3)-[:MENTIONED]->(w1), (a3)-[:MENTIONED]->(w2), (a3)-[:MENTIONED]->(w3)

Approach 1, comparing the wordlist size to the number of words mentioned in the article, looks like this:

WITH ["orange", "apple"] as words MATCH (word:Word)


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3