Optimise Cypher query

Hi all,

I am quite new to Neo4j and cypher queries and it would be great if you could help me.

I have the following nodes:

  • Transaction (tx)

  • Output (output)

with the following relations:

  • Incoming (in)

  • Outgoing (out)

a Transaction creates 1-n outputs which can be used as inputs for other transactions.

I want to check, which outputs and transactions are children of a target transaction (means, created inputs for the target transaction.) This query checks 2 hops back:


MATCH path= (roottx:tx {txid:'266160772'})<-[:in|:out]-(output0:output)<-[:in|:out]-(subtx:tx)<-[:in|:out]-(output1:output)<-[:in|:out]-(subtx1:tx)<-[:in|:out]-(output2:output)

RETURN path

I have the aim, that the query should be flexible - means it should be able to check more hops back.

I think there is definitely a better way than extending the query.

I already had a look in APOC methods - but I was not able to find the right one.

Thanks a Lot for your help and your time!

Hello @peetsees and welcome to the Ne4j community :slight_smile:

You should have a look at this:

Regards,
Cobra

@cobra: Thanks a lot! I had a look in this chapter, but somehow I was not able to handle the outputs and transactions....

Now I managed it!

MATCH path= (roottx:tx {txid:'266160772'})<-[:in|:out*1..6]-(tx:tx)
RETURN path

basically delivered the same result as my query in the original post above!

Thanks for your help!

1 Like

Nice, I'm happy to hear this :slight_smile:

On additional idea came to my mind, which I am trying to find a query:

It would be great to see the interactions of transactions. Means, I would like to see only transactions in my graph, without he outputs of the transactions (see picture above).

I tried to formulate the query like this:

MATCH  (roottx:tx {txid:'266160772'})<-[:in|:out*1..6]-(tx:tx)
RETURN tx

But then the relationsships are gone:

Is it possible, to replace the OUTPUT nodes, but still create a connection between the TRANSACTION nodes?

Thanks for your help!

Your orange nodes are not directly connected to each other right? There are blue node between them?

Yes.

Transactions (orange nodes) are connected with outputs (blue nodes) via IN or OUT relation.

So, I would need to replace the IN-OUTPUT-OUT part between the transactions with a kind of dummy relation (only in the query result).

You will have to create this "dummy" relation if you want to display it :confused:

You can simply return only the nodes that you want:

MATCH path= (roottx:tx {txid:'266160772'})<-[:in|:out*1..6]-(tx:tx)
RETURN tx