SPARQL query to retrieve all subjects starting with a particular URI
Asked Answered
G

0

7

I have triples data where subjects starts with uri's like

<http://www.test.com/work/12353>
<http://www.test.com/term/12353>
<http://www.test.com/name/12353>

there are approx 70k triples in total.

I want to get all the subject values that starts with <http://www.test.com/work/>

I am able to achieve this using FILTER as given below, I'm curious to know if this the correct way or there is a better way to achieve the same?

SELECT DISTINCT ?s
WHERE {
    ?s ?p ?o .
  FILTER (strstarts(str(?s), 'http://www.test.com/work/'))
}
LIMIT 1000

Please help.

Gandzha answered 12/6, 2018 at 7:23 Comment(4)
Using a regex will be less efficient.Sophistic
This is the correct way and there is no "better" way. Even for a triple store with fulltext-index support, usually a URI isn't index at all. Clearly, depending on the triple store this might be configured.Sedlik
This is the correct generic way to do it. However, if your 'work' URIs have a property unique to them it might be more efficient to select on that property as properties will almost certainly be indexed.Ow
Try also SELECT ?s { { SELECT DISTINCT ?s { ?s ?p ?o } } FILTER (strstarts(str(?s), 'http://www.test.com/work/')) } LIMIT 1000.Yulan

© 2022 - 2024 — McMap. All rights reserved.