简体   繁体   English

匹配连接到同一中介的节点-cypher / neo4j

[英]Match nodes connected to same intermediary - cypher/neo4j

I'm working on a graph with content nodes that are tagged with term nodes. 我正在处理带有用术语节点标记的内容节点的图。 I'm trying to get a query working that returns (matched) term nodes that are connected to the same content nodes as other (search) term nodes. 我正在尝试查询返回与其他(搜索)术语节点连接到相同内容节点的(匹配)术语节点的查询。

(matched:term)--(contentNode:content)--(search:term)

The problem I'm having is returning matched term nodes that are connected to content that has all of search terms connected to it, rather than any. 我遇到的问题是返回匹配的术语节点,这些节点连接到已连接所有搜索词而不是任何搜索词的内容。

Here is a simplified version of my current query: 这是我当前查询的简化版本:

query = [
            'MATCH (matched:term)<-[:TAGGED_WITH]-(contentNode:content)-[:TAGGED_WITH]->(searchTerms:term) ',
            'WHERE searchTerms.UUID IN {searchTerms} ',
            'RETURN DISTINCT matched.name AS name, matched.UUID AS UUID, matched.contentConnections AS connections ',
            'ORDER BY connections DESC LIMIT 10'
        ].join('\n');

Here is an example in practice - the top group of terms are the terms matched by my query, the bottom are the terms being used for the search: 这是一个实际的示例-术语的顶部是与我的查询匹配的术语,底部是用于搜索的术语:

搜索图片

The term 'astronomy' should not be returned, but it is because some content tagged with 'image' is also tagged with 'astronomy'. 术语“天文学”不应返回,这是因为某些标有“图像”的内容也标有“天文学”。

Graph representation of the results: 结果的图形表示:

图代表1

Note that only one piece of content (the gray nodes) matches both terms (it's hard to tell because the node ID is what is displayed). 请注意,只有一个内容(灰色节点)与两个术语都匹配(这很难说,因为节点ID是显示的内容)。 What is expected is to only return the terms that are connected to the content that has been tagged with both "image" and "organ" 期望的是仅返回与同时被“图像”和“器官”标记的内容相关的术语

Another example: 另一个例子:

第二次搜寻图片

This search should yield no terms, because no content is tagged with both 'organ' and 'astronomy', however, it is returning terms tagged to either. 此搜索不应产生任何术语,因为没有内容被同时标记为“器官”和“天文学”,但是,它返回的术语都被标记为其中一个。

Graph representation: 图形表示:

图代表2

I hope I've made my question clear. 我希望我已经阐明了我的问题。 I've tried working with count() to group the results but struggling to get it to work. 我试过使用count()将结果分组,但是努力使其工作。

Any help would be appreciated. 任何帮助,将不胜感激。

EDIT: 编辑:

Query: 查询:

query = [
    'MATCH (contentNode:content)-[:TAGGED_WITH]->(searchTerms:term) ',
    'WITH contentNode, COUNT(contentNode) as countContent, searchTerms ',
    'WHERE searchTerms.UUID IN {searchTerms} AND countContent = {searchTermsCount} ',
    'MATCH (typeNode:termType)<-[:IS_TYPE]-(matched:term)<-[:TAGGED_WITH]-contentNode, ',
        'matched-[:HAS_LANGUAGE {languageCode: {language} }]-(termMeta:termMeta) ',
    'WHERE NOT matched.UUID IN {ignoreTerms}',
    'RETURN DISTINCT termMeta.name AS name, matched.UUID AS UUID, matched.contentConnections AS connections ',
    'ORDER BY connections DESC LIMIT 10'
].join('\n');

With the following two terms selected, the query is expected to return terms tagged to any of those pieces of content ('evolution', 'ant', etc.), however, with the above query, no terms are returned. 选择以下两个术语后,查询将返回标有那些内容(“进化”,“蚂蚁”等)中任何一个的术语,但是,使用上述查询不会返回任何术语。

更新问题

You can try splitting your query in two: 您可以尝试将查询分为两部分:

MATCH (contentNode:content)-[:TAGGED_WITH]->(searchTerms:term)
WHERE searchTerms.UUID IN {searchTerms}
WITH contentNode, COUNT(*) as cnt
WHERE cnt = {_searchTerms_size_} 
MATCH (matched:term)<-[:TAGGED_WITH]-contentNode    
RETURN DISTINCT matched.name AS name, matched.UUID AS UUID, matched.contentConnections AS connections
ORDER BY connections DESC LIMIT 10

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

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