/
searchExtendedGraphDB.scala.txt
658 lines (419 loc) · 24.9 KB
/
searchExtendedGraphDB.scala.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
@*
* Copyright © 2015-2021 the contributors (see Contributors.md).
*
* This file is part of Knora.
*
* Knora is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Knora is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with Knora. If not, see <http://www.gnu.org/licenses/>.
*@
@import org.knora.webapi.IRI
@import org.knora.webapi.exceptions.SparqlGenerationException
@import org.knora.webapi.messages.twirl.SearchCriterion
@import org.knora.webapi.messages.v1.responder.searchmessages.SearchComparisonOperatorV1
@**
* Performs an extended search using GraphDB. The number of rows returned per matching resource is the product of the
* number of values that each search criterion matched in the resource.
*
* This template is used only by searchExtended.scala.txt.
*
* Since the triplestore type is GraphDB, we assume that inference is enabled, and we use it to optimise the generated
* SPARQL. Specifically, we use inference to return search results matching subclasses and subproperties of the ones
* specified in the search criteria. This inference requires us to use GraphDB's GRAPH
* <http://www.ontotext.com/explicit> whenever we need to get explicit (non-inferred) statements.
*
* @param searchCriteria a list of maps containing search criteria, each of which contains:
* @param preferredLanguage the language code of the user's preferred language.
* @param fallbackLanguage the language code of the application's default language.
* @param projectIriOption if filtering by project is desired, the IRI of the project to search.
* @param restypeIriOption if given, returns only resources of this class and its subclasses.
* @param ownerIriOption if filtering by owner is desired, the IRI of the resource owner to search for.
*@
@(searchCriteria: Seq[SearchCriterion],
preferredLanguage: String,
fallbackLanguage: String,
projectIriOption: Option[IRI],
restypeIriOption: Option[IRI],
ownerIriOption: Option[IRI])
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix knora-base: <http://www.knora.org/ontology/knora-base#>
prefix xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT DISTINCT
?resource
?resourceLabel @* The label of a matching resource. This is called "firstprop" in the v1 API. *@
?resourceClass
?previewPath
?previewDimX
?previewDimY
?resourceCreator
?resourceProject
?resourcePermissions
@for((searchCriterion, index) <- searchCriteria.zipWithIndex) {
?valueObject@index
?valueCreator@index
?valuePermissions@index
?targetResource@index
?targetResourceCreator@index
?targetResourceProject@index
?targetResourcePermissions@index
?literal@index
}
WHERE {
BIND(STR("@preferredLanguage") AS ?preferredLanguage)
BIND(STR("@fallbackLanguage") AS ?fallbackLanguage)
@for((searchCriterion, index) <- searchCriteria.zipWithIndex) {
# Search criterion @index: operator @searchCriterion.comparisonOperator
@*
If this search criterion involves a property that points to a knora-base:Value, find a resource that has
that property pointing to some value. If this search criterion involves a link property, the property will
point to another resource, and this is handled in the cases for comparison operators EQ and EXISTS in the
match-case expression below.
*@
@if(searchCriterion.valueType != "http://www.knora.org/ontology/knora-base#Resource") {
?resource <@searchCriterion.propertyIri> ?valueObject@index .
}
@*
possible values of searchCriterion.comparisonOperator: EXISTS, EQ, NOT_EQ, GT, GT_EQ, LT, LT_EQ, MATCH, MATCH_BOOLEAN, LIKE, NOT_LIKE, IN
*@
@searchCriterion.comparisonOperator match {
case SearchComparisonOperatorV1.MATCH => {
?valueObject@index knora-base:valueHasString ?literal@index .
?literal@index <http://www.ontotext.com/owlim/lucene#fullTextSearchIndex> '@searchCriterion.searchValue' .
}
case SearchComparisonOperatorV1.LIKE => {
@* TODO: escape regex metachars *@
?valueObject@index knora-base:valueHasString ?literal@index .
FILTER regex(?literal@index, '@searchCriterion.searchValue', "i")
}
case SearchComparisonOperatorV1.NOT_LIKE => {
@*
Previously we used FILTER NOT EXISTS here, but then we couldn't get the value of ?literal
(the literal that doesn't match the regex). So now we use just a negated regex. However, if a
resource has two instances of the same property and one of them matches and the other doesn't,
it will be returned in the search results.
*@
?valueObject@index knora-base:valueHasString ?literal@index .
FILTER (!regex(?literal@index, '@searchCriterion.searchValue', "i"))
}
case SearchComparisonOperatorV1.EXISTS => {
@searchCriterion.valueType match {
case "http://www.knora.org/ontology/knora-base#Resource" => {
?resource <@searchCriterion.propertyIri> ?targetResource@index .
?resource <@{searchCriterion.propertyIri}Value> ?valueObject@index .
GRAPH <http://www.ontotext.com/explicit> {
?resource ?linkProperty@index ?targetResource@index .
}
?targetResource@index rdfs:label ?literal@index ;
knora-base:isDeleted false ;
knora-base:attachedToUser ?targetResourceCreator@index ;
knora-base:attachedToProject ?targetResourceProject@index ;
knora-base:hasPermissions ?targetResourcePermissions@index .
?valueObject@index rdf:type knora-base:LinkValue ;
rdf:subject ?resource ;
rdf:predicate ?linkProperty@index ;
rdf:object ?targetResource@index .
}
case other => {
?valueObject@index knora-base:valueHasString ?literal@index .
}
}
}
case SearchComparisonOperatorV1.EQ => {
@searchCriterion.valueType match {
case "http://www.knora.org/ontology/knora-base#TextValue" => {
?valueObject@index knora-base:valueHasString '@searchCriterion.searchValue' .
BIND('@searchCriterion.searchValue' AS ?literal@index)
}
case "http://www.knora.org/ontology/knora-base#IntValue" => {
?valueObject@index knora-base:valueHasInteger @searchCriterion.searchValue .
BIND('@searchCriterion.searchValue' AS ?literal@index)
}
case "http://www.knora.org/ontology/knora-base#DecimalValue" => {
?valueObject@index knora-base:valueHasDecimal "@searchCriterion.searchValue"^^xsd:decimal .
BIND('@searchCriterion.searchValue' AS ?literal@index)
}
case "http://www.knora.org/ontology/knora-base#TimeValue" => {
?valueObject@index knora-base:valueHasTimeStamp "@searchCriterion.searchValue"^^xsd:dateTime .
BIND('@searchCriterion.searchValue' AS ?literal@index)
}
case "http://www.knora.org/ontology/knora-base#DateValue" => {
?valueObject@index knora-base:valueHasStartJDN ?dateStart@index .
?valueObject@index knora-base:valueHasEndJDN ?dateEnd@index .
?valueObject@index knora-base:valueHasString ?dateString@index .
@*
This condition checks whether the defined criterion for a date value and a resource's date value overlap.
Since dates are represented as periods, equality is given if a date value's period ends after or equals the start of the defined period (search criterion date start) and
a date value's period starts before or equals the end of the defined period (search criterion date end).
Formally, equality is the negation of inequality of two given dates. Inequality (see below) is given if a date value's period starts after the end of the defined period (search criterion date end) or
ends before the start of the defined period (search criterion date start).
Negation of the filter expression for inequality:
FILTER (!(@searchCriterion.dateStart > ?dateEnd@index || @searchCriterion.dateEnd < ?dateStart@index))
Following De Morgan's laws, the expression can be rewritten by negating both parts of the expression and combining them with a logical AND:
FILTER(!(@searchCriterion.dateStart > ?dateEnd@index) && !(@searchCriterion.dateEnd < ?dateStart@index))
which can be then further simplified to the expression stated below.
*@
FILTER (@searchCriterion.dateStart <= ?dateEnd@index && @searchCriterion.dateEnd >= ?dateStart@index)
BIND(?dateString@index as ?literal@index)
}
case "http://www.knora.org/ontology/knora-base#Resource" => {
BIND(IRI("@searchCriterion.searchValue") AS ?targetResource@index)
?resource <@searchCriterion.propertyIri> <@searchCriterion.searchValue> .
?resource <@{searchCriterion.propertyIri}Value> ?valueObject@index .
GRAPH <http://www.ontotext.com/explicit> {
?resource ?linkProperty@index <@searchCriterion.searchValue> .
}
<@searchCriterion.searchValue> rdfs:label ?literal@index ;
knora-base:isDeleted false ;
knora-base:attachedToUser ?targetResourceCreator@index ;
knora-base:attachedToProject ?targetResourceProject@index ;
knora-base:hasPermissions ?targetResourcePermissions@index .
?valueObject@index rdf:type knora-base:LinkValue ;
rdf:subject ?resource ;
rdf:predicate ?linkProperty@index ;
rdf:object <@searchCriterion.searchValue> .
}
case "http://www.knora.org/ontology/knora-base#ListValue" => {
@*
Get all the subnodes of the given node, including the given node (operator *)
*@
?valueObject@index knora-base:valueHasListNode ?listNode@index .
<@searchCriterion.searchValue> knora-base:hasSubListNode* ?listNode@index .
?listNode@index rdfs:label ?listNodeLabel@index .
BIND(?listNodeLabel@index AS ?literal@index)
}
case "http://www.knora.org/ontology/knora-base#BooleanValue" => {
?valueObject@index knora-base:valueHasBoolean @searchCriterion.searchValue .
BIND('@searchCriterion.searchValue' AS ?literal@index)
}
case "http://www.knora.org/ontology/knora-base#GeonameValue" => {
?valueObject@index knora-base:valueHasGeonameCode '@searchCriterion.searchValue' .
BIND('@searchCriterion.searchValue' AS ?literal@index)
}
case "http://www.knora.org/ontology/knora-base#UriValue" => {
?valueObject@index knora-base:valueHasUri '@searchCriterion.searchValue'^^xsd:anyURI .
BIND('@searchCriterion.searchValue' AS ?literal@index)
}
case other => {
@{throw SparqlGenerationException(s"Value type $other is not supported with comparison operator ${searchCriterion.comparisonOperator}"); ()}
}
}
}
case SearchComparisonOperatorV1.NOT_EQ => {
@searchCriterion.valueType match {
case "http://www.knora.org/ontology/knora-base#TextValue" => {
?valueObject@index knora-base:valueHasString ?literal@index .
FILTER (?literal@index != '@searchCriterion.searchValue')
}
case "http://www.knora.org/ontology/knora-base#IntValue" => {
?valueObject@index knora-base:valueHasInteger ?literal@index .
FILTER (?literal@index != @searchCriterion.searchValue)
}
case "http://www.knora.org/ontology/knora-base#DecimalValue" => {
?valueObject@index knora-base:valueHasDecimal ?literal@index .
FILTER (?literal@index != "@searchCriterion.searchValue"^^xsd:decimal)
}
case "http://www.knora.org/ontology/knora-base#TimeValue" => {
?valueObject@index knora-base:valueHasTimeStamp ?literal@index .
FILTER (?literal@index != "@searchCriterion.searchValue"^^xsd:dateTime)
}
case "http://www.knora.org/ontology/knora-base#DateValue" => {
?valueObject@index knora-base:valueHasStartJDN ?dateStart@index .
?valueObject@index knora-base:valueHasEndJDN ?dateEnd@index .
?valueObject@index knora-base:valueHasString ?dateString@index .
FILTER (@searchCriterion.dateEnd < ?dateStart@index || @searchCriterion.dateStart > ?dateEnd@index )
BIND(?dateString@index as ?literal@index)
}
case "http://www.knora.org/ontology/knora-base#BooleanValue" => {
?valueObject@index knora-base:valueHasBoolean ?literal@index .
FILTER (?literal@index != @searchCriterion.searchValue)
}
case other => {
@{throw SparqlGenerationException(s"Value type $other is not supported with comparison operator ${searchCriterion.comparisonOperator}"); ()}
}
}
}
case SearchComparisonOperatorV1.GT => {
@searchCriterion.valueType match {
case "http://www.knora.org/ontology/knora-base#IntValue" => {
?valueObject@index knora-base:valueHasInteger ?literal@index .
FILTER (?literal@index > @searchCriterion.searchValue)
}
case "http://www.knora.org/ontology/knora-base#DecimalValue" => {
?valueObject@index knora-base:valueHasDecimal ?literal@index .
FILTER (?literal@index > "@searchCriterion.searchValue"^^xsd:decimal )
}
case "http://www.knora.org/ontology/knora-base#TimeValue" => {
?valueObject@index knora-base:valueHasTimeStamp ?literal@index .
FILTER (?literal@index > "@searchCriterion.searchValue"^^xsd:dateTime )
}
case "http://www.knora.org/ontology/knora-base#DateValue" => {
?valueObject@index knora-base:valueHasStartJDN ?dateStart@index .
?valueObject@index knora-base:valueHasString ?dateString@index .
FILTER (?dateStart@index > @searchCriterion.dateEnd)
BIND(?dateString@index as ?literal@index)
}
case other => {
@{throw SparqlGenerationException(s"Value type $other is not supported with comparison operator ${searchCriterion.comparisonOperator}"); ()}
}
}
}
case SearchComparisonOperatorV1.GT_EQ => {
@searchCriterion.valueType match {
case "http://www.knora.org/ontology/knora-base#IntValue" => {
?valueObject@index knora-base:valueHasInteger ?literal@index .
FILTER (?literal@index >= @searchCriterion.searchValue)
}
case "http://www.knora.org/ontology/knora-base#DecimalValue" => {
?valueObject@index knora-base:valueHasDecimal ?literal@index .
FILTER (?literal@index >= "@searchCriterion.searchValue"^^xsd:decimal)
}
case "http://www.knora.org/ontology/knora-base#TimeValue" => {
?valueObject@index knora-base:valueHasTimeStamp ?literal@index .
FILTER (?literal@index >= "@searchCriterion.searchValue"^^xsd:dateTime)
}
case "http://www.knora.org/ontology/knora-base#DateValue" => {
?valueObject@index knora-base:valueHasEndJDN ?dateEnd@index .
?valueObject@index knora-base:valueHasString ?dateString@index .
@*
Logically, GT_EQ is the negation of LT (see below):
FILTER (!(?dateEnd@index < @searchCriterion.dateStart))
This can be further simplified to the expression stated below.
*@
FILTER(?dateEnd@index >= @searchCriterion.dateStart)
BIND(?dateString@index as ?literal@index)
}
case other => {
@{throw SparqlGenerationException(s"Value type $other is not supported with comparison operator ${searchCriterion.comparisonOperator}"); ()}
}
}
}
case SearchComparisonOperatorV1.LT => {
@searchCriterion.valueType match {
case "http://www.knora.org/ontology/knora-base#IntValue" => {
?valueObject@index knora-base:valueHasInteger ?literal@index .
FILTER (?literal@index < @searchCriterion.searchValue)
}
case "http://www.knora.org/ontology/knora-base#DecimalValue" => {
?valueObject@index knora-base:valueHasDecimal ?literal@index .
FILTER (?literal@index < "@searchCriterion.searchValue"^^xsd:decimal)
}
case "http://www.knora.org/ontology/knora-base#TimeValue" => {
?valueObject@index knora-base:valueHasTimeStamp ?literal@index .
FILTER (?literal@index < "@searchCriterion.searchValue"^^xsd:dateTime)
}
case "http://www.knora.org/ontology/knora-base#DateValue" => {
?valueObject@index knora-base:valueHasEndJDN ?dateEnd@index .
?valueObject@index knora-base:valueHasString ?dateString@index .
FILTER (?dateEnd@index < @searchCriterion.dateStart)
BIND(?dateString@index as ?literal@index)
}
case other => {
@{throw SparqlGenerationException(s"Value type $other is not supported with comparison operator ${searchCriterion.comparisonOperator}"); ()}
}
}
}
case SearchComparisonOperatorV1.LT_EQ => {
@searchCriterion.valueType match {
case "http://www.knora.org/ontology/knora-base#IntValue" => {
?valueObject@index knora-base:valueHasInteger ?literal@index .
FILTER (?literal@index <= @searchCriterion.searchValue)
}
case "http://www.knora.org/ontology/knora-base#DecimalValue" => {
?valueObject@index knora-base:valueHasDecimal ?literal@index .
FILTER (?literal@index <= "@searchCriterion.searchValue"^^xsd:decimal)
}
case "http://www.knora.org/ontology/knora-base#TimeValue" => {
?valueObject@index knora-base:valueHasTimeStamp ?literal@index .
FILTER (?literal@index <= "@searchCriterion.searchValue"^^xsd:dateTime)
}
case "http://www.knora.org/ontology/knora-base#DateValue" => {
?valueObject@index knora-base:valueHasStartJDN ?dateStart@index .
?valueObject@index knora-base:valueHasString ?dateString@index .
@*
Logically, LT_EQ is the negation of GT (see above):
FILTER (!(?dateStart@index > @searchCriterion.dateEnd))
This can be further simplified to the expression stated below.
*@
FILTER (?dateStart@index <= @searchCriterion.dateEnd)
BIND(?dateString@index as ?literal@index)
}
case other => {
@{throw SparqlGenerationException(s"Value type $other is not supported with comparison operator ${searchCriterion.comparisonOperator}"); ()}
}
}
}
case SearchComparisonOperatorV1.MATCH_BOOLEAN => {
@* see https://dev.mysql.com/doc/refman/5.5/en/fulltext-boolean.html *@
?valueObject@index knora-base:valueHasString ?literal@index .
@for(positiveTerm <- searchCriterion.matchBooleanPositiveTerms) {
?literal@index <http://www.ontotext.com/owlim/lucene#fullTextSearchIndex> '@positiveTerm' .
}
@if(searchCriterion.matchBooleanNegativeTerms.nonEmpty) {
FILTER NOT EXISTS {
@for(negativeTerm <- searchCriterion.matchBooleanNegativeTerms) {
?literal@index <http://www.ontotext.com/owlim/lucene#fullTextSearchIndex> '@negativeTerm' .
}
}
}
}
case other => {
@{throw SparqlGenerationException(s"Comparison operator $other is not supported in this SPARQL template"); ()}
}
}
?valueObject@index knora-base:isDeleted false ;
knora-base:attachedToUser ?valueCreator@index ;
knora-base:hasPermissions ?valuePermissions@index .
}
?resource knora-base:isDeleted false .
@projectIriOption match {
case Some(projectIri) => {
# filter by projectIri
?resource knora-base:attachedToProject <@projectIri> .
}
case None => {}
}
GRAPH <http://www.ontotext.com/explicit> {
?resource a ?resourceClass .
}
@restypeIriOption match {
case Some(restypeIri) => {
# Filter by resource class.
?resource a <@restypeIri> .
}
case None => {
@* Ensure that each matching resource is a knora-base:Resource. *@
?resource a knora-base:Resource .
}
}
@ownerIriOption match {
case Some(ownerIri) => {
# filter by ownerIri
?resource knora-base:attachedToUser <@ownerIri> .
}
case None => {}
}
?resource rdfs:label ?resourceLabel .
OPTIONAL {
?resource knora-base:hasStillImageFileValue ?fileValue .
?fileValue a knora-base:StillImageFileValue .
?fileValue knora-base:internalFilename ?previewPath .
OPTIONAL {
?fileValue knora-base:dimX ?previewDimX .
?fileValue knora-base:dimY ?previewDimY .
}
}
?resource knora-base:hasPermissions ?resourcePermissions ;
knora-base:attachedToUser ?resourceCreator ;
knora-base:attachedToProject ?resourceProject .
}
LIMIT 500