You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何在Wikidata RDF图服务中使用OR逻辑:成吉思汗子女查询示例

How to Use OR for gas:linkType in Wikidata SPARQL GAS Queries

Got it! To traverse both wdt:P40 (child) and wdt:P10 (spouse) edges in your Genghis Khan graph query, you just need to add multiple gas:linkType entries to the GAS service block. The BigData GAS framework treats repeated gas:linkType declarations as an OR condition—so it will follow edges of either type during the graph walk.

Here's your updated query with the OR logic implemented:

#Children of Genghis Khan (and Spouses) #defaultView:Graph
PREFIX gas: <http://www.bigdata.com/rdf/gas#>
SELECT ?item ?itemLabel ?pic ?linkTo WHERE {
  SERVICE gas:service {
    gas:program gas:gasClass "com.bigdata.rdf.graph.analytics.SSSP" ;
    gas:in wd:Q720 ;
    gas:traversalDirection "Forward" ;
    gas:out ?item ;
    gas:out1 ?depth ;
    gas:maxIterations 4 ;
    gas:linkType wdt:P40 ;  # First edge type: child
    gas:linkType wdt:P10    # Second edge type: spouse (OR condition)
  }
  # Update the optional clause to include both link types for ?linkTo
  OPTIONAL { ?item wdt:P40|wdt:P10 ?linkTo }
  OPTIONAL { ?item wdt:P18 ?pic }
  SERVICE wikibase:label {bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" }
}

Key Notes:

  • Multiple gas:linkType lines: Each line adds an allowed edge type for the traversal. The GAS service will traverse any edge matching either type, which is exactly the OR behavior you need.
  • Updated OPTIONAL clause: I modified ?item wdt:P40 ?linkTo to ?item wdt:P40|wdt:P10 ?linkTo so that ?linkTo correctly shows the connected node for both edge types (otherwise it would only show children, not spouses).
  • Graph visualization: Since you're using defaultView:Graph, the result will display both child and spouse connections from Genghis Khan, up to 4 iterations deep.

内容的提问来源于stack exchange,提问作者Loredra L

火山引擎 最新活动