Recommendation scenarios
When you shop on the Taobao mobile app, the Guess You Like page recommends items that you might be interested in. When you watch movies on Youku, the page also recommends a list of movies that you might like. Personalized recommendations are widely used to help enterprises discover potential user needs, increase conversion rates, and improve the user experience. Personalized recommendations are based on datasets such as user behavior history and the properties of items such as products and movies. The data points are interconnected. Recommendation policies leverage these complex relationships. Therefore, a graph database is well-suited for storing this data.
Modeling
This topic uses a personalized book recommendation scenario as an example to demonstrate how to build a personalized recommendation application by using a graph database.
Graph Database (GDB) supports the property graph model. A property graph stores data as basic elements: vertices, edges, and properties. Each vertex or edge has a label that represents its type. In a personalized book recommendation scenario, you can define the following basic elements:
-
Vertex: Book, Publisher, Author, and Reader.
-
Edge: Publish, Write, and Score.
-
Property: name, type, age, and year.

Code implementation
Suppose that you want to recommend books to a reader whose username is Xiao Ming. An intuitive approach is to find other readers who have rated the same books as Xiao Ming, and then recommend the other books that those readers have rated to Xiao Ming.
GDB supports the Gremlin query language. You can use the following Gremlin statements to implement the recommendation:
-
Find Xiao Ming.
g.V().hasLabel('Reader').has('name','Xiao Ming') -
Find the books that Xiao Ming rated.
g.V().hasLabel('Reader').has('name','Xiao Ming').as('myself').out('Score') -
Find other readers who rated the same books as Xiao Ming.
g.V().hasLabel('Reader').has('name','Xiao Ming').as('myself').out('Score').aggregate('scored_books').in('Score').where(neq('myself')) -
Find books that these readers rated but Xiao Ming has not rated.
g.V().hasLabel('Reader').has('name','Xiao Ming').as('myself').out('Score').aggregate('scored_books').in('Score').where(neq('myself')).out('Score').where(not(within('scored_books')))