缩小SPARQL查询 [英] Narrowing down on SPARQL query

查看:97
本文介绍了缩小SPARQL查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想查询一个仅返回person1(David)和person2(Charles)一起出现的项目的查询.

I want to make a query that returns only the items where person1(David) and person2(Charles) appears together.

我的查询是:

SELECT ?item ?par ?mod ?hon ?firstName ?lastName
WHERE {
    ?item   sci:itemTitle ?title.
    {?item sci:hasParticipant ?par. {?par sci:firstName "Charles".}UNION{?par sci:firstName "David".}} 
    UNION {?item sci:hasModerator ?mod. {?mod sci:firstName "Charles".}UNION{?mod sci:firstName "David".}} 
    UNION {?item sci:hasGuestOfHonor ?hon. {?hon sci:firstName "Charles".}UNION{?hon sci:firstName "David".}}
}

我的结果显示在这里:

只有两次出现person1和person2一起出现(计划项目4和6).如何缩小查询范围以获取所需的内容,或者有比我做的更好的方法呢?

There are only two occurrences where person1 and person2 appear together (Programme Item 4 and 6). How can I narrow down the query to get what I want or is there a better way to do it than what I did?

推荐答案

联盟用于表示替代项.每次您说 {x}联合{y} 时,您都在询问是否匹配 x y >(或同时按住).由于您正在寻找涉及两个人(而不是两个人)的案例,因此您不想使用联盟.据我了解,您可以通过三种方式使一个人参与某个项目:他们可以成为参与者,主持人或贵宾.您可以将其压缩为:

union is for expressing alternatives. Each time you say { x } union { y }, you're asking for matches whether either x or y (or both) hold. Since you're looking for cases where both people are involved (not either person), you don't want to use union. As I understand it, you have three ways that a person can part of an item: they can be a participant, a moderator, or a guest of honor. You can condense that into:

?item sci:hasModerator|sci:hasParticipant|sci:hasGuestofHonor ?person

模式?ax | y | z?b 表示?a 通过之一连接到?b em> x y z .这是一条交替的道路.您可以在SPARQL 1.1规范§ 9属性路径中了解有关属性路径的更多信息. a>.

The pattern ?a x|y|z ?b says that ?a is connected to ?b by either x, y, or z. It's an alternation path. You can read more about property paths in the SPARQL 1.1 spec, §9 Property Paths.

然后,您希望有两个人,所以:

Then, you want there to be two people, so:

?item sci:hasModerator|sci:hasParticipant|sci:hasGuestofHonor ?person1, ?person2

会给您带来两个人(或可能是一个人),但是当我们检查姓名时,我们将强制他们拥有我们想要的名字,这将要求他们是不同的人,或者至少是一个人.两个名字).模式?ap?b,?c 表示?a 两者 ?b ?c .

will get you two people (or possibly the same person, but when we check for names, we'll enforce that they have the names we want, which should require them to be different people, or at least a single person with two first names). The pattern ?a p ?b, ?c says that ?a is related by p to both ?b and ?c.

最后,您希望一个人的名字叫Charles,另一个叫David,所以我们只需要添加

Finally, you want one person to have the first name Charles and the other David, so we just need to add

?person1 sci:firstName "Charles" .
?person2 sci:firstName "David" .

由于您实际上并未将?person1 ?person2 的值用于其他任何内容,因此您也可以在此处使用空白节点.例如

Since you're not actually using the values of ?person1 and ?person2 for anything else, you can also just use a blank node here. E.g.,

?item sci:hasModerator|sci:hasParticipant|sci:hasGuestofHonor [ sci:firstName "Charles" ] ,  [ sci:firstName "David" ] .

让我们看看它们如何融合在一起.我根据您显示的结果创建了一些样本数据.显然,它与您的数据不同,但是足以重现您显示的结果并演示其工作原理.

Let's see how it all fits together. I created some sample data based on the results you showed. Obviously, it's not the same as your data, but it's got enough to reproduce the results that you showed, and to demonstrate how this works.

prefix sci: <urn:ex:>

sci:Male_Person2 sci:firstName "David" .
sci:Male_Person3 sci:firstName "Charles" .

sci:ProgrammeItem1 sci:itemTitle "" ;
                   sci:hasParticipant sci:Male_Person3 ;
                   sci:hasModerator sci:Male_Person3 .
sci:ProgrammeItem3 sci:itemTitle "" ;
                   sci:hasParticipant sci:Male_Person2 ;
                   sci:hasModerator sci:Male_Person2 .
sci:ProgrammeItem4 sci:itemTitle "" ;
                   sci:hasParticipant sci:Male_Person3 ;
                   sci:hasModerator sci:Male_Person2 .
sci:ProgrammeItem6 sci:itemTitle "" ;
                   sci:hasParticipant sci:Male_Person2, sci:Male_Person3 .

prefix sci: <urn:ex:>

select ?item {
    ?item sci:itemTitle ?title .
    ?item sci:hasModerator|sci:hasParticipant|sci:hasGuestofHonor
      [ sci:firstName "Charles" ] ,
      [ sci:firstName "David" ] .
}

----------------------
| item               |
======================
| sci:ProgrammeItem4 |
| sci:ProgrammeItem6 |
----------------------

这篇关于缩小SPARQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆