Optimize SPARQL query for single entity metadata using wikibase:label service

- Use SERVICE wikibase:label for efficient retrieval of labels and descriptions in preferred language
- Remove redundant label/description fetching logic
- Prevent Cartesian product and improve query performance for
This commit is contained in:
Sonal Yadav 2025-07-15 19:54:50 +05:30
parent 7500b6d374
commit 7aeb757fca
2 changed files with 22 additions and 46 deletions

View file

@ -7,6 +7,7 @@ class NearbyResultItem(
private val wikipediaArticle: ResultTuple?, private val wikipediaArticle: ResultTuple?,
private val commonsArticle: ResultTuple?, private val commonsArticle: ResultTuple?,
private val location: ResultTuple?, private val location: ResultTuple?,
@field:SerializedName("itemLabel")
private val label: ResultTuple?, private val label: ResultTuple?,
@field:SerializedName("streetAddress") private val address: ResultTuple?, @field:SerializedName("streetAddress") private val address: ResultTuple?,
private val icon: ResultTuple?, private val icon: ResultTuple?,
@ -15,7 +16,7 @@ class NearbyResultItem(
@field:SerializedName("commonsCategory") private val commonsCategory: ResultTuple?, @field:SerializedName("commonsCategory") private val commonsCategory: ResultTuple?,
@field:SerializedName("pic") private val pic: ResultTuple?, @field:SerializedName("pic") private val pic: ResultTuple?,
@field:SerializedName("destroyed") private val destroyed: ResultTuple?, @field:SerializedName("destroyed") private val destroyed: ResultTuple?,
@field:SerializedName("description") private val description: ResultTuple?, @field:SerializedName("itemDescription") private val description: ResultTuple?,
@field:SerializedName("endTime") private val endTime: ResultTuple?, @field:SerializedName("endTime") private val endTime: ResultTuple?,
@field:SerializedName("monument") private val monument: ResultTuple?, @field:SerializedName("monument") private val monument: ResultTuple?,
@field:SerializedName("dateOfOfficialClosure") private val dateOfOfficialClosure: ResultTuple?, @field:SerializedName("dateOfOfficialClosure") private val dateOfOfficialClosure: ResultTuple?,

View file

@ -1,45 +1,26 @@
SELECT SELECT
?item ?item
(SAMPLE(?label) AS ?label) ?itemLabel
(SAMPLE(?class) AS ?class) ?itemDescription
(SAMPLE(?description) AS ?description) ?class
(SAMPLE(?classLabel) AS ?classLabel) ?classLabel
(SAMPLE(?pic) AS ?pic) ?pic
(SAMPLE(?destroyed) AS ?destroyed) ?destroyed
(SAMPLE(?endTime) AS ?endTime) ?endTime
(SAMPLE(?wikipediaArticle) AS ?wikipediaArticle) ?wikipediaArticle
(SAMPLE(?commonsArticle) AS ?commonsArticle) ?commonsArticle
(SAMPLE(?commonsCategory) AS ?commonsCategory) ?commonsCategory
(SAMPLE(?dateOfOfficialClosure) AS ?dateOfOfficialClosure) ?dateOfOfficialClosure
(SAMPLE(?pointInTime) AS ?pointInTime) ?pointInTime
WHERE { WHERE {
SERVICE <https://query.wikidata.org/sparql> { SERVICE <https://query.wikidata.org/sparql> {
values ?item { VALUES ?item { ${ENTITY} }
${ENTITY}
}
} }
# Get the label in the preferred language of the user, or any other language if no label is available in that language. # Get item label/class label/description in the preferred language of the user
OPTIONAL {?item rdfs:label ?itemLabelPreferredLanguage. FILTER (lang(?itemLabelPreferredLanguage) = "${LANG}")} SERVICE wikibase:label { bd:serviceParam wikibase:language "${LANG},en". }
OPTIONAL {?item rdfs:label ?itemLabelAnyLanguage}
BIND(COALESCE(?itemLabelPreferredLanguage, ?itemLabelAnyLanguage, "?") as ?label)
# Get the description in the preferred language of the user, or any other language if no description is available in that language. OPTIONAL { ?item p:P31/ps:P31 ?class. }
OPTIONAL {?item schema:description ?itemDescriptionPreferredLanguage. FILTER (lang(?itemDescriptionPreferredLanguage) = "${LANG}")}
OPTIONAL {?item schema:description ?itemDescriptionAnyLanguage}
BIND(COALESCE(?itemDescriptionPreferredLanguage, ?itemDescriptionAnyLanguage, "?") as ?description)
# Get the class label in the preferred language of the user, or any other language if no label is available in that language.
OPTIONAL {
?item p:P31/ps:P31 ?class.
OPTIONAL {?class rdfs:label ?classLabelPreferredLanguage. FILTER (lang(?classLabelPreferredLanguage) = "${LANG}")}
OPTIONAL {?class rdfs:label ?classLabelAnyLanguage}
BIND(COALESCE(?classLabelPreferredLanguage, ?classLabelAnyLanguage, "?") as ?classLabel)
}
OPTIONAL {
?item p:P31/ps:P31 ?class.
}
# Get picture # Get picture
OPTIONAL {?item wdt:P18 ?pic} OPTIONAL {?item wdt:P18 ?pic}
@ -54,15 +35,9 @@ WHERE {
OPTIONAL {?item wdt:P373 ?commonsCategory} OPTIONAL {?item wdt:P373 ?commonsCategory}
# Get Wikipedia article # Get Wikipedia article
OPTIONAL { OPTIONAL { ?wikipediaArticle schema:about ?item;schema:isPartOf <https://en.wikipedia.org/>. }
?wikipediaArticle schema:about ?item.
?wikipediaArticle schema:isPartOf <https://en.wikipedia.org/>. # TODO internationalization
}
# Get Commons article # Get Commons article
OPTIONAL { OPTIONAL { ?commonsArticle schema:about ?item; schema:isPartOf <https://commons.wikimedia.org/>. }
?commonsArticle schema:about ?item.
?commonsArticle schema:isPartOf <https://commons.wikimedia.org/>.
}
} }
GROUP BY ?item