Skip to content

Commit e5dbcfc

Browse files
sonalyadav1Sonal Yadavnicolas-raoul
authored
Optimise SPARQL query for single entity metadata using wikibase:label (#6376)
* 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 * appeded all possible Wikidata languages * Remove duplicate 'en' * Update query_for_item.rq * formatting, comments --------- Co-authored-by: Sonal Yadav <sonalyadav@Sonals-MacBook-Air.local> Co-authored-by: Nicolas Raoul <nicolas.raoul@gmail.com>
1 parent 0cda8e4 commit e5dbcfc

File tree

2 files changed

+21
-39
lines changed

2 files changed

+21
-39
lines changed

app/src/main/java/fr/free/nrw/commons/nearby/model/NearbyResultItem.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class NearbyResultItem(
77
private val wikipediaArticle: ResultTuple?,
88
private val commonsArticle: ResultTuple?,
99
private val location: ResultTuple?,
10+
@field:SerializedName("itemLabel")
1011
private val label: ResultTuple?,
1112
@field:SerializedName("streetAddress") private val address: ResultTuple?,
1213
private val icon: ResultTuple?,
@@ -15,7 +16,7 @@ class NearbyResultItem(
1516
@field:SerializedName("commonsCategory") private val commonsCategory: ResultTuple?,
1617
@field:SerializedName("pic") private val pic: ResultTuple?,
1718
@field:SerializedName("destroyed") private val destroyed: ResultTuple?,
18-
@field:SerializedName("description") private val description: ResultTuple?,
19+
@field:SerializedName("itemDescription") private val description: ResultTuple?,
1920
@field:SerializedName("endTime") private val endTime: ResultTuple?,
2021
@field:SerializedName("monument") private val monument: ResultTuple?,
2122
@field:SerializedName("dateOfOfficialClosure") private val dateOfOfficialClosure: ResultTuple?,
Lines changed: 19 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,32 @@
11
SELECT
22
?item
3-
(SAMPLE(?label) AS ?label)
4-
(SAMPLE(?class) AS ?class)
5-
(SAMPLE(?description) AS ?description)
6-
(SAMPLE(?classLabel) AS ?classLabel)
7-
(SAMPLE(?pic) AS ?pic)
8-
(SAMPLE(?destroyed) AS ?destroyed)
9-
(SAMPLE(?endTime) AS ?endTime)
10-
(SAMPLE(?wikipediaArticle) AS ?wikipediaArticle)
11-
(SAMPLE(?commonsArticle) AS ?commonsArticle)
12-
(SAMPLE(?commonsCategory) AS ?commonsCategory)
13-
(SAMPLE(?dateOfOfficialClosure) AS ?dateOfOfficialClosure)
14-
(SAMPLE(?pointInTime) AS ?pointInTime)
3+
?itemLabel
4+
?itemDescription
5+
?class
6+
?classLabel
7+
?pic
8+
?destroyed
9+
?endTime
10+
?wikipediaArticle
11+
?commonsArticle
12+
?commonsCategory
13+
?dateOfOfficialClosure
14+
?pointInTime
1515
WHERE {
1616
SERVICE <https://query.wikidata.org/sparql> {
17-
values ?item {
18-
${ENTITY}
19-
}
17+
VALUES ?item {${ENTITY}}
2018
}
2119

22-
# Get the label in the preferred language of the user, or any other language if no label is available in that language.
23-
OPTIONAL {?item rdfs:label ?itemLabelPreferredLanguage. FILTER (lang(?itemLabelPreferredLanguage) = "${LANG}")}
24-
OPTIONAL {?item rdfs:label ?itemLabelAnyLanguage}
25-
BIND(COALESCE(?itemLabelPreferredLanguage, ?itemLabelAnyLanguage, "?") as ?label)
20+
# Get item label/class label/description in the preferred language of the user, or fallback.
21+
SERVICE wikibase:label { bd:serviceParam wikibase:language "${LANG},en,aa,ab,ae,af,ak,am,an,ar,as,av,ay,az,ba,be,bg,bh,bi,bm,bn,bo,br,bs,ca,ce,ch,co,cr,cs,cu,cv,cy,da,de,dv,dz,ee,el,eo,es,et,eu,fa,ff,fi,fj,fo,fr,fy,ga,gd,gl,gn,gu,gv,ha,he,hi,ho,hr,ht,hu,hy,hz,ia,id,ie,ig,ii,ik,io,is,it,iu,ja,jv,ka,kg,ki,kj,kk,kl,km,kn,ko,kr,ks,ku,kv,kw,ky,la,lb,lg,li,ln,lo,lt,lu,lv,mg,mh,mi,mk,ml,mn,mo,mr,ms,mt,my,na,nb,nd,ne,ng,nl,nn,no,ny,oc,oj,om,or,os,pa,pi,pl,ps,pt,qu,rm,rn,ro,ru,rw,sa,sc,sd,se,sg,sh,si,sk,sl,sm,sn,so,sq,sr,ss,st,su,sv,sw,ta,te,tg,th,ti,tk,tl,tn,to,tr,ts,tt,tw,ty,ug,uk,ur,uz,ve,vi,vo,wa,wo,xh,yi,yo,za,zh,zu". }
2622

27-
# Get the description in the preferred language of the user, or any other language if no description is available in that language.
28-
OPTIONAL {?item schema:description ?itemDescriptionPreferredLanguage. FILTER (lang(?itemDescriptionPreferredLanguage) = "${LANG}")}
29-
OPTIONAL {?item schema:description ?itemDescriptionAnyLanguage}
30-
BIND(COALESCE(?itemDescriptionPreferredLanguage, ?itemDescriptionAnyLanguage, "?") as ?description)
23+
# Get class (such as forest or bridge)
24+
OPTIONAL {?item p:P31/ps:P31 ?class}
3125

32-
# Get the class label in the preferred language of the user, or any other language if no label is available in that language.
33-
OPTIONAL {
34-
?item p:P31/ps:P31 ?class.
35-
OPTIONAL {?class rdfs:label ?classLabelPreferredLanguage. FILTER (lang(?classLabelPreferredLanguage) = "${LANG}")}
36-
OPTIONAL {?class rdfs:label ?classLabelAnyLanguage}
37-
BIND(COALESCE(?classLabelPreferredLanguage, ?classLabelAnyLanguage, "?") as ?classLabel)
38-
}
39-
40-
OPTIONAL {
41-
?item p:P31/ps:P31 ?class.
42-
}
43-
44-
# Get picture
26+
# Get picture (items without a picture will be shown in red on the Nearby map)
4527
OPTIONAL {?item wdt:P18 ?pic}
4628

47-
# Get existence
29+
# Get existence (whether an item still exists or not)
4830
OPTIONAL {?item wdt:P576 ?destroyed}
4931
OPTIONAL {?item wdt:P582 ?endTime}
5032
OPTIONAL {?item wdt:P3999 ?dateOfOfficialClosure}
@@ -65,4 +47,3 @@ WHERE {
6547
?commonsArticle schema:isPartOf <https://commons.wikimedia.org/>.
6648
}
6749
}
68-
GROUP BY ?item

0 commit comments

Comments
 (0)