cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

what would be the search query if I want to search a node and it should return me the main node?

Let me explain it very clearly.

I have a graph structure in neo4j which contains head nodes, tail nodes and relationship.

Apart from head, tail nodes, i have a main node which connects all head nodes.

so it look like this..

Jupyter123_0-1672116621521.png

As you can see in above picture, handbook is the main node and whichever node is connected with handbook is the head node and whichever node is connnected with head node is tail node.

So i want to write a query to search some keyword like 'safe design' | 'safety in design' and it should return me the main node (handbook) 

what would be the search query for this?

 

1 ACCEPTED SOLUTION

Try this:

match(entity:Entity{name: "constructible"})
match(entity)<-[*0..1]-(:Entity)-[:HAS_MAIN]->(main)
return main 
limit 1

View solution in original post

12 REPLIES 12

Where are the keywords stored?  Do you have a working  query  that search keywords?  What kind of string search do you  want to use, something like “keyword contains search criteria”, do you want to use the full-text searching feature? 

hi, @glilienfield 

keyword = i meant the node name, 

assume that there is a node called 'safety design', i want to search that node name safety design via cypher query language and it should return me the main node (which is handbook)

similarly if im searching for other node like 'risks' then it should return me the main node because main node is linked to all other head nodes

Your data model from your other post is:

(tail:Entity)<-[]-(head:Entity)-[:HAS_MAIN]->(main:Entity)

If your search by name results in a tail node, then the match pattern to get the main node is the following:

(tail)<-[]-(:Entity)-[:HAS_MAIN]->(main:Entity)

if your search results in a head node, then your match pattern to find the main nodes:

(head)-[:HAS_MAIN]->(main:Entity)

the only difference if the first node and relationship, so you can use a variable length relation of 0..1 to account for both cases. The following should work: 

match(entity:Entity{name:$name})

match(entity)<-[*0..1]-(:Entity)-[:HAS_MAIN]->(main)

return main 

limit 1

I added the ‘limit 1’ because it is possible from your data that there could be multiple paths back to the single ‘main’ node. 

@glilienfield hi im getting this error

Jupyter123_0-1672200154823.png

 

Hi @glilienfield , Happy New year.
I have typed your cypher query but I'm  getting this error, could u pls check what mistake i did? 

Jupyter123_1-1672630836134.png

I have a head node called 'risk', i want to find the main node relation from the head node so i typed this query

match(entity:Entity{risk:$risk})

match(entity)<-[*0..1]-(:Entity)-[:HAS_MAIN]->(main)

return main 

limit 1

but im getting error, pls have a look 

 

You have a parameter in the query named ‘risks’. It is referenced by ‘$risks’   It’s value needs to be defined, which you can do with

:param risks => “high”

where you would substitute ‘high’ with your value for risks.  This value will be passed to the query when executed. 

sorry @glilienfield i did not get you?

 

Variables prefixed with ‘$’ are considered parameters. Their value needs to be defined, and the value will be substituted during execution. Read below reference on parameters in neo4j desktop. 

https://neo4j.com/docs/browser-manual/current/operations/query-parameters/

you can also replace the parameter with the value if you want to avoid using a parameter. Assuming the value of risk for the head node is “high”, the query would look like this. Replace “high” with your actual value. 

 

match(entity:Entity{risk:”high”})

match(entity)<-[*0..1]-(:Entity)-[:HAS_MAIN]->(main)

return main 

limit 1

@glilienfield 

check this out.
'document' is the main node name

i have a node called maintainable

if i search for maintainable then it should return me document main node but when i run this code im getting, 

Jupyter123_1-1672664221145.png

 

Jupyter123_0-1672664130957.png

 

@glilienfield the query seems complicated to me.

Can you send me the query for searching using full search index of my previous scenario?

The query should do what you want. It needs to deal with the fact traversing to the main node may require going through one or two nodes, either a head node or both a tail and head node. 

what is your previous scenario? 

Try this:

match(entity:Entity{name: "constructible"})
match(entity)<-[*0..1]-(:Entity)-[:HAS_MAIN]->(main)
return main 
limit 1