CTE query in neo4j

15 views
Skip to first unread message

Ryan Velasco

unread,
Oct 5, 2015, 1:11:52 AM10/5/15
to Neo4j, Michael Hunger
How do you do CTE queries in neo4j?

Getting all the buyers and buyers and buyers.... of the seller and its seller.

So outcome should be:
Seller - Search Seller - Buyer - Buyer of Buyer - Buyer of Buyer


SELLER BUYER relationship is structure this way
 (sellerCompanyTopic:Company)-[rel:Seller]->(sr:SalesRelation)<-[rel2:Buyer]-(buyer:Company)



SAMPLE QUERY WHICH RETURNS OKAY BUT NOT SO FAST:

MATCH (sellerCompanyTopic:Company)-[rel:Seller]->(sr:SalesRelation)-[rel2:Buyer]-(buyer:Company), 
sr<-[:PriceAgreement]-(contract:Contract)
MATCH (rootTopic:Company) WHERE rootTopic.ID = 172096
OPTIONAL MATCH p = shortestPath(rootTopic-[:Seller|Buyer*]-sellerCompanyTopic)
WITH rootTopic, sellerCompanyTopic, buyer, contract,  
reduce(
index = 0, 
type IN EXTRACT(r in relationships(p) | type(r))| 
case 
when index % 2 = 0 AND type = 'Buyer'
then -1000
when index % 2 = 1 AND type = 'Seller'
then -1000
ELSE index + 1
END
) AS Related
WHERE rootTopic = sellerCompanyTopic OR rootTopic = buyer OR Related > 0
RETURN rootTopic.ID AS RootCompanyTopicID, sellerCompanyTopic.ID AS SellerTopicID, buyer.ID AS BuyerTopicID, contract.ID AS ContractTopicID, case 
when Related is null
then 1
when Related < 0
then 0
ELSE Related
END AS Depth
ORDER BY RootCompanyTopicID, SellerTopicID, BuyerTopicID, ContractTopicID


Best Regards,
Ryan
Reply all
Reply to author
Forward
0 new messages