neo4j query result changed base on return node and properties

58 Views Asked by At

I have a query with 2 different return nodes. My question is why when I return m2 node, result will change and when remove m2 node from return statement, result will change too. result dcount change by removing or adding m2. my question is why?!!

first query with m2 in return statement:

MATCH (a1:Example)<-[r1:REL_EXAMPLE]-(a2:Example),
      (a1)-[:REL_2_EXAMPLE]->(m1:Mexample),
      (a2)-[:REL_2_EXAMPLE]->(m2:Mexample)
WHERE a1.key = 123456
      AND a2.key <> a1.key
      AND (r1.delta < 300 AND r1.delta > 20)
      AND ((a1.love <> 0 OR a2.love <> 0) OR (abs(a1.love - a2.love) < 0.1))
WITH a1,a2,m1,m2,r1, CASE
WHEN exists((m1)<-[:REL_2_EXAMPLE]-(a2)) OR exists((m2)<-[:REL_2_EXAMPLE]-(a1)) THEN 0.20
      ELSE 1
  END AS factor
RETURN count(r1) * factor as dcount,a2.title as anode, a2.key as id, m2.full_name ORDER BY dcount DESC LIMIT 30;

second query without m2:

MATCH (a1:Example)<-[r1:REL_EXAMPLE]-(a2:Example),
      (a1)-[:REL_2_EXAMPLE]->(m1:Mexample),
      (a2)-[:REL_2_EXAMPLE]->(m2:Mexample)
WHERE a1.key = 123456
      AND a2.key <> a1.key
      AND (r1.delta < 300 AND r1.delta > 20)
      AND ((a1.love <> 0 OR a2.love <> 0) OR (abs(a1.love - a2.love) < 0.1))
WITH a1,a2,m1,m2,r1, CASE
WHEN exists((m1)<-[:REL_2_EXAMPLE]-(a2)) OR exists((m2)<-[:REL_2_EXAMPLE]-(a1)) THEN 0.20
      ELSE 1
  END AS factor
RETURN count(r1) * factor as dcount,a2.title as anode, a2.key as id ORDER BY dcount DESC LIMIT 30;
1

There are 1 best solutions below

0
On BEST ANSWER

This happens because you are performing an aggregation operation in the return statement. In the first query with m2 in the return statement.

RETURN count(r1) * factor as dcount,a2.title as anode, a2.key as id, m2.full_name ORDER BY dcount DESC LIMIT 30;

dcount is calculated for each distinct combination of a2.title, a2.key and m2.full_name.

In the second query, with the return statement

RETURN count(r1) * factor as dcount,a2.title as anode, a2.key as id ORDER BY dcount DESC LIMIT 30;

dcount is calculated, for each distinct combination of a2.title and a2.key, since the aggregation criteria for each query is different, hence the results are different.