首页 > 解决方案 > Sparql 查询仅获取匹配条件的最顶层元素

问题描述

我有多个树,其中节点链接在一起hasParent,每个节点都有一些特定的颜色。我需要按颜色过滤树并提取最顶层的元素(过滤后排除嵌套元素)。

这是我的数据示例:

在此处输入图像描述

我需要的结果是:[A, B, I].

我知道如何实现返回的基本树过滤器[A, B, I, J, L, O]

SELECT DISTINCT ?s WHERE {
  ?s <http://looneytunes-graph.com/color> "yellow"^^xsd:string
}

有没有办法实现这种额外的过滤?还是我需要重新考虑我的解决方案并使用其他东西而不是 graph db (AWS Neptune)?

我正在使用https://sparql-playground.sib.swiss/data和以下数据对其进行测试:

@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

<http://looneytunes-graph.com/A> <http://looneytunes-graph.com/color> "yellow"^^xsd:string .

<http://looneytunes-graph.com/B> <http://looneytunes-graph.com/color> "yellow"^^xsd:string .

<http://looneytunes-graph.com/C> <http://looneytunes-graph.com/color> "green"^^xsd:string .

<http://looneytunes-graph.com/D> <http://looneytunes-graph.com/color> "red"^^xsd:string ;
    <http://looneytunes-graph.com/hasParent> <http://looneytunes-graph.com/A> .

<http://looneytunes-graph.com/E> <http://looneytunes-graph.com/color> "blue"^^xsd:string ;
    <http://looneytunes-graph.com/hasParent> <http://looneytunes-graph.com/A> .

<http://looneytunes-graph.com/F> <http://looneytunes-graph.com/color> "blue"^^xsd:string ;
    <http://looneytunes-graph.com/hasParent> <http://looneytunes-graph.com/B> .

<http://looneytunes-graph.com/G> <http://looneytunes-graph.com/color> "green"^^xsd:string ;
    <http://looneytunes-graph.com/hasParent> <http://looneytunes-graph.com/B> .

<http://looneytunes-graph.com/H> <http://looneytunes-graph.com/color> "red"^^xsd:string ;
    <http://looneytunes-graph.com/hasParent> <http://looneytunes-graph.com/C> .

<http://looneytunes-graph.com/I> <http://looneytunes-graph.com/color> "yellow"^^xsd:string ;
    <http://looneytunes-graph.com/hasParent> <http://looneytunes-graph.com/C> .

<http://looneytunes-graph.com/J> <http://looneytunes-graph.com/color> "yellow"^^xsd:string ;
    <http://looneytunes-graph.com/hasParent> <http://looneytunes-graph.com/D> .

<http://looneytunes-graph.com/K> <http://looneytunes-graph.com/color> "green"^^xsd:string ;
    <http://looneytunes-graph.com/hasParent> <http://looneytunes-graph.com/D> .

<http://looneytunes-graph.com/L> <http://looneytunes-graph.com/color> "yellow"^^xsd:string ;
    <http://looneytunes-graph.com/hasParent> <http://looneytunes-graph.com/F> .

<http://looneytunes-graph.com/M> <http://looneytunes-graph.com/color> "red"^^xsd:string ;
    <http://looneytunes-graph.com/hasParent> <http://looneytunes-graph.com/G> .

<http://looneytunes-graph.com/N> <http://looneytunes-graph.com/color> "blue"^^xsd:string ;
    <http://looneytunes-graph.com/hasParent> <http://looneytunes-graph.com/G> .

<http://looneytunes-graph.com/O> <http://looneytunes-graph.com/color> "yellow"^^xsd:string ;
    <http://looneytunes-graph.com/hasParent> <http://looneytunes-graph.com/I> .

标签: sparqlrdf

解决方案


SELECT DISTINCT ?s WHERE {
  ?s <http://looneytunes-graph.com/color> "yellow"^^xsd:string .
  FILTER NOT EXISTS {
     ?s hasParent+ [<http://looneytunes-graph.com/color> "yellow"^^xsd:string] .
  }
}

使用此查询,您将过滤掉具有黄色父节点的节点(具有父节点的父节点具有...)。


推荐阅读