-
Notifications
You must be signed in to change notification settings - Fork 1
/
others-where-constraint.xqy
194 lines (170 loc) · 5.73 KB
/
others-where-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
xquery version "1.0-ml";
module namespace facet = "http://marklogic.com/others-where-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 variable $parsed-queries := map:map();
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]
(: pull real constraint def from annotation :)
let $real-constraint := facet:_get-real-constraint( $constraint )
(: 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) :)
(: FIXME: no better way of doing this? :)
let $toks := (
<searchdev:tok type="term">{ $constraint-name }</searchdev:tok>,
<searchdev:tok type="joiner"><search:joiner strength="50" apply="constraint">:</search:joiner></searchdev:tok>,
for $term in $terms
return
<searchdev:tok type="term">{ $term }</searchdev:tok>
)
let $query := impl:parse($toks, $real-options, 0)
(: keep track of what query comes from which constraint :)
let $_ := map:put($parsed-queries, $constraint-name, (map:get($parsed-queries, $constraint-name), cts:query($query)))
(: return the query :)
return $query
};
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()*
{
(: check if there are sub-queries for this constraint :)
let $parsed := map:get($parsed-queries, $constraint/@name)
(: exclude sub-queries for this constraint from overall query :)
let $filtered-query :=
if (exists($parsed)) then
facet:_filter-query( $query, $parsed )
else
$query
(: pull real constraint def from annotation :)
let $real-constraint := facet:_get-real-constraint( $constraint )
(: and loop through to search impl for start-facet :)
let $buckets :=
if ( $real-constraint/opt:range[opt:bucket|opt:computed-bucket] ) then
impl:resolve-buckets($real-constraint)
else ()
return
impl:start-facet(
$real-constraint,
$buckets,
$filtered-query,
$quality-weight,
$forests
)
};
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)
{
(: check if there are sub-queries for this constraint :)
let $parsed := map:get($parsed-queries, $constraint/@name)
(: exclude sub-queries for this constraint from overall query :)
let $filtered-query :=
if (exists($parsed)) then
facet:_filter-query( $query, $parsed )
else
$query
(: pull real constraint def from annotation :)
let $real-constraint := facet:_get-real-constraint( $constraint )
(: and loop through to search impl for start-facet :)
let $buckets :=
if ( $real-constraint/opt:range[opt:bucket|opt:computed-bucket] ) then
impl:resolve-buckets($real-constraint)
else ()
let $facet :=
impl:finish-facet(
$real-constraint,
$buckets,
$start,
$filtered-query,
$quality-weight,
$forests
)
return
element { node-name($facet) } {
$facet/(@* except @type),
attribute type { 'custom' },
$facet/node()
}
};
declare private function facet:_intersect( $left, $right ) {
let $r := $right ! xdmp:quote(.)
for $l in $left ! xdmp:quote(.)
where $l = $r
return $l
};
declare private function facet:_filter-query( $queries, $exclude-queries ) {
for $query in $queries
return typeswitch ($query)
case cts:and-query
return cts:and-query(
facet:_filter-query(cts:and-query-queries($query), $exclude-queries),
cts:and-query-options($query)
)
case cts:or-query
return cts:or-query(
facet:_filter-query(cts:or-query-queries($query), $exclude-queries),
cts:or-query-options($query)
)
case cts:not-query
return cts:not-query(
facet:_filter-query(cts:not-query-query($query), $exclude-queries),
cts:not-query-weight($query)
)
case cts:near-query
return cts:near-query(
facet:_filter-query(cts:near-query-queries($query), $exclude-queries),
cts:near-query-distance($query),
cts:near-query-options($query),
cts:near-query-weight($query)
)
default return
if (facet:_intersect($query, $exclude-queries)) then
cts:true-query()
else
$query
};
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:additional-query ) ]
return
element { node-name($node) } {
$node/@*,
$node/node()
}
}
};