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.

Hi, I want to limit the result of node, after the phrase of 'where' in neo4j

Hello neo4j,
Hope you guys having a nice day today.

By the way, I made some queries for showing the place is associated with some city.

So, I want to limit the result place of each city. for example,
if location A is related with 10 City and location B is related with 10 cities, I just want to reduce the number of each result as 6. ( location A is related with 6 places, and location B is related with 6 places)

For my business logic, I use where any() clause for showing variety Possibility of results.
here is my code.

MATCH (place)<-[:IS_Location]-(location:Location)
WHERE any ( 
	filters IN ['ALL', 'B', 'C', 'D', 'E'] 
	WHERE (place)<-[:IS_Location]-(location{name : filters})
    with distinct location // I add this statement
    limit 10 // and this statement
)

I add two statements, but it doesn't work.
I really need some help and also any comments would be appriciated.

Thanks!

4 REPLIES 4

Hello @gjtnwjd40

You can use a subquery:

MATCH (place)
CALL {
    WITH place
    MATCH (place)<-[:IS_Location]-(location:Location)
    WHERE any(filter IN ['ALL', 'B', 'C', 'D', 'E'] WHERE location.name = filter)
    RETURN DISTINCT location, place
    LIMIT 10
}
RETURN location, place

Regards,
Cobra

Hi, @Cobra
Thank you for your help.
But I got an error like below.

Invalid input '{': expected whitespace, comment, namespace of a procedure or a procedure name (line 8, column 6 (offset: 227))
"CALL {"
      ^

So I search for this error,
And I found that it occurs because of the neo4j version.

I had a neo4j 3.5.x version, but if I would like to use CALL {}, I have to use neo4j version 4.1.

But I can't upgrade the neo4j version right now, because there is a code that communicated with python. ( yeah, it's python driver code )

so, It would be appreciated if you suggest a solution when using a version of 3.5.x in neo4j.

Warmest Regards,

Sujeong 🙂

Thanks for your reply!

I'll try and adapt that in my code.

Regards,
sujeong

Hi @gjtnwjd40

Sure, I'm sorry, since you didn't specify the neo4j version, I thought you were on 4.0

MATCH (place)
WITH id(place) AS idp
CALL apoc.cypher.run('
    MATCH (place)<-[:IS_Location]-(location:Location)
    WHERE id(place) = $idp
    AND any(filter IN $filters WHERE location.name = filter)
    RETURN DISTINCT location, place
    LIMIT 10
', {idp:idp, filters:['ALL', 'B', 'C', 'D', 'E']}) YIELD value
RETURN value.location AS location, value.place AS place

Documentation:

Regards,
Cobra