Skip to content

Commit 41ae107

Browse files
committed
Optimize smells filters queries
1 parent dca9b41 commit 41ae107

File tree

1 file changed

+112
-74
lines changed

1 file changed

+112
-74
lines changed

config/routes/smells.js

Lines changed: 112 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -653,9 +653,11 @@ module.exports = {
653653
],
654654
$where: [
655655
`
656-
?emission od:F1_generated ?id .
657-
?emission od:F3_had_source ?source .
658-
?source skos:inScheme [] .
656+
FILTER EXISTS {
657+
?emission od:F1_generated [] .
658+
?emission od:F3_had_source ?source .
659+
?source skos:inScheme [] .
660+
}
659661
{
660662
?source skos:prefLabel ?sourceLabel .
661663
FILTER(LANG(?sourceLabel) = "${language}" || LANG(?sourceLabel) = "")
@@ -671,11 +673,11 @@ module.exports = {
671673
],
672674
$langTag: 'hide',
673675
}),
674-
whereFunc: (_val, index) => [
676+
whereFunc: (val, index) => [
675677
`
676678
?emission od:F1_generated ?id .
677679
?emission od:F3_had_source / skos:broader* ?source_${index} .
678-
VALUES ?source_${index} { <${_val}> }
680+
VALUES ?source_${index} { <${val}> }
679681
`,
680682
],
681683
},
@@ -692,16 +694,21 @@ module.exports = {
692694
],
693695
$where: [
694696
`
695-
?emission od:F4_had_carrier ?carrier .
696-
olfactory-objects:carrier skos:member ?carrier .
697+
[] od:F4_had_carrier ?carrier .
698+
FILTER EXISTS { olfactory-objects:carrier skos:member ?carrier }
697699
?carrier skos:prefLabel ?carrierLabel .
698700
FILTER(LANG(?carrierLabel) = "${language}" || LANG(?carrierLabel) = "")
699701
`,
700702
],
701703
$langTag: 'hide',
702704
}),
703-
whereFunc: () => ['?emission od:F1_generated ?id', '?emission od:F4_had_carrier ?carrier'],
704-
filterFunc: (val) => `?carrier = <${val}>`,
705+
whereFunc: (val, index) => [
706+
`
707+
?emission od:F1_generated ?id .
708+
?emission od:F4_had_carrier ?carrier_${index} .
709+
VALUES ?carrier_${index} { <${val}> }
710+
`,
711+
],
705712
},
706713
{
707714
id: 'quality',
@@ -716,23 +723,23 @@ module.exports = {
716723
],
717724
$where: [
718725
`
719-
?assignment crm:P140_assigned_attribute_to ?id .
720-
?assignment crm:P141_assigned ?quality .
721-
?quality skos:inScheme [] .
722-
OPTIONAL { ?quality skos:prefLabel ?label_hl . FILTER(LANGMATCHES(LANG(?label_hl), "${language}")) }
723-
OPTIONAL { ?quality skos:prefLabel ?label_en . FILTER(LANGMATCHES(LANG(?label_en), "en")) }
724-
OPTIONAL { ?quality rdfs:label ?original_label . }
725-
BIND(COALESCE(?label_hl, ?label_en, ?original_label) AS ?qualityLabel)
726+
FILTER EXISTS {
727+
?quality ^crm:P141_assigned / crm:P140_assigned_attribute_to ?id .
728+
?quality skos:inScheme [] .
729+
}
730+
OPTIONAL { ?quality skos:prefLabel ?qualityLabel FILTER(LANGMATCHES(LANG(?qualityLabel), "${language}")) }
731+
OPTIONAL { ?quality skos:prefLabel ?qualityLabel FILTER(LANGMATCHES(LANG(?qualityLabel), "en")) }
732+
OPTIONAL { ?quality skos:prefLabel ?qualityLabel }
726733
`,
727734
],
728735
$langTag: 'hide',
729736
}),
730-
whereFunc: () => [
731-
'?assignment crm:P140_assigned_attribute_to ?id',
732-
'?assignment crm:P141_assigned ?quality',
733-
'?quality skos:inScheme []',
737+
whereFunc: (val, index) => [
738+
`
739+
?id ^crm:P140_assigned_attribute_to / crm:P141_assigned ?quality_${index} .
740+
VALUES ?quality_${index} { <${val}> }
741+
`,
734742
],
735-
filterFunc: (val) => `?quality = <${val}>`,
736743
},
737744
{
738745
id: 'emotion',
@@ -747,16 +754,20 @@ module.exports = {
747754
],
748755
$where: [
749756
`
750-
?emotion reo:readP27 ?experience .
751-
OPTIONAL { ?emotion skos:prefLabel ?emotionLabel . FILTER(LANGMATCHES(LANG(?emotionLabel), "${language}")) }
752-
OPTIONAL { ?emotion skos:prefLabel ?emotionLabel . FILTER(LANG(?emotionLabel) = "en") }
753-
OPTIONAL { ?emotion rdfs:label ?emotionLabel . }
757+
FILTER EXISTS { ?emotion reo:readP27 [] }
758+
OPTIONAL { ?emotion skos:prefLabel ?emotionLabel FILTER(LANGMATCHES(LANG(?emotionLabel), "${language}")) }
759+
OPTIONAL { ?emotion skos:prefLabel ?emotionLabel FILTER(LANGMATCHES(LANG(?emotionLabel), "en")) }
760+
OPTIONAL { ?emotion skos:prefLabel ?emotionLabel }
754761
`,
755762
],
756763
$langTag: 'hide',
757764
}),
758-
whereFunc: () => ['?experience od:F2_perceived ?id', '?emotion reo:readP27 ?experience'],
759-
filterFunc: (val) => `?emotion = <${val}>`,
765+
whereFunc: (val, index) => [
766+
`
767+
?emotion_${index} reo:readP27 / od:F2_perceived ?id .
768+
VALUES ?emotion_${index} { <${val}> }
769+
`,
770+
],
760771
},
761772
{
762773
id: 'language',
@@ -771,16 +782,17 @@ module.exports = {
771782
],
772783
$where: [
773784
`
774-
?source schema:inLanguage ?language .
785+
[] schema:inLanguage ?language .
775786
`,
776787
],
777788
$langTag: 'hide',
778789
}),
779-
whereFunc: () => [
780-
'?textualObject crm:P67_refers_to ?id',
781-
'?textualObject schema:inLanguage ?language',
790+
whereFunc: (val) => [
791+
`
792+
?id ^crm:P67_refers_to / schema:inLanguage ?language .
793+
VALUES ?language { ${JSON.stringify(val)} }
794+
`,
782795
],
783-
filterFunc: (val) => `STR(?language) = ${JSON.stringify(val)}`,
784796
},
785797
{
786798
id: 'time',
@@ -803,11 +815,13 @@ module.exports = {
803815
],
804816
$where: [
805817
`
806-
?id a od:L11_Smell .
807-
?emission od:F1_generated ?id .
808-
?emission od:F3_had_source ?source .
809818
?emission time:hasTime ?time .
810819
?time time:hasBeginning ?timeBegin .
820+
FILTER EXISTS {
821+
?id a od:L11_Smell .
822+
?emission od:F1_generated ?id .
823+
?emission od:F3_had_source ?source .
824+
}
811825
`,
812826
],
813827
$langTag: 'hide',
@@ -836,11 +850,13 @@ module.exports = {
836850
],
837851
$where: [
838852
`
839-
?id a od:L11_Smell .
840-
?emission od:F1_generated ?id .
841-
?emission od:F3_had_source ?source .
842853
?emission time:hasTime ?time .
843854
?time time:hasEnd ?timeEnd .
855+
FILTER EXISTS {
856+
?id a od:L11_Smell .
857+
?emission od:F1_generated ?id .
858+
?emission od:F3_had_source ?source .
859+
}
844860
`,
845861
],
846862
$langTag: 'hide',
@@ -866,10 +882,16 @@ module.exports = {
866882
{ label: '20th century', value: 1900, key: 'twentieth-century' },
867883
],
868884
whereFunc: () => [
869-
'?emission od:F1_generated ?id',
870-
'?emission time:hasTime ?time',
871-
'?time time:hasBeginning ?timeBegin',
872-
'?time time:hasEnd ?timeEnd',
885+
`
886+
?emission time:hasTime ?time .
887+
?time time:hasBeginning ?timeBegin .
888+
?time time:hasEnd ?timeEnd .
889+
FILTER EXISTS {
890+
?id a od:L11_Smell .
891+
?emission od:F1_generated ?id .
892+
?emission od:F3_had_source ?source .
893+
}
894+
`,
873895
],
874896
filterFunc: (val) =>
875897
`?timeBegin >= ${JSON.stringify(
@@ -890,9 +912,15 @@ module.exports = {
890912
{ label: 'Autumn', value: 'autumn' },
891913
],
892914
whereFunc: () => [
893-
'?emission od:F1_generated ?id',
894-
'?emission time:hasTime ?time',
895-
'?time time:hasBeginning ?timeBegin',
915+
`
916+
?time time:hasBeginning ?timeBegin .
917+
FILTER EXISTS {
918+
?id a od:L11_Smell .
919+
?emission od:F1_generated ?id .
920+
?emission od:F3_had_source ?source .
921+
?emission time:hasTime ?time .
922+
}
923+
`,
896924
],
897925
filterFunc: (val) => {
898926
if (val === 'winter') return `MONTH(?timeBegin) IN (12, 1, 2)`; // December, January, February
@@ -906,7 +934,7 @@ module.exports = {
906934
id: 'place',
907935
isMulti: true,
908936
isSortable: false,
909-
query: ({ language }) => ({
937+
query: () => ({
910938
'@graph': [
911939
{
912940
'@id': '?country',
@@ -915,19 +943,22 @@ module.exports = {
915943
],
916944
$where: [
917945
`
918-
?id a od:L11_Smell .
919-
?experience od:F2_perceived ?id .
946+
FILTER EXISTS {
947+
?id a od:L11_Smell .
948+
?experience od:F2_perceived ?id .
949+
}
920950
?experience crm:P7_took_place_at / gn:parentCountry ?country .
921951
?country gn:name ?countryName .
922952
`,
923953
],
924954
$langTag: 'hide',
925955
}),
926-
whereFunc: () => [
927-
'?experience od:F2_perceived ?id',
928-
'?experience crm:P7_took_place_at / gn:parentCountry ?placeCountry',
956+
whereFunc: (val, index) => [
957+
`
958+
?placeCountry_${index} ^gn:parentCountry / ^ crm:P7_took_place_at / od:F2_perceived ?id .
959+
VALUES ?placeCountry_${index} { <${val}> }
960+
`,
929961
],
930-
filterFunc: (val) => `?placeCountry = <${val}>`,
931962
},
932963
{
933964
id: 'museum',
@@ -941,17 +972,22 @@ module.exports = {
941972
},
942973
],
943974
$where: [
944-
'?id crm:P53_has_former_or_current_location ?location',
945-
'?location a gn:Feature',
946-
'?location gn:name ?locationName',
975+
`
976+
FILTER EXISTS {
977+
?id crm:P53_has_former_or_current_location ?location .
978+
?location a gn:Feature
979+
}
980+
?location gn:name ?locationName .
981+
`,
947982
],
948983
$langTag: 'hide',
949984
}),
950-
whereFunc: () => [
951-
'?source crm:P67_refers_to ?id',
952-
'?source crm:P53_has_former_or_current_location ?location',
985+
whereFunc: (val, index) => [
986+
`
987+
?location_${index} ^crm:P53_has_former_or_current_location / crm:P67_refers_to ?id .
988+
VALUES ?location_${index} { <${val}> }
989+
`,
953990
],
954-
filterFunc: (val) => `?location = <${val}>`,
955991
},
956992
{
957993
id: 'graphs',
@@ -968,7 +1004,11 @@ module.exports = {
9681004
$orderby: 'ASC(?label)',
9691005
$langTag: 'hide',
9701006
}),
971-
filterFunc: (val) => `?g = <${val}>`,
1007+
whereFunc: (val) => [
1008+
`
1009+
VALUES ?g { <${val}> }
1010+
`,
1011+
],
9721012
},
9731013
{
9741014
id: 'space',
@@ -985,11 +1025,9 @@ module.exports = {
9851025
$where: [
9861026
`
9871027
?space skos:inScheme <http://data.odeuropa.eu/vocabulary/fragrant-spaces> .
988-
OPTIONAL { ?space skos:prefLabel ?label_hl . FILTER(LANGMATCHES(LANG(?label_hl), "${language}")) }
989-
OPTIONAL { ?space skos:prefLabel ?label_en . FILTER(LANGMATCHES(LANG(?label_en), "en")) }
990-
OPTIONAL { ?space rdfs:label ?original_label . }
991-
BIND(COALESCE(?label_hl, ?label_en, ?original_label) AS ?spaceLabel)
992-
FILTER(BOUND(?spaceLabel))
1028+
OPTIONAL { ?space skos:prefLabel ?spaceLabel FILTER(LANGMATCHES(LANG(?spaceLabel), "${language}")) }
1029+
OPTIONAL { ?space skos:prefLabel ?spaceLabel FILTER(LANGMATCHES(LANG(?spaceLabel), "en")) }
1030+
OPTIONAL { ?space skos:prefLabel ?spaceLabel }
9931031
`,
9941032
],
9951033
$langTag: 'hide',
@@ -1015,20 +1053,20 @@ module.exports = {
10151053
$where: [
10161054
`
10171055
?gesture skos:inScheme <http://data.odeuropa.eu/vocabulary/olfactory-gestures> .
1018-
OPTIONAL { ?gesture skos:prefLabel ?label_hl . FILTER(LANGMATCHES(LANG(?label_hl), "${language}")) }
1019-
OPTIONAL { ?gesture skos:prefLabel ?label_en . FILTER(LANGMATCHES(LANG(?label_en), "en")) }
1020-
OPTIONAL { ?gesture rdfs:label ?original_label . }
1021-
BIND(COALESCE(?label_hl, ?label_en, ?original_label) AS ?gestureLabel)
1022-
FILTER(BOUND(?gestureLabel))
1056+
OPTIONAL { ?gesture skos:prefLabel ?gestureLabel FILTER(LANGMATCHES(LANG(?gestureLabel), "${language}")) }
1057+
OPTIONAL { ?gesture skos:prefLabel ?gestureLabel FILTER(LANGMATCHES(LANG(?gestureLabel), "en")) }
1058+
OPTIONAL { ?gesture skos:prefLabel ?gestureLabel }
10231059
`,
10241060
],
10251061
$langTag: 'hide',
10261062
}),
1027-
whereFunc: () => [
1028-
'?experience od:F2_perceived ?id',
1029-
'?experience od:F5_involved_gesture ?gesture',
1063+
whereFunc: (val) => [
1064+
`
1065+
?experience od:F2_perceived ?id .
1066+
?experience od:F5_involved_gesture ?gesture_${index} .
1067+
VALUES ?gesture_${index} { <${val}> }
1068+
`,
10301069
],
1031-
filterFunc: (val) => `?gesture = <${val}>`,
10321070
},
10331071
],
10341072
};

0 commit comments

Comments
 (0)