forked from tvogels/hz-search
-
Notifications
You must be signed in to change notification settings - Fork 1
/
search_page.php
307 lines (232 loc) · 8.64 KB
/
search_page.php
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
<?php
require_once 'vendor/autoload.php';
use Symfony\Component\Yaml\Yaml;
// Define some constants
define(ROOT_NODE, 'ROOT');
define(SEARCH_CONTEXT_MIN_WEIGHT, 0.8);
// WME 13072015: disabled since this is already done in search_settings.php
// that one is currently always loaded before this one (see Deltaskin.skin.php)
// Load parameters from parameters.yml
//$settings = Yaml::parse(file_get_contents(__DIR__ .'/parameters.yml'));
//$parameters = $settings['parameters'];
// Check if a query was given
if (!isset($_GET['q'])) {
header("Location: {$indexUrl}");
exit;
}
// Create an ElasticSearch client
$esclient = new Elasticsearch\Client(
array('hosts' =>
array($parameters['elastic.server'])
)
);
// Retrieve all contexts
$contexts = $esclient->search(array(
'index' => $parameters['elastic.index'],
'type' => 'context',
'size' => 1000,
'body' => array(
'query' => array(
'match_all' => array()
)
)
));
$contexts = $contexts['hits']['hits'];
// Get search results
$search_results = $esclient->search(array(
'index' => $parameters['elastic.index'],
// 'type' => 'intentional_element',
'size' => 100,
'body' => array(
'query' => array(
'multi_match' => array(
'query' => $_GET['q'],
'fields' => array(
"skos:prefLabel^3",
"skos:definition",
"title^3",
"content",
"concerns_readable^2",
"context_readable^2"
)
)
)
)
));
$search_results = $search_results['hits']['hits'];
// only take results with VN page
$search_results = array_filter($search_results, function ($res) {
return count($res['_source']['suggest']['payload']['vn_pages'])>0;
});
// This helper returns the VN page that should be visited
// when a link is clicked (if available)
function vn_url ($source) {
if (count($source['suggest']['payload']['vn_pages'])>0)
return $source['suggest']['payload']['vn_pages'][0];
else
return $source['url'];
}
// Construct a data structure that will be used
// to quickly find parent- and children nodes of the
// context tree
// The data structures are two dictionaries:
// parents[url] = superurl, and
// children[url] = [child,child,child]
$parents = array();
$children = array();
$info = array();
foreach ($contexts as $context) {
$url = $context['_source']['url'];
$super = $context['_source']['supercontext'];
$parents[urldecode($url)] = urldecode($super);
$children[$super][] = $url;
$info[$url] = $context['_source'];
}
$info[ROOT_NODE] = array(
'name' => 'Alle Contexten',
'url' => ''
);
$contextExists = function ($context) use ($parents) {
return isset($parents[$context]);
};
// Determine the 'search context'
// (1) Define an array that will store the weight
// that is attached to all context for the current
// search, and define a function that adds weight
// to the nodes recursively.
$weights = array();
$addWeight = function ($context_url, $weight_to_add) use (&$weights, $parents, &$addWeight, $contextExists) {
// we SKIP invalid contexts
if ($context_url != ROOT_NODE && !$contextExists($context_url)) return;
// printf("<b>Add weight to %s.</b><br>\n",$context_url);
if (isset($weights[$context_url])) {
$weights[$context_url] += $weight_to_add;
} else {
$weights[$context_url] = $weight_to_add;
}
// recursive step
if ($context_url != ROOT_NODE) {
// printf("Add weight to parent %s.<br>\n",$parents[$context_url]);
$addWeight($parents[$context_url] , $weight_to_add);
}
};
// (2) Initialize the weights for the current search
foreach ($search_results as $result) {
$weight = $result['_score'];
foreach ($result['_source']['context'] as $context) {
$addWeight($context, $weight);
}
}
// The search context should have a minimum weight of
// MIN_PERCENTAGE * WEIGHT[ROOT]
$min_weight = SEARCH_CONTEXT_MIN_WEIGHT * $weights[ROOT_NODE];
// This searches for the child with the highest percentage
$findSearchContext = function ($context_url, $min_weight) use (&$findSearchContext, $weights, $children) {
// Collect the children of the node and their weights
$kiddos = $children[$context_url];
$kiddo_weights = array_map(function ($kid) use ($weights) {
if (isset($weights[$kid])) return $weights[$kid];
else return 0.;
}, $kiddos);
// If there is a child with a weight > $min_weight, recurse,
// otherwise, return this context as the search context.
if (count($kiddos) > 0 && max($kiddo_weights) >= $min_weight) {
$key = array_keys($kiddo_weights, max($kiddo_weights));
$key = $key[0];
return $findSearchContext($kiddos[$key],$min_weight);
} else {
return $context_url;
}
};
// Determine the search context
$search_context = $findSearchContext(ROOT_NODE, $min_weight);
// And store some info about it
$search_context_info = $info[$search_context];
// This traces a context's parents and returns them in an array
$trace = function ($context_url) use (&$trace, $parents, $contextExists) {
// we SKIP invalid contexts
if ($context_url != ROOT_NODE && !$contextExists($context_url)) return array();
if ($context_url == ROOT_NODE)
return array(md5(ROOT_NODE));
else {
$parent_trace = $trace($parents[$context_url]);
array_push($parent_trace,md5($context_url));
return $parent_trace;
}
};
// Count the number of results per context
// in a dictionary like $counts[md5 of url] = int.
$counts = array();
foreach ($search_results as $result) {
$url = urldecode($result['_source']['url']);
foreach ($result['_source']['context'] as $cntxt) {
$tr = $trace(urldecode($cntxt));
foreach ($tr as $t) {
$counts[$t]++;
}
}
}
$urlCounts = function ($url) use ($counts) {
$md5 = md5($url);
if (isset($counts[$md5])) return $counts[$md5];
else return 0;
};
?>
<div id="sectionNav"></div>
<div id="body">
<h1>Zoekresultaat “<?php echo htmlentities($_GET['q']) ?>”</h1>
<input type="hidden" id="search-query" value="<?php echo htmlentities($_GET['q']) ?>">
<div id="mw-content-text" lang="nl" dir="ltr" class="mw-content-ltr">
<p class="count-string"><?php echo count($search_results) ?> zoekresultaten</p>
<div id="page">
<ul class="search-results">
<?php foreach($search_results as $result): ?>
<a data-contexts="<?php echo implode(" ",$trace($result['_source']['context'][0])) ?>" href="<?php echo htmlentities(vn_url($result['_source'])) ?>"><li>
<h2><?php echo htmlentities(utf8_decode($result['_source']['title'])) ?></h2>
<p>Context: <?php echo htmlentities($result['_source']['context_readable']) ?>
<?php
if (!$contextExists($result['_source']['context'][0])) {
echo "<span class=\"error\"> !</span>\n";
}
?>
</p>
</li></a>
<?php endforeach ?>
</ul>
</div>
<?php if (count($search_results) > 1): ?>
<div class="aside search-context-info" id="taxonomy">
<div>
<p>Zoek specifieker in:</p>
<h2>
<a
data-count="<?php echo $urlCounts($search_context) ?>"
data-context="<?php echo md5($search_context) ?>"
data-context-name="<?php echo htmlentities($search_context_info['name']) ?>"
href="<?php echo vn_url($search_context_info) ?>"
>
<?php echo $search_context_info['name'] ?> (<?php echo $urlCounts($search_context) ?>)
</a>
</h2>
<ul class="list-unstyled">
<?php foreach($children[$search_context] as $child): ?>
<?php if ($urlCounts($child) > 0): ?>
<li>
<a
data-count="<?php echo $urlCounts($child) ?>"
data-context="<?php echo md5($child) ?>"
data-context-name="<?php echo htmlentities($info[$child]['name']) ?>"
href="<?php echo vn_url($info[$child]) ?>"
>
<?php echo $info[$child]['name'] ?> (<?php echo $urlCounts($child) ?>)
</a>
</li>
<?php endif ?>
<?php endforeach ?>
</ul>
<p><a href="#" data-count="<?php echo count($search_results) ?>" class="search-everywhere" data-context="null">Zoek overal (<?php echo count($search_results) ?>)</a></p>
</div>
</div>
<?php endif ?>
</div>
</div>