Search code examples
sparql

How to retrieve all skos:member of a given class (tree view)


I'm dealing with the data from this SPARQL endpoint: http://vocab.getty.edu/sparql. I need some input about how to retrieve all the skos:member(s) of a facet maintaining the hierarchical tree representation. For each concept/sub-concept I would have also the relative labels filtered by @en , @de , @it , @fr languages.

  1. I've started fetching the "core" hierarchy based on the facet and the related skos:member
SELECT DISTINCT ?facet ?concept ?concept_2  
?concept_3 ?concept_4 
?concept_5 
?concept_6 
WHERE {{?facet dc:identifier "300264086"} UNION {
              OPTIONAL {?facet dc:identifier "300264086";
                            skos:member ?concept.}} UNION {
              OPTIONAL {?facet dc:identifier "300264086";
                            skos:member/skos:member ?concept_2}} UNION {
            OPTIONAL{ ?facet dc:identifier "300264086";
                            skos:member/ skos:member ?concept_2. 
                                ?concept_2 skos:member ?concept_3}} UNION {
                    OPTIONAL{?facet dc:identifier "300264086";
                            skos:member/skos:member/skos:member ?concept_3. 
                                ?concept_3 skos:member ?concept_4}} UNION {
                    OPTIONAL{?facet dc:identifier "300264086";
                            skos:member/skos:member/skos:member/skos:member ?concept_4.
                                ?concept_4 skos:member ?concept_5}} UNION {
                    OPTIONAL{?facet dc:identifier "300264086";
                            skos:member/skos:member/skos:member/skos:member/skos:member ?concept_5.
                                ?concept_5 skos:member ?concept_6}}

       }
  1. Then I moved a step forward fetching also the sub-concepts with the labels filtered by @en, @de, @it, @fr languages.
SELECT DISTINCT ?facet ?facet_label_en
        ?concept ?concept_label_en ?concept_label_de ?concept_label_it ?concept_label_fr
        ?concept_2 ?concept_2_label_en ?concept_2_label_de ?concept_2_label_it ?concept_2_label_fr
        ?concept_2a ?concept_2a ?concept_2a_label_en ?concept_2a_label_de ?concept_2a_label_it ?concept_2a_label_fr
        ?concept_2b ?concept_2b ?concept_2b_label_en ?concept_2b_label_de ?concept_2b_label_it ?concept_2b_label_fr
        ?concept_3 ?concept_3_label_en
        ?concept_4 ?concept_4_label_en
        ?concept_5 ?concept_5_label_en
        ?concept_6 ?concept_6_label_en

WHERE {{?facet dc:identifier "300264086".
              OPTIONAL {{ ?facet xl:prefLabel ?facet_labelEN.?facet_labelEN gvp:term ?facet_label_en.} FILTER langMatches(lang(?facet_label_en), "en").}} 

          UNION {
              OPTIONAL {?facet  dc:identifier "300264086";
                                skos:member ?concept. 
                                OPTIONAL {{ ?concept xl:prefLabel ?concept_labelEN.?concept_labelEN gvp:term ?concept_label_en.} FILTER langMatches(lang(?concept_label_en), "en").}
                                OPTIONAL {{ ?concept xl:prefLabel ?concept_labelDE.?concept_labelDE gvp:term ?concept_label_de.} FILTER langMatches(lang(?concept_label_de), "de").}
                                OPTIONAL {{ ?concept xl:prefLabel ?concept_labelIT.?concept_labelIT gvp:term ?concept_label_it.} FILTER langMatches(lang(?concept_label_it), "it").}
                                OPTIONAL {{ ?concept xl:prefLabel ?concept_labelFR.?concept_labelFR gvp:term ?concept_label_fr.} FILTER langMatches(lang(?concept_label_fr), "fr").}}} 

          UNION {
              OPTIONAL {?facet  dc:identifier "300264086";
                                skos:member/skos:member ?concept_2.
                                OPTIONAL {{ ?concept_2 xl:prefLabel ?concept_2_labelEN.?concept_2_labelEN gvp:term ?concept_2_label_en.} FILTER langMatches(lang(?concept_2_label_en), "en").}
                                OPTIONAL {{ ?concept_2 xl:prefLabel ?concept_2_labelDE.?concept_2_labelDE gvp:term ?concept_2_label_de.} FILTER langMatches(lang(?concept_2_label_de), "de").}
                                OPTIONAL {{ ?concept_2 xl:prefLabel ?concept_2_labelIT.?concept_2_labelIT gvp:term ?concept_2_label_it.} FILTER langMatches(lang(?concept_2_label_it), "it").}
                                OPTIONAL {{ ?concept_2 xl:prefLabel ?concept_2_labelFR.?concept_2_labelFR gvp:term ?concept_2_label_fr.} FILTER langMatches(lang(?concept_2_label_fr), "fr").}

                                OPTIONAL {?concept_2 skos:member ?concept_2a.
                                    OPTIONAL {{ ?concept_2a xl:prefLabel ?concept_2a_labelEN.?concept_2a_labelEN gvp:term ?concept_2a_label_en.} FILTER langMatches(lang(?concept_2a_label_en), "en").}
                                    OPTIONAL {{ ?concept_2a xl:prefLabel ?concept_2a_labelDE.?concept_2a_labelDE gvp:term ?concept_2a_label_de.} FILTER langMatches(lang(?concept_2a_label_de), "de").}
                                    OPTIONAL {{ ?concept_2a xl:prefLabel ?concept_2a_labelIT.?concept_2a_labelIT gvp:term ?concept_2a_label_it.} FILTER langMatches(lang(?concept_2a_label_it), "it").}
                                    OPTIONAL {{ ?concept_2a xl:prefLabel ?concept_2a_labelFR.?concept_2a_labelFR gvp:term ?concept_2a_label_fr.} FILTER langMatches(lang(?concept_2a_label_fr), "fr").}
                                OPTIONAL {?concept_2a skos:member ?concept_2b.
                                    OPTIONAL {{ ?concept_2b xl:prefLabel ?concept_2b_labelEN.?concept_2b_labelEN gvp:term ?concept_2b_label_en.} FILTER langMatches(lang(?concept_2b_label_en), "en").}
                                    OPTIONAL {{ ?concept_2b xl:prefLabel ?concept_2b_labelDE.?concept_2b_labelDE gvp:term ?concept_2b_label_de.} FILTER langMatches(lang(?concept_2b_label_de), "de").}
                                    OPTIONAL {{ ?concept_2b xl:prefLabel ?concept_2b_labelIT.?concept_2b_labelIT gvp:term ?concept_2b_label_it.} FILTER langMatches(lang(?concept_2b_label_it), "it").}
                                    OPTIONAL {{ ?concept_2b xl:prefLabel ?concept_2b_labelFR.?concept_2b_labelFR gvp:term ?concept_2b_label_fr.} FILTER langMatches(lang(?concept_2b_label_fr), "fr").}
                                OPTIONAL {?concept_2b skos:member ?concept_2c.
                                OPTIONAL {?concept_2c skos:member ?concept_2d}}}}

                                }} 
          UNION {
              OPTIONAL{ ?facet  dc:identifier "300264086";
                                skos:member/ skos:member ?concept_2. 
                                ?concept_2 skos:member ?concept_3.
                                OPTIONAL {{ ?concept_3 xl:prefLabel ?concept_3_labelEN.?concept_3_labelEN gvp:term ?concept_3_label_en.} FILTER langMatches(lang(?concept_3_label_en), "en").}}} 
          UNION {
              OPTIONAL{?facet   dc:identifier "300264086";
                                skos:member/skos:member/skos:member ?concept_3. 
                                ?concept_3 skos:member ?concept_4.
                                OPTIONAL {{ ?concept_4 xl:prefLabel ?concept_4_labelEN.?concept_4_labelEN gvp:term ?concept_4_label_en.} FILTER langMatches(lang(?concept_4_label_en), "en").}}} 
          UNION {
              OPTIONAL{?facet   dc:identifier "300264086";
                                skos:member/skos:member/skos:member/skos:member ?concept_4.
                                ?concept_4 skos:member ?concept_5.
                                OPTIONAL {{ ?concept_5 xl:prefLabel ?concept_5_labelEN.?concept_5_labelEN gvp:term ?concept_5_label_en.} FILTER langMatches(lang(?concept_5_label_en), "en").}}} 
          UNION {
              OPTIONAL{?facet   dc:identifier "300264086";
                                skos:member/skos:member/skos:member/skos:member/skos:member ?concept_5.
                                ?concept_5 skos:member ?concept_6.
                                OPTIONAL {{ ?concept_6 xl:prefLabel ?concept_6_labelEN.?concept_6_labelEN gvp:term ?concept_6_label_en.} FILTER langMatches(lang(?concept_6_label_en), "en").}}}

       }

However, adding more sub-concepts following the 2b (e.g 2c/2d/3/3a/3b...) the SPARQL endpoint is going to block(I don't know if this is the corect term) the request (Page unreacheble).

Any suggestions about how to improve/optimize/re-write the SPARQL query?

Thanks!


Solution

  • Another approach is to use several nested OPTIONAL blocks instead of UNION. That way, the path to a node in the tree is part of the solution. As @ASKW pointed out, that will work only for a fixed depth over skos:member (or any other transitive property).

    E.g. suggest using something along the following lines:

    SELECT DISTINCT ?facet ?facet_label_en ?hop1 ?hop2 ?hop3 ?hop4 ?hop5 ?hop6
                ?concept ?concept_label_en ?concept_label_de ?concept_label_it ?concept_label_fr
        WHERE {
            ?facet dc:identifier "300264086"
            OPTIONAL {{ ?facet xl:prefLabel ?facet_labelEN.?facet_labelEN gvp:term ?facet_label_en.} FILTER langMatches(lang(?facet_label_en), "en").}
            OPTIONAL {{ ?facet skos:member ?hop1.} union {?facet skos:member ?hop1.
                          OPTIONAL { {?hop1 skos:member ?hop2.} union {?hop1 skos:member ?hop2
                            OPTIONAL { {?hop2 skos:member ?hop3} union {?hop2 skos:member ?hop3
                                OPTIONAL { {?hop3 skos:member ?hop4} union {?hop3 skos:member ?hop4
                                    OPTIONAL { {?hop4 skos:member ?hop5} union {?hop4 skos:member ?hop5
                                        OPTIONAL { ?hop5 skos:member ?hop6 }
                            }}
                        }}
                    }}
                }}
            }}
            bind (COALESCE(?hop6, ?hop5, ?hop4, ?hop3, ?hop2, ?hop1) as ?concept)
            OPTIONAL {{ ?concept xl:prefLabel ?concept_labelEN.?concept_labelEN gvp:term ?concept_label_en.} FILTER langMatches(lang(?concept_label_en), "en").}
            OPTIONAL {{ ?concept xl:prefLabel ?concept_labelDE.?concept_labelDE gvp:term ?concept_label_de.} FILTER langMatches(lang(?concept_label_de), "de").}
            OPTIONAL {{ ?concept xl:prefLabel ?concept_labelIT.?concept_labelIT gvp:term ?concept_label_it.} FILTER langMatches(lang(?concept_label_it), "it").}
            OPTIONAL {{ ?concept xl:prefLabel ?concept_labelFR.?concept_labelFR gvp:term ?concept_label_fr.} FILTER langMatches(lang(?concept_label_fr), "fr").}
    
    }
    

    Note the use of UNION in each nested OPTIONAL, it's purpose is to include also the intermediate nodes in the result.

    HTH