Rank API

4.1 Rank API overview

Not only the Graph iteration (traverser) method, HugeGraph-Server also provide Rank API for recommendation purpose. You can use it to recommend some vertexes much closer to a vertex.

4.2 Details of Rank API

4.2.1 Personal Rank API

A typical scenario for Personal Rank algorithm is in recommendation application. According to the out edges of a vertex, recommend some other vertices that having the same or similar edges.

Here is a use case: According to someone’s reading habit or reading history, we can recommend some books he may be interested or some book pal.

For Example:

  1. Suppose we have a vertex, Person type, and named tom.He like 5 books a,b,c,d,e. If we want to recommend some book pal and books for tom, an easier idea is let’s check whoever also liked these books (common hobby based).
  2. Now, we need someone else, like neo, he like three books b,d,f. And Jay, he like 4 books c,d,e,g, and Lee, he also like 4 books a,d,e,f.
  3. For we don’t need to recommend books tom already read, the recommend-list should only contain the books Tom’s book pal already read but tom haven’t read yet. Such as book “f” and “g”, and with priority f > g.
  4. Now, we recompute Tom’s personal rank value, we will get a sorted TopN book pal or book recommend-list. (Choose OTHER_LABEL,for Only Book purpose)
4.2.1.0 Data Preparation

The case above is simple. Here we also provide a public test dataset MovieLens for use case. You should download the dataset. The load it into HugeGraph with HugeGraph-Loader. To make it simple, we ignore all properties data of user and move. only field id is enough. we also ignore the value of edge rating.

The metadata for input file and mapping file as follows:

  1. ////////////////////////////////////////////////////////////
  2. // UserID::Gender::Age::Occupation::Zip-code
  3. // MovieID::Title::Genres
  4. // UserID::MovieID::Rating::Timestamp
  5. ////////////////////////////////////////////////////////////
  6. // Define schema
  7. schema.propertyKey("id").asInt().ifNotExist().create();
  8. schema.propertyKey("rate").asInt().ifNotExist().create();
  9. schema.vertexLabel("user")
  10. .properties("id")
  11. .primaryKeys("id")
  12. .ifNotExist()
  13. .create();
  14. schema.vertexLabel("movie")
  15. .properties("id")
  16. .primaryKeys("id")
  17. .ifNotExist()
  18. .create();
  19. schema.edgeLabel("rating")
  20. .sourceLabel("user")
  21. .targetLabel("movie")
  22. .properties("rate")
  23. .ifNotExist()
  24. .create();
  1. {
  2. "vertices": [
  3. {
  4. "label": "user",
  5. "input": {
  6. "type": "file",
  7. "path": "users.dat",
  8. "format": "TEXT",
  9. "delimiter": "::",
  10. "header": ["UserID", "Gender", "Age", "Occupation", "Zip-code"]
  11. },
  12. "ignored": ["Gender", "Age", "Occupation", "Zip-code"],
  13. "mapping": {
  14. "UserID": "id"
  15. }
  16. },
  17. {
  18. "label": "movie",
  19. "input": {
  20. "type": "file",
  21. "path": "movies.dat",
  22. "format": "TEXT",
  23. "delimiter": "::",
  24. "header": ["MovieID", "Title", "Genres"]
  25. },
  26. "ignored": ["Title", "Genres"],
  27. "mapping": {
  28. "MovieID": "id"
  29. }
  30. }
  31. ],
  32. "edges": [
  33. {
  34. "label": "rating",
  35. "source": ["UserID"],
  36. "target": ["MovieID"],
  37. "input": {
  38. "type": "file",
  39. "path": "ratings.dat",
  40. "format": "TEXT",
  41. "delimiter": "::",
  42. "header": ["UserID", "MovieID", "Rating", "Timestamp"]
  43. },
  44. "ignored": ["Timestamp"],
  45. "mapping": {
  46. "UserID": "id",
  47. "MovieID": "id",
  48. "Rating": "rate"
  49. }
  50. }
  51. ]
  52. }

Note: modify the input.path to your local path.

4.2.1.1 Function Introduction

suitable for bipartite graph, will return all vertex or a list of its correlation which related to all source vertex.

Bipartite Graph is a special model in Graph Theory, as well as a special flow in network. The strongest feature is, it split all vertex in graph into two sets. The vertex in the set is not connected. However,the vertex in two sets may connect with each other.

Suppose we have one bipartite graph based on user and things. A random walk based PersonalRank algorithm should be likes this:

  1. Choose a user u as start vertex, let’s set the initial weight to be 1.0 . Go from Vu with probability alpha to a neighbor vertex, and (1-alpha) to stay.
  2. If we decide to go outside, we would like to choose an edge, such as rating, to find a common judge.
    1. Then choose the neighbors of current vertex randomly with uniform distribution, and reset the weights with uniform distribution.
    2. Compensate the source vertex’s weight with (1 - alpha)
    3. Repeat step 2;
  3. Convergence after reaching a certain number of steps or precision, then we got a recommend-list.
Params

Required:

  • source: the id of source vertex
  • label: edge label go from the source vertex, should connect two different type of vertex

Optional:

  • alpha: the probability of going out for one vertex in each iteration,similar to the alpha of PageRank,required, value range is (0, 1], default 0.85.
  • max_degree: in query process, the max iteration number of adjacency edge for a vertex, default 10000
  • max_depth: iteration number,range [2, 50], default 5
  • with_label:result filter,default BOTH_LABEL,optional list as follows:
    • SAME_LABEL:Only keep vertex which has the same type as source vertex
    • OTHER_LABEL:Only keep vertex which has different type as source vertex (the another part in bipartite graph)
    • BOTH_LABEL:Keep both type vertex
  • limit: max return vertex number,default 100
  • max_diff: accuracy for convergence, default 0.0001 (will implement soon)
  • sorted: whether sort the result by rank or not, true for descending sort, false for none, default true
4.2.1.2 Usage
Method & Url
  1. POST http://localhost:8080/graphs/hugegraph/traversers/personalrank
Request Body
  1. {
  2. "source": "1:1",
  3. "label": "rating",
  4. "alpha": 0.6,
  5. "max_depth": 15,
  6. "with_label": "OTHER_LABEL",
  7. "sorted": true,
  8. "limit": 10
  9. }
Response Status
  1. 200
Response Body
  1. {
  2. "2:2858": 0.0005014026017816927,
  3. "2:1196": 0.0004336708357653617,
  4. "2:1210": 0.0004128083140214213,
  5. "2:593": 0.00038117341069881513,
  6. "2:480": 0.00037005373269728036,
  7. "2:1198": 0.000366641614652057,
  8. "2:2396": 0.0003622362410538888,
  9. "2:2571": 0.0003593312457300953,
  10. "2:589": 0.00035922123055598566,
  11. "2:110": 0.0003466135844390885
  12. }
4.2.1.3 Suitable Scenario

In a bipartite graph build by two different type of vertex, recommend other most related vertex to one vertex. for example:

  • Reading recommendation: find out the books should be recommended to someone first, It is also possible to recommend book pal with the highest common preferences at the same time (just like: WeChat “your friend also read xx “ function)
  • Social recommendation: find out other Poster who interested in same topics, or other News/Messages you may be interested with (Such as : “Hot News” function in Weibo)
  • Commodity recommendation: according to someone’s shopping habit,find out a commodity list should recommend first, some online salesman may also be good (Such as : “You May Like” function in TaoBao)

4.2.2 Neighbor Rank API

4.2.2.0 Data Preparation
  1. public class Loader {
  2. public static void main(String[] args) {
  3. HugeClient client = new HugeClient("http://127.0.0.1:8080", "hugegraph");
  4. SchemaManager schema = client.schema();
  5. schema.propertyKey("name").asText().ifNotExist().create();
  6. schema.vertexLabel("person")
  7. .properties("name")
  8. .useCustomizeStringId()
  9. .ifNotExist()
  10. .create();
  11. schema.vertexLabel("movie")
  12. .properties("name")
  13. .useCustomizeStringId()
  14. .ifNotExist()
  15. .create();
  16. schema.edgeLabel("follow")
  17. .sourceLabel("person")
  18. .targetLabel("person")
  19. .ifNotExist()
  20. .create();
  21. schema.edgeLabel("like")
  22. .sourceLabel("person")
  23. .targetLabel("movie")
  24. .ifNotExist()
  25. .create();
  26. schema.edgeLabel("directedBy")
  27. .sourceLabel("movie")
  28. .targetLabel("person")
  29. .ifNotExist()
  30. .create();
  31. GraphManager graph = client.graph();
  32. Vertex O = graph.addVertex(T.label, "person", T.id, "O", "name", "O");
  33. Vertex A = graph.addVertex(T.label, "person", T.id, "A", "name", "A");
  34. Vertex B = graph.addVertex(T.label, "person", T.id, "B", "name", "B");
  35. Vertex C = graph.addVertex(T.label, "person", T.id, "C", "name", "C");
  36. Vertex D = graph.addVertex(T.label, "person", T.id, "D", "name", "D");
  37. Vertex E = graph.addVertex(T.label, "movie", T.id, "E", "name", "E");
  38. Vertex F = graph.addVertex(T.label, "movie", T.id, "F", "name", "F");
  39. Vertex G = graph.addVertex(T.label, "movie", T.id, "G", "name", "G");
  40. Vertex H = graph.addVertex(T.label, "movie", T.id, "H", "name", "H");
  41. Vertex I = graph.addVertex(T.label, "movie", T.id, "I", "name", "I");
  42. Vertex J = graph.addVertex(T.label, "movie", T.id, "J", "name", "J");
  43. Vertex K = graph.addVertex(T.label, "person", T.id, "K", "name", "K");
  44. Vertex L = graph.addVertex(T.label, "person", T.id, "L", "name", "L");
  45. Vertex M = graph.addVertex(T.label, "person", T.id, "M", "name", "M");
  46. O.addEdge("follow", A);
  47. O.addEdge("follow", B);
  48. O.addEdge("follow", C);
  49. D.addEdge("follow", O);
  50. A.addEdge("follow", B);
  51. A.addEdge("like", E);
  52. A.addEdge("like", F);
  53. B.addEdge("like", G);
  54. B.addEdge("like", H);
  55. C.addEdge("like", I);
  56. C.addEdge("like", J);
  57. E.addEdge("directedBy", K);
  58. F.addEdge("directedBy", B);
  59. F.addEdge("directedBy", L);
  60. G.addEdge("directedBy", M);
  61. }
  62. }
4.2.2.1 Function Introduction

In a general graph structure,find the first N vertices of each layer with the highest correlation with a given starting point and their relevance.

In graph words: to go out from the starting point, get the probability of going to each vertex of each layer.

Params
  • source: id of source vertex,required
  • alpha:the probability of going out for one vertex in each iteration,similar to the alpha of PageRank,required, value range is (0, 1]
  • steps: a path rule for source vertex visited,it’s a list of Step,each Step map to a layout in result,required.The structure of each Step as follows:
    • direction:the direction of edge(OUT, IN, BOTH), BOTH for default.
    • labels:a list of edge types, will union all edge types
    • max_degree:in query process, the max iteration number of adjacency edge for a vertex, default 10000 (Note: before v0.12 step only support degree as parameter name, from v0.12, use max_degree, compatible with degree)
    • top: retains only the top N results with the highest weight in each layer of the results, default 100, max 1000
  • capacity: the maximum number of vertexes visited during the traversal, optional, default 10000000
4.2.2.2 Usage
Method & Url
  1. POST http://localhost:8080/graphs/hugegraph/traversers/neighborrank
Request Body
  1. {
  2. "source":"O",
  3. "steps":[
  4. {
  5. "direction":"OUT",
  6. "labels":[
  7. "follow"
  8. ],
  9. "max_degree":-1,
  10. "top":100
  11. },
  12. {
  13. "direction":"OUT",
  14. "labels":[
  15. "follow",
  16. "like"
  17. ],
  18. "max_degree":-1,
  19. "top":100
  20. },
  21. {
  22. "direction":"OUT",
  23. "labels":[
  24. "directedBy"
  25. ],
  26. "max_degree":-1,
  27. "top":100
  28. }
  29. ],
  30. "alpha":0.9,
  31. "capacity":-1
  32. }
Response Status
  1. 200
Response Body
  1. {
  2. "ranks": [
  3. {
  4. "O": 1
  5. },
  6. {
  7. "B": 0.4305,
  8. "A": 0.3,
  9. "C": 0.3
  10. },
  11. {
  12. "G": 0.17550000000000002,
  13. "H": 0.17550000000000002,
  14. "I": 0.135,
  15. "J": 0.135,
  16. "E": 0.09000000000000001,
  17. "F": 0.09000000000000001
  18. },
  19. {
  20. "M": 0.15795,
  21. "K": 0.08100000000000002,
  22. "L": 0.04050000000000001
  23. }
  24. ]
  25. }
4.2.2.3 Suitable Scenario

Find the vertices in different layers for a given start point that should be most recommended

  • For example, in the four-layered structure of the audience, friends, movies, and directors, according to the movies that a certain audience’s friends like, recommend movies for that audience, or recommend directors for those movies based on who made them.

Last modified September 15, 2022: add rank api & fix typo (06499b02)