I have one vertex connected to other vertices as:
B1 <------E1------ A1 ------E2------> B2
name=name1 name=name2
Vertex A1 of class A has several properties.
Edges E1 and E2 are of class and each have a name property.
I want a query to return Vertex A1 but with a map of the connected vertices' rids as keys and the name on their respective edge as values.
I.e, I want to return:
{
<all of vertex A1's normal properties>,
connected: {
<ridB1>: name1,
<ridB2>: name2
}
}
I know how to return this:
{
<all of vertex A1's normal properties>,
names: ["name1", "name2"],
rids: ["ridB1", "ridB2"]
}
By doing:
SELECT *, out("E")[#rid] as rids, outE("E")[name] as names from #14:0
But I can't seem to be able to build the map I would like from the two lists.
Try this:
select #rid, name from E where outV().name LIKE "%A1%"
this is what I get:
Hope it helps
Regards
Related
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.
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.
I want to write a query in OrientDB performs WHERE filtering on some columns/fields on the SELECTed vertex.
Here is the equivalent query implemented with nested SELECT's-
SELECT FROM (SELECT EXPAND(OUT('Foo')) FROM #13:1 ) WHERE prop = 'bar'
How I can write this query with a single SELECT?
create class Foo extends E
create vertex #9:0
create vertex set prop = 'bar' #9:1
create vertex set prop = 'baz' #9:2
create edge Foo from #9:0 to #9:1
create edge Foo from #9:0 to #9:2
You can:
select expand(out('Foo')[prop = 'bar']) from #9:0
I am a beginner in OrientDB.
Consider that I have 2 Vertex, Cat and Val.
Cat contains a property called category and Val contains a property called value.
Categories can have sub-categories and those can further have sub-categories and so on. The categories and sub-categories are stored in Vertex Cat. The sub-categories are mapped using an edge called CatEdge whose from and to are the same vertex i.e. Cat.
For example, consider a category 'Education' which has two sub-categories 'School' and 'College'. The 'College' sub-category has further sub-categories 'Bachelors' and 'Masters'. So, there will be an edge in CatEdge from 'Education' to 'School' and 'College', and from 'College' to 'Bachelors' and 'Masters'.
Education
|- School
|- College
|- Bachelors
|- Masters
Apart from these, the Cat Vertex can have categories that do not have any sub-categories, for example 'FirstName', 'LastName', etc.
All the 'leaf' categories (that do not have further sub-category) have an edge called ValEdge from Vertex Cat to Vertex Val.
I want to retrieve all 'value' from Val for all the categories and sub-categories.
What I have done:
First, I fired the following query to retrieve all categories that do not have sub-categories and which are not a sub-category of other category:
select from Cat where #rid not in (select #rid, expand(both('CatEdge')) from Cat)
Then, programatically, I loop through all the categories fetched and find their corresponding values:
select expand(out('ValEdge')) from Cat where category = 'FirstName'
Second, I fetch all the categories that have sub-categories or are itself a sub-category using:
select from (traverse out('CatEdge') from Cat) where out('CatEdge').size() > 0
And store it in a list called SubList.
The above query will give me 'Education' and 'College'.
Using this list, for each item, I check if there exists its sub-category using:
select expand(out('CatEdge')) from Cat where category = 'Education'
The above query will give 'School' and 'College'. Then, programmatically, I check if 'School' and 'College' exists in SubList.
If it exists, I first remove it from the SubList and fire the above query again and this continues until I get zero rows.
If it does not exists in SubList, then it is a 'leaf' category and then find its value in the Val Vertex.
As you may have noticed, this is getting too complex. Is there any other way that I can achieve the same?
If this is your situation:
create class Cat extends V
create property Cat.category string
create class CatEdge extends E
create class Val extends V
create property Val.value integer
create class ValEdge extends E
create vertex Cat set category = 'Education'
create vertex Cat set category = 'School'
create vertex Cat set category = 'College'
create vertex Cat set category = 'Bachelors'
create vertex Cat set category = 'Masters'
create vertex Val set value = 1
create vertex Val set value = 2
create vertex Val set value = 3
create edge CatEdge from (select from Cat where category = 'Education') to (select from Cat where category = 'School')
create edge CatEdge from (select from Cat where category = 'Education') to (select from Cat where category = 'College')
create edge CatEdge from (select from Cat where category = 'College') to (select from Cat where category = 'Bachelors')
create edge CatEdge from (select from Cat where category = 'College') to (select from Cat where category = 'Masters')
create edge ValEdge from (select from Cat where category = 'School') to (select from Val where value = 1)
create edge ValEdge from (select from Cat where category = 'Bachelors') to (select from Val where value = 2)
create edge ValEdge from (select from Cat where category = 'Masters') to (select from Val where value = 3)
And if I understood your intention correctly, this query will work:
select in("ValEdge").category, value from Val
Output:
UPDATE
select category, $subcategories, $value from Cat
let
$subcategories = ( select category from (traverse out('CatEdge') from $parent.$current ) where $depth >=1 ),
$value = ( select out('ValEdge').value as value from $current )
returns this JSON.
Note that for all categories, or you have a list of subcategories, or, if it's a leaf, its value.
Like lightweight edges, it seems like when a new vertex subtype is added to the schema then it is "invisible" until it has a concrete instance, ie:
create class Invisible extends V
create property Invisible.name string
select distinct(#class) from V
will return 0 results until I
create vertex Invisible set name='Not invisible anymore'
Is there a way around this?
select name from ( select expand(classes) from metadata:schema ) where superClass='V'
Thanks, Lvca