/
grouping-constraint.xqy
369 lines (326 loc) · 10 KB
/
grouping-constraint.xqy
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
xquery version "1.0-ml";
module namespace facet = "http://marklogic.com/grouping-constraint";
import module namespace impl = "http://marklogic.com/appservices/search-impl" at "/MarkLogic/appservices/search/search-impl.xqy";
import schema namespace opt = "http://marklogic.com/appservices/search" at "search.xsd";
declare namespace search = "http://marklogic.com/appservices/search";
declare namespace searchdev = "http://marklogic.com/appservices/search/searchdev";
declare default function namespace "http://www.w3.org/2005/xpath-functions";
declare option xdmp:mapping "false";
declare function facet:parse-structured(
$query-elem as element(),
$options as element(search:options)
)
as schema-element(cts:query)
{
(: pull parameters from $query-elem :)
let $constraint-name as xs:string := $query-elem/search:constraint-name
let $terms as xs:string* := $query-elem//(search:text, search:value)
(: take appropriate constraint from full $options :)
let $constraint := $options/search:constraint[@name eq $constraint-name]
let $fragments :=
if ($constraint/*/search:facet-option = 'any') then
('document', 'locks', 'properties')
else
$constraint/*/search:facet-option[. = ('document', 'locks', 'properties')]/string(.)
let $fragments :=
if (exists($fragments)) then
$fragments
else
'document' (: TODO: consider fragment-scope :)
(: get grouping config from annotation :)
let $config := facet:_get-config( $constraint )
let $groups := facet:_get-groups( $config )
(: check if $terms matches any group :)
let $matching-groups :=
if ($terms = $config/search:show-remainder/@label) then
$groups
else
$groups[@label = $terms]
return
if (empty($matching-groups)) then
let $log-warning := xdmp:log(concat("No group name(s) matching: '", string-join($terms, "', '"), "'"))
return <cts:or-query/> (: nothing will match :)
else
(: pull real constraint def from annotation :)
let $real-constraint := facet:_get-real-constraint( $constraint )
(: loop through to search impl to derive index reference from $real-constraint :)
let $reference := impl:construct-reference(impl:get-refspecs($real-constraint))
(: use patterns to pull search values from lexicons :)
let $values := facet:_get-matches($reference, $matching-groups/search:match/@pattern)
return
if (exists($values)) then
(: reconstruct full options, but with $real-constraint :)
let $real-options :=
element { node-name($options) } {
$options/@*,
$options/node()[@name != $constraint-name],
$real-constraint
}
(: loop through to search impl for parse (passing through tokens, because we jump into processing of the AST) :)
let $toks := (
<searchdev:tok type="term">{ $constraint-name }</searchdev:tok>,
<searchdev:tok type="joiner"><search:joiner strength="50" apply="constraint">:</search:joiner></searchdev:tok>,
(: this pretends a string query, so we inject a fake value, which gets replaced later :)
<searchdev:tok type="term">##$$@@FAKEVALUE##$$@@</searchdev:tok>
)
let $query := impl:parse($toks, $real-options, 0)
let $values-query := facet:_insert-values(document{$query}/*, $values)
let $real-query :=
if ($terms = $config/search:show-remainder/@label) then
<cts:not-query>{ $values-query }</cts:not-query>
else
$values-query
return
<cts:or-query>{
for $fragment in $fragments
return element { xs:QName("cts:" || $fragment || "-fragment-query") } {
$real-query
}
}</cts:or-query>
else (: no values :)
if ($terms = $config/search:show-remainder/@label) then
<cts:and-query/> (: no anti-values, so everything will match :)
else
<cts:or-query/> (: no values, so nothing will match :)
};
declare function facet:start(
$constraint as element(search:constraint),
$query as cts:query?,
$facet-options as xs:string*,
$quality-weight as xs:double?,
$forests as xs:unsignedLong*
)
as item()*
{
(: pull real constraint def from annotation :)
let $real-constraint := facet:_get-real-constraint( $constraint )
(: get grouping config from annotation :)
let $config := facet:_get-config( $constraint )
(: isolate limit from $facet-options:)
let $limit := xs:int(
substring-after(
($facet-options, "limit=10")[starts-with(., "limit=")][1],
"="
)
)
let $facet-options := $facet-options[not(starts-with(., "limit="))]
(: loop through to search impl to derive index reference from $real-constraint :)
let $reference := impl:construct-reference(impl:get-refspecs($real-constraint))
(: calculate facet counts :)
return (
let $groups := facet:_get-groups($config)
(: go over all groups :)
for $group in ($groups, $config/search:show-remainder)
let $label as xs:string := ($group/@label, "(missing label)")[1]
let $show-remainder := $group instance of element(search:show-remainder)
let $sum :=
if ($show-remainder) then
let $overall-sum := xdmp:estimate(
cts:search(collection(),
$query,
("unfaceted"),
$quality-weight,
$forests
)
)
let $anti-values :=
for $group in $groups
for $anti-pattern in $group/search:match/@pattern
return
cts:value-match(
$reference,
$anti-pattern,
("concurrent", $facet-options),
$query,
$quality-weight,
$forests
)
let $anti-sum := sum(
for $anti-value in $anti-values
return
cts:frequency($anti-value)
)
(: one large estimate on all anti-values might be quicker than sum of their frequencies?
let $anti-sum := xdmp:estimate(
cts:search(collection(),
cts:and-query((
$query,
))
("unfaceted"),
$quality-weight,
$forests
)
)
:)
return $overall-sum - $anti-sum
else
sum(
for $pattern in $group/search:match/@pattern
for $value in cts:value-match(
$reference,
$pattern,
("concurrent", $facet-options),
$query,
$quality-weight,
$forests
)
return cts:frequency($value)
)
where $sum gt 0
order by $sum descending
return
<search:facet-value name="{$label}" count="{$sum}">{$label}</search:facet-value>
)[1 to $limit]
};
(:
<finish> function to apply for custom contraint [Document Type].
:)
declare function facet:finish(
$start as item()*,
$constraint as element(search:constraint),
$query as cts:query?,
$facet-options as xs:string*,
$quality-weight as xs:double?,
$forests as xs:unsignedLong*
)
as element(search:facet)
{
<search:facet name="{$constraint/@name}" type="custom">{
$start
}</search:facet>
};
declare private function facet:_get-real-constraint(
$constraint as element(search:constraint)
)
as element(opt:constraint)
{
element opt:constraint {
$constraint/@*,
for $node in $constraint/search:annotation/*[ empty( self::search:config ) ]
return
element { node-name($node) } {
$node/@*,
$node/node()
}
}
};
declare private function facet:_get-config(
$constraint as element(search:constraint)
)
as element(search:config)?
{
$constraint/search:annotation/search:config
};
declare private function facet:_get-groups(
$config as element(search:config)?
)
as element(search:group)*
{
$config/search:group
};
declare private function facet:_match-group-by-value(
$config as element(search:config)?,
$value as xs:string
)
as element()?
{
let $group := (
for $group in facet:_get-groups($config)
let $label := $group/@label
for $pattern in $group/search:match/@pattern
let $match-pattern := replace(replace($pattern, "?", "."), "*", ".*")
return
if ( matches($value, $match-pattern) ) then
$group
else (),
$config/search:show-remainder
)[1]
return $group
};
declare private function facet:_insert-values(
$query as node()*,
$values as xs:string*
)
as node()*
{
for $node in $query
return typeswitch ($node)
(: replace fake value, and expand according to $values :)
case schema-element(cts:text)
return
facet:_expand-element(
$node,
$values
)
case element(cts:value)
return
facet:_expand-element(
$node,
$values
)
case schema-element(cts:uri)
return
facet:_expand-element(
$node,
$values
)
(: recursive descend :)
case element()
return
facet:_copy-element(
$node,
facet:_insert-values($node/node(), $values)
)
(: fall-through for non-elements :)
default return $node
};
declare private function facet:_expand-element(
$elem as element(),
$values as item()*
)
as element()*
{
for $value in $values
return
facet:_copy-element(
$elem,
$value
)
};
declare private function facet:_copy-element(
$elem as element(),
$contents as item()*
)
as element()
{
element { node-name($elem) } {
$elem/@*,
$elem/namespace::*,
$contents
}
};
declare private function facet:_get-matches(
$reference,
$patterns as xs:anyAtomicType*
)
as xs:anyAtomicType*
{
facet:_get-matches($reference, $patterns, (), (), (), ())
};
declare private function facet:_get-matches(
$reference,
$patterns as xs:anyAtomicType*,
$options,
$query,
$quality-weight,
$forests
)
as xs:anyAtomicType*
{
for $pattern in $patterns
return
if (contains($pattern, "?") or contains($pattern, "*")) then
cts:value-match($reference, $pattern, $options, $query, $quality-weight, $forests)
else
$pattern
};