Muhammad Yaseen Khan
Muhammad Yaseen Khan

Reputation: 809

neo4j - How to bring all nodes of one type in a result of relationship query?

I am sorry for the stupid question. I have two types of nodes in neo4j database, namely Recipes and Meal_Type. I am running a cypher query in neo4j that results all relationship between the two types of nodes. The query is not that special, it is the default query that returns relationship with a limit of 200 nodes.

MATCH ()-[r]->() RETURN r LIMIT 200

It is running fine. But I need, at least, all Meal_Types nodes in result regardless the rest of result. Right now it is returning 3 (sometimes 4,5 on re-running query) out of 11 Meal_Types.

Upvotes: 1

Views: 1070

Answers (2)

Dave Bennett
Dave Bennett

Reputation: 11216

I think you should fetch all of the Meal_Type nodes first and then with that result fetch a set of Recipe nodes that correspond to it.

Here is an example of what I am talking about. Fetch all of the different meal types, unless of course you have some specific ones you are interested in. Then with those meal types return a sampling of the corresponding set of recipes (200 ~= 19 * 11).

// match meal types
MATCH (mt:Meal_Type)
WITH mt
// find a sampling of the the corresponding recipes.
MATCH (mt)<-[OF_TYPE]-(r:Recipe)
RETURN mt, collect(r)[0..18] AS recipe_sample

Upvotes: 3

Frank Pavageau
Frank Pavageau

Reputation: 11715

Really? I answered that yesterday with your previous question, it's just a variation.

This should do the trick, sorting the relationships by node label:

MATCH (n)-[r]-()
RETURN r
ORDER BY head(labels(n))

Upvotes: 0

Related Questions