首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >OrientDB查找顶点与另一个顶点的列表链接。

OrientDB查找顶点与另一个顶点的列表链接。
EN

Stack Overflow用户
提问于 2015-08-04 16:09:40
回答 2查看 170关注 0票数 1

我发现OrientDB和我有一个问题:

我有两个顶点定义:

代码语言:javascript
复制
 Product
 Criterion

和一种边缘定义:

代码语言:javascript
复制
 - IsRelatedToEdge

多亏了IsRelatedToEdge,我可以将一个产品链接到多个标准

在我的示例中,我已经用5种产品填充了数据库。

代码语言:javascript
复制
 shoe-1
 shoe-2
 shoe-3
 hat-1
 hat-2

4种标准:蓝红帽鞋

然后,我用这种方式将产品与准则联系起来:

代码语言:javascript
复制
 shoe-1 <=> shoe
 shoe-1 <=> blue
 shoe-2 <=> shoe
 shoe-2 <=> red
 shoe-3 <=> shoe
 shoe-3 <=> blue
 hat-1 <=> shoe
 hat-1 <=> blue
 hat-2 <=> hat
 hat-2 <=> red

所以我们有两只蓝鞋,一顶蓝帽子,一只红鞋,一顶红帽。

我想不出怎么才能找到所有的蓝色鞋子。

编辑:我找到了一个“解决方案”,但看起来不太好:

代码语言:javascript
复制
select from Product where 
    in('IsRelatedToEdge')[name="blue"].size() = 1 and
    in('IsRelatedToEdge')[name="shoe"].size() = 1
EN

回答 2

Stack Overflow用户

发布于 2015-08-05 01:56:49

IMO,OrientDB的强大之处在于图形功能,而对表/索引的查询并没有真正利用这一点。我觉得做这个查询最好的方法是得到鞋的标准,然后得到所有的产品,有一个标准的边缘。从那些产品(所有的鞋),你现在可以过滤那些也有一个边缘的蓝色标准。写这篇文章的方法如下..。

代码语言:javascript
复制
select * 
from (select expand(both('IsRelatedToEdge')) from Criterion where name = 'Shoe') 
let $blue_criterion = (select from Criterion where name = 'Blue')
where both('IsRelatedToEdge') contains $blue_criterion[0]

尽管如此,您可以考虑重新安排您的数据,以便更好/更容易地查询。例如,您可以创建一个Hat和Shoe类,它们都是Product的子类。通过这种方式对鞋子进行查询,只对Shoe顶点类进行查询。类似地,您可以创建不同的条件子类,例如Color。要获得这样一个配置的蓝色鞋子,查询需要以下内容.

代码语言:javascript
复制
select * 
from Shoes
let $blue_criterion = (select from Color where name = 'Blue')
where both('IsRelatedToEdge') contains $blue_criterion[0]

你甚至可以做出更具体的边缘,以使这一步进一步。

票数 1
EN

Stack Overflow用户

发布于 2015-08-05 08:54:41

在研究了neRok的解决方案之后,我找到了以下解决方案:

代码语言:javascript
复制
select expand($result)
let
       $crit1 = (select expand(out('IsRelatedToEdge')) from Criterion where name='blue'),
       $crit2 = (select expand(out('IsRelatedToEdge')) from Criterion where name='shoe'),
       $result = intersect($crit1, $crit2)

通过这种查询,我可以添加另一个标准。想象一下,如果我们有阿迪达斯标准,我想要所有蓝色的阿迪达斯鞋:

代码语言:javascript
复制
select expand($result)
    let
           $crit1 = (select expand(out('IsRelatedToEdge')) from Criterion where name='blue'),
           $crit2 = (select expand(out('IsRelatedToEdge')) from Criterion where name='shoe'),
           $crit3 = (select expand(out('IsRelatedToEdge')) from Criterion where name='adidas'),
           $result = intersect($crit1, $crit2, $crit3)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31814217

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档