我正在处理来自以下SPARQL端点:http://vocab.getty.edu/sparql的数据。我需要一些有关如何检索维护分层树表示形式的所有skos:member的输入。对于每个概念/子概念,我还将使用@en,@de,@it,@fr语言过滤相对标签。
SELECT DISTINCT ?facet ?concept ?concept_2
?concept_3 ?concept_4
?concept_5
?concept_6
WHERE {{?facet dc:identifier "300264086"} UNION {
OPTIONAL {?facet dc:identifier "300264086";
skos:member ?concept.}} UNION {
OPTIONAL {?facet dc:identifier "300264086";
skos:member/skos:member ?concept_2}} UNION {
OPTIONAL{ ?facet dc:identifier "300264086";
skos:member/ skos:member ?concept_2.
?concept_2 skos:member ?concept_3}} UNION {
OPTIONAL{?facet dc:identifier "300264086";
skos:member/skos:member/skos:member ?concept_3.
?concept_3 skos:member ?concept_4}} UNION {
OPTIONAL{?facet dc:identifier "300264086";
skos:member/skos:member/skos:member/skos:member ?concept_4.
?concept_4 skos:member ?concept_5}} UNION {
OPTIONAL{?facet dc:identifier "300264086";
skos:member/skos:member/skos:member/skos:member/skos:member ?concept_5.
?concept_5 skos:member ?concept_6}}
}
SELECT DISTINCT ?facet ?facet_label_en
?concept ?concept_label_en ?concept_label_de ?concept_label_it ?concept_label_fr
?concept_2 ?concept_2_label_en ?concept_2_label_de ?concept_2_label_it ?concept_2_label_fr
?concept_2a ?concept_2a ?concept_2a_label_en ?concept_2a_label_de ?concept_2a_label_it ?concept_2a_label_fr
?concept_2b ?concept_2b ?concept_2b_label_en ?concept_2b_label_de ?concept_2b_label_it ?concept_2b_label_fr
?concept_3 ?concept_3_label_en
?concept_4 ?concept_4_label_en
?concept_5 ?concept_5_label_en
?concept_6 ?concept_6_label_en
WHERE {{?facet dc:identifier "300264086".
OPTIONAL {{ ?facet xl:prefLabel ?facet_labelEN.?facet_labelEN gvp:term ?facet_label_en.} FILTER langMatches(lang(?facet_label_en), "en").}}
UNION {
OPTIONAL {?facet dc:identifier "300264086";
skos:member ?concept.
OPTIONAL {{ ?concept xl:prefLabel ?concept_labelEN.?concept_labelEN gvp:term ?concept_label_en.} FILTER langMatches(lang(?concept_label_en), "en").}
OPTIONAL {{ ?concept xl:prefLabel ?concept_labelDE.?concept_labelDE gvp:term ?concept_label_de.} FILTER langMatches(lang(?concept_label_de), "de").}
OPTIONAL {{ ?concept xl:prefLabel ?concept_labelIT.?concept_labelIT gvp:term ?concept_label_it.} FILTER langMatches(lang(?concept_label_it), "it").}
OPTIONAL {{ ?concept xl:prefLabel ?concept_labelFR.?concept_labelFR gvp:term ?concept_label_fr.} FILTER langMatches(lang(?concept_label_fr), "fr").}}}
UNION {
OPTIONAL {?facet dc:identifier "300264086";
skos:member/skos:member ?concept_2.
OPTIONAL {{ ?concept_2 xl:prefLabel ?concept_2_labelEN.?concept_2_labelEN gvp:term ?concept_2_label_en.} FILTER langMatches(lang(?concept_2_label_en), "en").}
OPTIONAL {{ ?concept_2 xl:prefLabel ?concept_2_labelDE.?concept_2_labelDE gvp:term ?concept_2_label_de.} FILTER langMatches(lang(?concept_2_label_de), "de").}
OPTIONAL {{ ?concept_2 xl:prefLabel ?concept_2_labelIT.?concept_2_labelIT gvp:term ?concept_2_label_it.} FILTER langMatches(lang(?concept_2_label_it), "it").}
OPTIONAL {{ ?concept_2 xl:prefLabel ?concept_2_labelFR.?concept_2_labelFR gvp:term ?concept_2_label_fr.} FILTER langMatches(lang(?concept_2_label_fr), "fr").}
OPTIONAL {?concept_2 skos:member ?concept_2a.
OPTIONAL {{ ?concept_2a xl:prefLabel ?concept_2a_labelEN.?concept_2a_labelEN gvp:term ?concept_2a_label_en.} FILTER langMatches(lang(?concept_2a_label_en), "en").}
OPTIONAL {{ ?concept_2a xl:prefLabel ?concept_2a_labelDE.?concept_2a_labelDE gvp:term ?concept_2a_label_de.} FILTER langMatches(lang(?concept_2a_label_de), "de").}
OPTIONAL {{ ?concept_2a xl:prefLabel ?concept_2a_labelIT.?concept_2a_labelIT gvp:term ?concept_2a_label_it.} FILTER langMatches(lang(?concept_2a_label_it), "it").}
OPTIONAL {{ ?concept_2a xl:prefLabel ?concept_2a_labelFR.?concept_2a_labelFR gvp:term ?concept_2a_label_fr.} FILTER langMatches(lang(?concept_2a_label_fr), "fr").}
OPTIONAL {?concept_2a skos:member ?concept_2b.
OPTIONAL {{ ?concept_2b xl:prefLabel ?concept_2b_labelEN.?concept_2b_labelEN gvp:term ?concept_2b_label_en.} FILTER langMatches(lang(?concept_2b_label_en), "en").}
OPTIONAL {{ ?concept_2b xl:prefLabel ?concept_2b_labelDE.?concept_2b_labelDE gvp:term ?concept_2b_label_de.} FILTER langMatches(lang(?concept_2b_label_de), "de").}
OPTIONAL {{ ?concept_2b xl:prefLabel ?concept_2b_labelIT.?concept_2b_labelIT gvp:term ?concept_2b_label_it.} FILTER langMatches(lang(?concept_2b_label_it), "it").}
OPTIONAL {{ ?concept_2b xl:prefLabel ?concept_2b_labelFR.?concept_2b_labelFR gvp:term ?concept_2b_label_fr.} FILTER langMatches(lang(?concept_2b_label_fr), "fr").}
OPTIONAL {?concept_2b skos:member ?concept_2c.
OPTIONAL {?concept_2c skos:member ?concept_2d}}}}
}}
UNION {
OPTIONAL{ ?facet dc:identifier "300264086";
skos:member/ skos:member ?concept_2.
?concept_2 skos:member ?concept_3.
OPTIONAL {{ ?concept_3 xl:prefLabel ?concept_3_labelEN.?concept_3_labelEN gvp:term ?concept_3_label_en.} FILTER langMatches(lang(?concept_3_label_en), "en").}}}
UNION {
OPTIONAL{?facet dc:identifier "300264086";
skos:member/skos:member/skos:member ?concept_3.
?concept_3 skos:member ?concept_4.
OPTIONAL {{ ?concept_4 xl:prefLabel ?concept_4_labelEN.?concept_4_labelEN gvp:term ?concept_4_label_en.} FILTER langMatches(lang(?concept_4_label_en), "en").}}}
UNION {
OPTIONAL{?facet dc:identifier "300264086";
skos:member/skos:member/skos:member/skos:member ?concept_4.
?concept_4 skos:member ?concept_5.
OPTIONAL {{ ?concept_5 xl:prefLabel ?concept_5_labelEN.?concept_5_labelEN gvp:term ?concept_5_label_en.} FILTER langMatches(lang(?concept_5_label_en), "en").}}}
UNION {
OPTIONAL{?facet dc:identifier "300264086";
skos:member/skos:member/skos:member/skos:member/skos:member ?concept_5.
?concept_5 skos:member ?concept_6.
OPTIONAL {{ ?concept_6 xl:prefLabel ?concept_6_labelEN.?concept_6_labelEN gvp:term ?concept_6_label_en.} FILTER langMatches(lang(?concept_6_label_en), "en").}}}
}
但是,在2b之后添加更多子概念(例如2c / 2d / 3 / 3a / 3b ...),SPARQL端点将阻塞该请求(我不知道这是否是核心术语) (无法到达页面)。
关于如何改进/优化/重写SPARQL查询的任何建议?
谢谢!
答案 0 :(得分:0)
另一种方法是使用多个嵌套的OPTIONAL
块而不是UNION
。这样,到树中节点的路径就是解决方案的一部分。正如@ASKW指出的那样,这仅适用于skos:member(或任何其他传递属性)上的固定深度。
例如建议使用以下内容:
SELECT DISTINCT ?facet ?facet_label_en ?hop1 ?hop2 ?hop3 ?hop4 ?hop5 ?hop6
?concept ?concept_label_en ?concept_label_de ?concept_label_it ?concept_label_fr
WHERE {
?facet dc:identifier "300264086"
OPTIONAL {{ ?facet xl:prefLabel ?facet_labelEN.?facet_labelEN gvp:term ?facet_label_en.} FILTER langMatches(lang(?facet_label_en), "en").}
OPTIONAL {{ ?facet skos:member ?hop1.} union {?facet skos:member ?hop1.
OPTIONAL { {?hop1 skos:member ?hop2.} union {?hop1 skos:member ?hop2
OPTIONAL { {?hop2 skos:member ?hop3} union {?hop2 skos:member ?hop3
OPTIONAL { {?hop3 skos:member ?hop4} union {?hop3 skos:member ?hop4
OPTIONAL { {?hop4 skos:member ?hop5} union {?hop4 skos:member ?hop5
OPTIONAL { ?hop5 skos:member ?hop6 }
}}
}}
}}
}}
}}
bind (COALESCE(?hop6, ?hop5, ?hop4, ?hop3, ?hop2, ?hop1) as ?concept)
OPTIONAL {{ ?concept xl:prefLabel ?concept_labelEN.?concept_labelEN gvp:term ?concept_label_en.} FILTER langMatches(lang(?concept_label_en), "en").}
OPTIONAL {{ ?concept xl:prefLabel ?concept_labelDE.?concept_labelDE gvp:term ?concept_label_de.} FILTER langMatches(lang(?concept_label_de), "de").}
OPTIONAL {{ ?concept xl:prefLabel ?concept_labelIT.?concept_labelIT gvp:term ?concept_label_it.} FILTER langMatches(lang(?concept_label_it), "it").}
OPTIONAL {{ ?concept xl:prefLabel ?concept_labelFR.?concept_labelFR gvp:term ?concept_label_fr.} FILTER langMatches(lang(?concept_label_fr), "fr").}
}
请注意在每个嵌套的UNION
中使用OPTIONAL
,目的是在结果中也包括中间节点。
HTH