cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! Site migration is underway. Phase 2: migrate recent content

Spring Data Neo4j mapping Yens algorithm result

xenoxi
Node

Hi,

I want to execute Yen's algorithm using Query annotation in Spring Data. It works but I have problem with mapping the paths:

in option A: I'm mapping to List<NodeName>, it returns all of nodes in one list without separating paths

in option B: I'm using mapping to List<List<NodeName>> and it separates path bur returns only first nodes from all paths as list.

I want to map algorithm result to List<List<NodeName>> like:
[

[a,b,c],

[f,b],

[s,g,t,h],

] etc

Could you please help me how to do it? I was trying using  nodes, collect function and also with another class containing only list of NodeName but nothing worked as expected.

Thank you

4 REPLIES 4

glilienfield
Ninja
Ninja

What does your Spring Data entities and repositories look like?

xenoxi
Node

Hi, code snippets:

// base class
@Node
public class City {
    @Id
    @GeneratedValue
    private Long id;

    @NonNull
    private String name;
    @NonNull
    private String code;
}
// neo4j repository, example returning separated list, always with 1 node
public interface CityRepository extends Neo4jRepository<City, Long> {
@Query("MATCH (source:City {code: 'BCC'}), (target:City {code: 'LAX'})
CALL gds.shortestPath.yens.stream('projection', {
    sourceNode: source,
    targetNode: target,
    k: 20,
    relationshipWeightProperty: 'distance'
})
YIELD index, sourceNode, targetNode, nodeIds, path, totalCost, costs
RETURN nodes(path) as path")
List<List<City>> findPaths();
}

On the single path there are two types of node like:

city->village->city->village and ideally I want to parse only city nodes, I was trying list comprehension but it also does not work

RETURN [x in nodes(path) WHERE 'City' IN LABELS(x)]

 

xenoxi
Node

Problem solved using neo4j client and custom mapping

Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

All the sessions of the conference are now available online