Apache AGE - Changing Labels of Nodes and Edges - postgresql

Is there any possible way to change the current label of a vertex or an edge? For example, if I create a graph to store movies and series and then create the following vertex:
SELECT * FROM cypher ('visual_arts', $$
CREATE (v:Movie {title: "The Last of Us", episodes: 4, seasons: 1, main_actors: ["Pedro Pascal", "Bella Ramsey"]})
RETURN v $$) as (v agtype);
And then I want to correct this vertex by changing the label to "Series" instead of "Movie". How can I do that?

I believe, this feature is currently in development right now. See https://github.com/apache/age/issues/464

In current version of AGE, we cannot update label of any vertex or edge.
To do so, the best way is to introduce a new property UpdatedLabel and set it to Series.
You can do so by:
SELECT * FROM cypher('visual_arts', $$
MATCH (v:Movie)
SET v.UpdatedLabel = "Series" RETURN v
$$) as (v agtype);
Now you can retrieve the Series by using:
SELECT * FROM cypher('visual_arts', $$
MATCH (v:Movie)
where v.UpdatedLabel = "Series" RETURN v
$$) as (v agtype);

Related

Can't see the first property of a node on Age Viewer

I have created a Book node called Book as follow:
Select * FROM cypher('graph_two',$$ CREATE (n:Book {title:'Something'}) $$) as (a agtype);
And a person Node as follows:
Select * FROM cypher('graph_two',$$ CREATE (n:User {Name:'Something'}) $$) as (a agtype);
And an edge between them as follows:
Select * FROM cypher('graph_two',$$ MATCH (a:User), (b:Book) CREATE (a)-[r:READS]->(b) Return r $$) as (r agtype);
And when I visualize this in age-viewer I get:
What am I doing wrong here? I want the User name and the Book title to be displayed rather than the OId.
If you click on any Node Label (your's are "Book" and "User") it will appear a bar on the bottom of the current graph scene. There you can set what the vertex or edge is currently displaying, the size of the vertex and also the color of them. This also works with the edges.

OrientDB query to receive last vertex before a given date

Let's say I have the following list of vertices (connected by edges) in the orient database:
[t=1] --> [t=2] --> [t=3] --> [t=4] --> [t=5] --> [t=6] --> [t=7]
Each vertex has a timestamp t. I now want to receive the last vertex before a given date. Example: give me the last vertex before t=5, which is t=4.
Currently I'am using the following query to do this:
SELECT FROM ANYVERTEX WHERE t < 5 ORDER BY t DESC LIMIT 1
This is working fine when having up to let's say 1000 elements but the performance of that query drops with the number of elements inserted in the list. I already tried using an index, which improved the overall performance, but the problem, that the performance drops with the amount of elements still persists.
When building queries, always try to use the information you have about the relationship in your query to improve performance. In this case you don't need the sort (which is an expensive operation) because you know that the vertex you need has an incoming edge to the vertex, you can simply use that information in your query.
For example, let's say I have the following setup:
CREATE CLASS T EXTENDS V
CREATE VERTEX T SET t = 1
CREATE VERTEX T SET t = 2
CREATE VERTEX T SET t = 3
CREATE VERTEX T SET t = 4
CREATE VERTEX T SET t = 5
CREATE CLASS link EXTENDS E
CREATE EDGE link FROM (SELECT * FROM T WHERE t = 1) TO (SELECT * FROM T WHERE t = 2)
CREATE EDGE link FROM (SELECT * FROM T WHERE t = 2) TO (SELECT * FROM T WHERE t = 3)
CREATE EDGE link FROM (SELECT * FROM T WHERE t = 3) TO (SELECT * FROM T WHERE t = 4)
CREATE EDGE link FROM (SELECT * FROM T WHERE t = 4) TO (SELECT * FROM T WHERE t = 5)
Then I can select the vertex before any T as such:
SELECT expand(in('link')) FROM T WHERE t = 2
This query does the following:
Select the vertex from T where t=2
From that vertex, traverse the incoming edge(s) of type link
expand() the vertex from which that edge comes from to get all of its information
The result is exactly what you want:
This should give better performance (especially if you add an index on the attribute t of the vertices) because you are using all the information you know in advance about the relationship = the node you need has an edge to the node you select.
Hope that helps you out.

Postgis nearest coordinates

I'm trying to make a REST service that returns a list of places ordered by distance from the user coordinate. I found this query using postgis:
SELECT *
FROM your_table
ORDER BY your_table.geom <-> "your location..."
LIMIT 5;
But I'm not able to apply this to my actual database. I have a table that contains these columns:
title, address, description, latitude, longitude
all these values as Strings.
I'll be very happy if someone help me. Thx!
I dont know why, but ORDER BY <-> isnt exact. Sometime the closest link is on the 3rd position. So I get 101 element and then use distance to selected the closest one.
CREATE OR REPLACE FUNCTION map.get_near_link(
x numeric,
y numeric)
RETURNS TABLE(Link_ID int, distance int) AS
$BODY$
DECLARE
strPoint text;
BEGIN
strPoint = 'POINT('|| X || ' ' || Y || ')';
With CTE AS (
SELECT Link_ID,
TRUNC(ST_Distance(ST_GeomFromText(strPoint,4326), geom )*100000)::integer as distance
FROM map.vzla_seg S
ORDER BY
geom <-> ST_GeomFromText(strPoint, 4326)
LIMIT 101
)
SELECT *
FROM CTE
ORDER BY distance
LIMIT 5
In order to use PostGIS you have to enable the extension in the database. Ideally, you just run the CREATE EXTENSION postgis; command and it works. NOTE form the install page: DO NOT INSTALL it in the database called postgres. For more information visit the site.
Adding a geometry column (spatial data can be stored in this type of columns) to your table:
SELECT AddGeometryColumn(
'your_schema',
'your_table',
'geom', -- name of the column
4326, -- SRID, for GPS coordinates you can use this, for more information https://en.wikipedia.org/wiki/Spatial_reference_system
'POINT', -- type of geometry eg. POINT, POLYGON etc.
2 -- number of dimension (2 xy - 3 xyz)
);
UPDATE yourtable t SET t.geom = ST_SetSRID(ST_MakePoint(t.x, t.y), 4326)
-- the x and y is the latitude and longitude
Now you can use spatial queries on your table like this:
SELECT
*
FROM
your_table
ORDER BY
your_table.geom <-> ST_SetSRID(ST_MakePoint(x, y), 4326)
LIMIT 5;
NOTE: as others mentioned, below PostgreSQL 9.5 <-> isn't always reliable.

OrientDB combination of edge and vertex properties

I am trying to select a edge "Contained" that points to a "Rgb" vertex, in OrientDB 2.1, with PhpOrient. The edge haves a property "probability", and the Rgb haves a property "hex".
So:
Artist -> created -> Artwork -> contained -> Rgb.
This query finds the edge "Contained" with the property "probability".
SELECT *, EXPAND (OUT("Created").OUTE("Contained")) FROM Artist WHERE path="Achebach, Andreas"
This query finds the vertex Rgb with the property "hex".
SELECT *, EXPAND (OUT("Created").OUT("Contained")) FROM Artist WHERE path="Achebach, Andreas"
But I want to have both properties. How can I combine the queries to have one query, that can find both properties?
In v 2.1 you can do it with a subquery:
SELECT probability, inV().hex FROM (
SELECT expand(out("Created").outE("Contained"))
FROM Artist WHERE path="Achebach, Andreas"
)
In OrientDB v 2.2 you can also do it with pattern matching
MATCH
{class:Artist, as:a, where:(path="Achebach, Andreas")}
.out("created"){as:aw}
.outE("contained"){as:cont}
.inV(){as:rgb}
RETURN cont.probability, rgb.hex

How to create an exclusion filter set in Tableau

I am working in Tableau and trying to figure out how to create a filter exclusion. For example I have the following fields.
Hospital CallType CallDate
I want to filter out all hospitals where one of the Calls has a call type of ColdCall and a Call DateBetween X and Y.
I can do this easily in SQL but don't have access to this data in the SQL Database. It would be the following:
Select
Hospital
,CallType
,CallDate
Into
#TempTable
From
Database
Select
Hospital
,CallType
,CallDate
Into
#ExclusionTable
From
Database
Where
CallType = 'Cold'
and
CallDate Between X and Y
Select
Hospital
,CallType
,CallDate
From
#TempTable
Where
Hospital not in
(Select
Hospital
From
#ExclusionTable)
Any suggestions would be greatly appreciated.
Thanks,
Simple. Create a calculated field Filter:
IF CallType = "Cold" AND CallDate < X AND CallDate > Y
THEN 1
ELSE 0
END
Then drag Hospital to filter, go to Condition tab, select by field, get your Filter field, use sum > 0. It will filter out any hospital that have at least one call with your conditions (because all the calls that don't meet will be zero, and if at least one is not zero, the sum will be over 0)
For X and Y, I'd create parameters. It's easier (and safer) than trying to write the dates directly on the field. And you can manipulate then more easily too