I need help debugging this cypher query

I am trying to complete a guide about using BAML with Langchain and neo4j to create a KnowledgeGraph. In the guide at the end we try to create communities and relationships with the following query

MATCH (e:`__Entity__`)
UNWIND range(0, size(e.communities) - 1 , 1) AS index
// Create the link from an entity to its lowest-level community
CALL {
  WITH e, index
  WHERE index = 0
  MERGE (c:`__Community__` {id: toString(index) + '-' + toString(e.communities[index])})
  ON CREATE SET c.level = index
  MERGE (e)-[:IN_COMMUNITY]->(c)
  RETURN count(*) AS count_0
}
// Create links between hierarchical community levels
CALL {
  WITH e, index
  WHERE index > 0
  MERGE (current:`__Community__` {id: toString(index) + '-' + toString(e.communities[index])})
  ON CREATE SET current.level = index
  MERGE (previous:`__Community__` {id: toString(index - 1) + '-' + toString(e.communities[index - 1])})
  ON CREATE SET previous.level = index - 1
  MERGE (previous)-[:IN_COMMUNITY]->(current)
  RETURN count(*) AS count_1
}
RETURN count(*)

It shows a syntax error:

Importing WITH should consist only of simple references to outside variables. WHERE is not allowed. (line 5, column 7 (offset: 166))
"      WITH e, index"
       ^

Could someone give me a hint on how to fix this?

MATCH (e:`__Entity__`)
UNWIND range(0, size(e.communities) - 1, 1) AS index

// 
WITH e, index // <-- you are missing this

// Create the link from an entity to its lowest-level community
CALL {
  WITH e, index
  WHERE index = 0
  MERGE (c:`__Community__` {id: toString(index) + '-' + toString(e.communities[index])})
    ON CREATE SET c.level = index
  MERGE (e)-[:IN_COMMUNITY]->(c)
  RETURN count(*) AS count_0
}

WITH e, index // <-- and this

// Create links between hierarchical community levels
CALL {
  WITH e, index
  WHERE index > 0
  MERGE (current:`__Community__` {id: toString(index) + '-' + toString(e.communities[index])})
    ON CREATE SET current.level = index
  MERGE (previous:`__Community__` {id: toString(index - 1) + '-' + toString(e.communities[index - 1])})
    ON CREATE SET previous.level = index - 1
  MERGE (previous)-[:IN_COMMUNITY]->(current)
  RETURN count(*) AS count_1
}

RETURN count(*) AS total

Are you sure? When I plug this in to my neo4j browser it still gives pretty much the same error.

Try this one:

MATCH (e:`__Entity__`)
WITH e, range(0, size(e.communities) - 1, 1) AS LargeIndex
UNWIND LargeIndex as index
WITH index, e
CALL (e, index) {
  WITH *
  WHERE index = 0
  MERGE (c:`__Community__` {id: toString(index) + '-' + toString(e.communities[index])})
    ON CREATE SET c.level = index
  MERGE (e)-[:IN_COMMUNITY]->(c)
  RETURN count(*) AS count_0
}
WITH index, e
// Create links between hierarchical community levels
CALL (e, index) {
  WITH *
  WHERE index > 0
  MERGE (current:`__Community__` {id: toString(index) + '-' + toString(e.communities[index])})
    ON CREATE SET current.level = index
  MERGE (previous:`__Community__` {id: toString(index - 1) + '-' + toString(e.communities[index - 1])})
    ON CREATE SET previous.level = index - 1
  MERGE (previous)-[:IN_COMMUNITY]->(current)
  RETURN count(*) AS count_1
}


RETURN count(*) AS total

It does work syntactically, but I do have a few too many communities now.

Yeah without the data and the objective, I can get only to the syntactic :frowning:

Well what i actually wanted to do is run

gds.leiden.write(
    G,
    writeProperty="communities",
    includeIntermediateCommunities=True,
    relationshipWeightProperty="weight",
  )

and then create a community node per leiden community and generate relationships to all nodes in that community, as long as they are in the group _Entitiy__.

I think the difficulty lies in connecting hierarchical communities. That could also be the reason Im seeing strings of communities, because they are of different hierarchical level. Checked again and thats the reason for the chains of communities.

Your first error is due to your import with a “with” followed by a “where”. The error message states the import must be simple. A work around is to use two “with” statements, one for the simple import and the second for the filtering with “where.”

for example:

Call {
With a
With a
Where a>0
#subquery code
}