如何在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:linkTypelines: 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
OPTIONALclause: I modified?item wdt:P40 ?linkToto?item wdt:P40|wdt:P10 ?linkToso that?linkTocorrectly 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




