Apache AGE扩展内的Cypher查询中,WHERE子句如何工作?

huangapple go评论53阅读模式
英文:

How does the WHERE clause work in the apache AGE extension inside a cypher query?

问题

以下是已翻译的部分:

这是数据设置:

select * from cypher('test', $$ CREATE (david:Person {name: 'David'})
CREATE (alice:Person {name: 'Alice'})
CREATE (bob:Person {name: 'Bob'})
CREATE (charlie:Person {name: 'Charlie'})
CREATE (eve:Person {name: 'Eve'})

CREATE (david)-[:FRIEND]->(alice)
CREATE (david)-[:FRIEND]->(bob)
CREATE (david)-[:FRIEND]->(charlie)
CREATE (david)-[:FRIEND]->(eve)
CREATE (alice)-[:FRIEND]->(bob)                                    
CREATE (charlie)-[:FRIEND]->(bob)
CREATE (eve)-[:FRIEND]->(frank:Person {name: 'Frank'})
CREATE (frank)-[:FRIEND]->(eve) 
CREATE (alice)-[:FRIEND]->(charlie) $$ ) as (a agtype);

我正在寻找一个查询,该查询返回与David是朋友并具有2个或更多出站关系的人。根据文档,此查询应该能够实现:

SELECT *
FROM cypher('graph_name', $$
    MATCH (david {name: 'David'})-[]-(otherPerson)-[]->()
    WITH otherPerson, count(*) AS foaf
    WHERE foaf > 1
    RETURN otherPerson.name
$$) as (name agtype);

然而,这返回了Alice、Eve和Charlie。就像WHERE子句完全被忽略一样。正确的查询是什么?

英文:

This is the data setup:

select * from cypher('test', $$ CREATE (david:Person {name: 'David'})
CREATE (alice:Person {name: 'Alice'})
CREATE (bob:Person {name: 'Bob'})
CREATE (charlie:Person {name: 'Charlie'})
CREATE (eve:Person {name: 'Eve'})

CREATE (david)-[:FRIEND]->(alice)
CREATE (david)-[:FRIEND]->(bob)
CREATE (david)-[:FRIEND]->(charlie)
CREATE (david)-[:FRIEND]->(eve)
CREATE (alice)-[:FRIEND]->(bob)                                    
CREATE (charlie)-[:FRIEND]->(bob)
CREATE (eve)-[:FRIEND]->(frank:Person {name: 'Frank'})
CREATE (frank)-[:FRIEND]->(eve) 
CREATE (alice)-[:FRIEND]->(charlie) $$ ) as (a agtype);

Im searching for the query that returns the person that is a friend of David and has 2 or more outgoing relationships. According to the documantation this query should do it:

SELECT *
FROM cypher('graph_name', $$
	MATCH (david {name: 'David'})-[]-(otherPerson)-[]->()
	WITH otherPerson, count(*) AS foaf
	WHERE foaf > 1
	RETURN otherPerson.name
$$) as (name agtype);

However this returns Alice, Eve, and Charlie. It is like the WHERE clause is completely ignored. What is the correct query?

答案1

得分: 2

这是最近修复的错误(即在忽略WHERE子句的情况下)。因此,直到现在,最新的版本v1.2.0没有包含这个修复。

如果你想让它生效,从GitHub仓库https://github.com/apache/age上获取最新的更改,并重新构建age。

以下是我在我的机器上得到的输出:

Apache AGE扩展内的Cypher查询中,WHERE子句如何工作?

英文:

This was a bug that is fixed recently (i.e WITH ignoring WHERE clause). So until now, the newest release that is v1.2.0 doesn't incorporate this fix.

If you want to make it work, pull the recent changes from github repo https://github.com/apache/age and rebuild age.

Here is what I get as output on my machine

Apache AGE扩展内的Cypher查询中,WHERE子句如何工作?

答案2

得分: 1

One approach of solving this problem is as follows:

首先,我们对FRIEND关系运行查询,然后使用size函数来计算出站关系的数量,然后使用withcarry子句来提取具有2个或更多出站关系的朋友。以下是相应的代码:

SELECT *
FROM cypher('test', $$
    MATCH (david:Person {name: 'David'})-[:FRIEND]->(friend)
    WITH friend, size((friend)-[:FRIEND]->()) as outgoingCount
    WHERE outgoingCount >= 2
    RETURN friend.name
$$) as (name agtype);
英文:

One approach of solving this problem is as follows:

Firstly, we run a query on the FRIEND relationship and then use size to count the relationships that are outgoing and the use the with and carry clause to extract the friends that have 2 or more outgoing repationships. Here's the code for it:

SELECT *
FROM cypher('test', $$
    MATCH (david:Person {name: 'David'})-[:FRIEND]->(friend)
    WITH friend, size((friend)-[:FRIEND]->()) as outgoingCount
    WHERE outgoingCount >= 2
    RETURN friend.name
$$) as (name agtype);

答案3

得分: 0

这个查询会返回是David的朋友并且有2个或更多出站关系的人:

SELECT *
FROM cypher('graph_name', $$
    MATCH (david:Person {name: 'David'})-[:FRIEND]->(otherPerson)
    WITH otherPerson, count(*) AS foaf
    WHERE foaf >= 2
    RETURN otherPerson.name
$$) as (name agtype);
英文:

This query will return the person that is a friend of David and has 2 or more outgoing relationships.

SELECT *
FROM cypher('graph_name', $$
    MATCH (david:Person {name: 'David'})-[:FRIEND]->(otherPerson)
    WITH otherPerson, count(*) AS foaf
    WHERE foaf >= 2
    RETURN otherPerson.name
$$) as (name agtype);

答案4

得分: 0

代码示例翻译:

  • 明确指定关系应该是类型为 :FRIEND,使用 MATCH (david:Person {name: 'David'})-[:FRIEND]->(otherPerson)
  • 统计 otherPerson 节点的数量,而不是所有的关系,使用 count(otherPerson) AS foaf
  • 使用 WHERE foaf >= 2 来筛选具有 2 个或更多出站 :FRIEND 关系的节点。
英文:

It appears that the issue with your query lies in the WHERE clause and how you're counting the number of outgoing relationships. The count(*) is returning the count of all relationships of otherPerson, and that's why you're getting unexpected results. To count only the outgoing relationships from otherPerson, you need to adjust your query like this:

SELECT *
FROM cypher('graph_name', $$
    MATCH (david:Person {name: 'David'})-[:FRIEND]->(otherPerson)
    WITH otherPerson, count(otherPerson) AS foaf
    WHERE foaf >= 2
    RETURN otherPerson.name
$$) as (name agtype);

  • explicitly specify that the relationship should be of type :FRIEND using MATCH (david:Person {name: 'David'})-[:FRIEND]->(otherPerson).
  • count the number of otherPerson nodes, not all relationships, using count(otherPerson) AS foaf.
  • use WHERE foaf >= 2 to filter for nodes with 2 or more outgoing :FRIEND relationships.

答案5

得分: -1

以下是已翻译的内容:

"看起来 Apache AGE 忽略了 cypher 函数内部的 WHERE 子句。您可以通过在 cypher 函数外部使用 WHERE 子句,并在 RETURN 子句中添加 foaf 来执行它,就像这样:

SELECT * FROM cypher('test', $$
    MATCH (david {name: 'David'})-[]-(otherPerson)-[]->() 
    WITH otherPerson, count(*) AS foaf 
    RETURN otherPerson.name, foaf 
    $$) as (name agtype, foaf agtype)
    WHERE foaf > 1;

此查询返回了与 David 为朋友且具有 2 个或更多出站关系的每个人的姓名。根据提供的数据,这将是 Alice。"

英文:

It does appear that Apache AGE is ignoring the WHERE clause inside the cypher function. One way you could do it is by using the WHERE clause outside the cypher function and adding foaf at the RETURN clause, just like this:

SELECT * FROM cypher('test', $$
    MATCH (david {name: 'David'})-[]-(otherPerson)-[]->() 
    WITH otherPerson, count(*) AS foaf 
    RETURN otherPerson.name, foaf 
    $$) as (name agtype, foaf agtype)
    WHERE foaf > 1;

This query returns every person's name that is a friend of David and has 2 or more outgoing relationships. According to the data provided it would be Alice.
Apache AGE扩展内的Cypher查询中,WHERE子句如何工作?

huangapple
  • 本文由 发表于 2023年3月21日 02:43:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75794146.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定