0

I'm trying to use graphAware's neo4j recommendation engine (https://github.com/graphaware/neo4j-reco). I'm new to a lot of the technologies involved, but as I understand after I have my recommendation engine built I run the following to get my recommendations:

List<Recommendation<Node>> recoForVince = recommendationEngine.recommend(getPersonByName("Vince"), new SimpleConfig(2));

The examples getPersonByName is this:

private Node getPersonByName(String name) {
    return IterableUtils.getSingle(getDatabase().findNodes(DynamicLabel.label("Person"), "name", name));
}

The problem is as I understand it that'll only work if your neo4j server is embedded. We are looking at using spring for our mvc setup and with that we'll have something like:

We will be getting our data with something like:

@Repository
public interface PersonRepository extends GraphRepository<Person> {
    @Query("MATCH (p:Person) WHERE m.name = {name} RETURN p")
    Person findByTitleContaining(@Param("name") String name);
}

or maybe returned as a Hashmap. My issue is I'm unclear how I can convert either of these results into a Node object that would be usable by the Graphaware framework or how I should go about getting a Node object.

Ryan C
  • 1,003
  • 1
  • 14
  • 26

1 Answers1

3

If you run Neo4j in embedded mode, you'll have anyway access to the GraphDatabaseService.

In your Person entity you have access to the GraphId property which is the node internal id.

So you can, in for example your custom RecommendationService, retrieve the Node object for this id :

private final GraphDatabaseService database;

private final MyRecommendationEngine engine;

@Autowired
public RecommendationService(GraphDatabaseService database, MyRecommendationEngine engine) {
  this.database = database;
  this.engine = engine;

public List<Recommendation<Node>> processRecommendation(Person person) {
  Node node = database.getNodeById(person.getId());

  return engine.recommend(node);
}

Please note that this will return Node objects, if you want to convert them to Person entities you'll need to handle the hydration yourself.

If you run Neo4j in ServerMode and SDN4 is at your application level then, the best would be to make http request calls to the endpoints provided by the Neo4j-Reco plugin. You can pass then only the name of the Person or his id.

You can see an example here : https://github.com/graphaware/recommendations-meetup

Christophe Willemsen
  • 19,399
  • 2
  • 29
  • 36
  • What is the best approach if I'm running in server mode? I've only seen the node object referenced in embedded mode, is it only for embedded mode? – Ryan C Nov 07 '15 at 17:50
  • If you use server mode, you'll need to make http requests to the API endpoints of the neo4j-reco plugin. Neo4j-Reco is a server plugin and thus run on the server side, not on the application side like SDN4. I edited my answer – Christophe Willemsen Nov 07 '15 at 18:03
  • Thanks Christophe. That's what I was missing, I was thinking reco could work on the application side, but at a complete loss of how that would work. The example and your explanation cleared up that you build the reco and include it as a plugin for the server. – Ryan C Nov 08 '15 at 06:56