skip to Main Content

I have a graph that represents how Wikipedia articles are related to one another following the "Getting to Philosophy" phenomena, which is based on clicking on the very first link in the main text of an article, and repeating this processes for subsequent articles. Usually, this process ends when we get to the Philosophy article. All the vertices in this graph are labelled "Article" and the relationships between them – the edges – are labelled as "RELATED_TO".

I wanted to get the length of the path between two different vertices to see how distant one article is to another. For example, I wanted to see how far the "Tulpa" article is from "Philosophy" by counting the edges between them. One thing I thought it would work is to query using a VLE (Variable Length Edge). At the Variable Length Edge section in the AGE Manual, it is stated that " When the connection between two vertices is of variable length, the list of edges that form the connection can be returned using the following connection.". But when I query with it, it returns 1, which is not the correct length between these two vertices.

SELECT * FROM cypher('Wikipedia', $$
    MATCH p =(a)-[:RELATED_TO*]->(b)
    WHERE a.name = 'Tulpa' AND b.name = 'Philosophy'
    RETURN count(p)
$$) as (edge_count agtype);

 edge_count 
------------
 1
(1 row)

What is the correct query to search for the vertices like this?

2

Answers


  1. I think you are searching for the function length(). This function returns the length of a path. Based on your example and the Apache AGE documentation it would be:

    SELECT * FROM cypher('Wikipedia', $$
        MATCH p =(a)-[*]->(b)
        WHERE a.name = 'Tulpa' AND b.name = 'Philosophy'
        RETURN length(p)
    $$) as (edge_count agtype);
    

    With [*] representing the variable path between a and b. The function will return the length of all paths between ‘Tulpa’ and ‘Philosophy’.

    Login or Signup to reply.
  2. If count(p) is returning 1, it indicates that your query is returning only one path or one row of data.

    These are two possible queries to determine the number of edges linking ‘Tulpa’ and ‘Philosophy’:

    length(path):

    SELECT * FROM cypher('Wikipedia', $$
        MATCH p = (a)-[:RELATED_TO*]->(b)
        WHERE a.name = 'Tulpa' AND b.name = 'Philosophy'
        RETURN length(p)
    $$) as (edge_count agtype);
    

    size(list):

    SELECT * FROM cypher('Wikipedia', $$
        MATCH (a)-[e:RELATED_TO*]->(b)
        WHERE a.name = 'Tulpa' AND b.name = 'Philosophy'
        RETURN size(e)
    $$) as (edge_count agtype);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search