2

I want to find all entities related to another entity in wikidata, say matthew effect.

How to query by SPARQL?

olo
  • 133
  • 1
  • 7
  • not sure what "related" means in your context, but write a query with a single triple pattern where the entity is in subject position would be a good start to get all outgoing connected entities (and literals). If you want to omit the literals, use a filter on the objects to get only URIs. If you also want entities related via incoming edges, write another triple pattern with the entity being in object position. Then combine both via a `UNION` construct. Done. – UninformedUser Oct 16 '22 at 14:58
  • @UninformedUser Thank you. I do not quite understand. Could you show some code? like 'SELECT ?items WHERE { ?items wdt:* wd:Q1337985. }' – olo Oct 17 '22 at 07:09
  • no, i was saying in "subject position" the entity, the predicate and object leave unspecified. like `select ?item {wd:Q1337985 ?p ?item}` – UninformedUser Oct 17 '22 at 07:48
  • @UninformedUser Thank you. I got different language versions of the item. I would like sth more diverse. All related to that item, including the author who created that item, all the books cited that item , the families of the author, etc. – olo Oct 17 '22 at 08:09
  • not sure if I understand, but the query does return all being explicitely stated about the entity, like you open in browser: https://www.wikidata.org/wiki/Q1337985 - if you don't have an explicit definition of "related" it will be hard to translate it to SPARQL. Saying "I want also families of author" would need to gather those properties from Wikidata first, then add corresponding triple patterns - works then for one case or one type of entities, but what about other types of entities? – UninformedUser Oct 17 '22 at 08:15
  • 1
    you can also run that query to get more structured data: `SELECT ?wdLabel ?ps_Label ?wdpqLabel ?pq_Label { VALUES (?item) {(wd:Q1337985)} ?item ?p ?statement . ?statement ?ps ?ps_ . ?wd wikibase:claim ?p. ?wd wikibase:statementProperty ?ps. OPTIONAL { ?statement ?pq ?pq_ . ?wdpq wikibase:qualifier ?pq . } SERVICE wikibase:label { bd:serviceParam wikibase:language "en" } } ORDER BY ?wd ?statement ?ps_` – UninformedUser Oct 17 '22 at 08:18
  • but it will not return transitive related entities. Getting entities via another hop in the knowledge graph is trivial though, just append a triple pattern where subject is the object variable of the original triple pattern: like add `?item ?p2 ?item2 .` to the original query – UninformedUser Oct 17 '22 at 08:20
  • @UninformedUser Thank you. If my understanding is correct, I should , in advance, know what elations I am interested in. If I want to obtain the "whole picture" of that entity, it is not possible? – olo Oct 17 '22 at 08:29
  • @UninformedUser, maybe I need sth. ike this (https://stackoverflow.com/questions/33241812/sparql-query-to-get-all-triples-related-to-a-specific-subject), and the 2-hop items of the item. – olo Oct 17 '22 at 08:51
  • what is the "whole picture" of en entity? that is exactly my point. Indeed, you can get all directly and 1 hop related entities: `select ?item { { wd:Q1337985 ?p ?item } UNION { wd:Q1337985 ?p ?tmp . ?tmp ?p1 ?item }}` – UninformedUser Oct 17 '22 at 09:40
  • @UninformedUser. Thank you. After thinking your question, I am getting much clearer. I would like to extract all triples (entity1 property entity2), which contains all 1- and 2- hop entities (The entity is either as entity 1 or entity2) . – olo Oct 18 '22 at 11:29

0 Answers0