-
Notifications
You must be signed in to change notification settings - Fork 0
/
apachesolr_autocomplete.module
501 lines (456 loc) · 17.3 KB
/
apachesolr_autocomplete.module
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
<?php
/**
* @file
* Alters search forms to suggest terms using Apache Solr using AJAX.
* Thanks to robertDouglass who contributed some of the code.
*/
/**
* Implementation of hook_init().
*/
function apachesolr_autocomplete_init() {
drupal_add_css( drupal_get_path('module', 'apachesolr_autocomplete') . '/apachesolr_autocomplete.css');
// If using custom JS widget, include files and settings.
if (apachesolr_autocomplete_variable_get_widget() == 'custom') {
// Add custom autocomplete files
drupal_add_js(drupal_get_path('module', 'apachesolr_autocomplete') .'/apachesolr_autocomplete.js');
drupal_add_js(drupal_get_path('module', 'apachesolr_autocomplete') .'/jquery-autocomplete/jquery.autocomplete.js');
drupal_add_css( drupal_get_path('module', 'apachesolr_autocomplete') .'/jquery-autocomplete/jquery.autocomplete.css');
// Specify path to autocomplete handler.
drupal_add_js(array('apachesolr_autocomplete' => array('path' => url('apachesolr_autocomplete'))), 'setting');
}
}
/**
* Implementation of hook_form_FORM_ID_alter().
*/
function apachesolr_autocomplete_form_search_form_alter(&$form, $form_state) {
if ($form['module']['#value'] == 'apachesolr_search' || $form['module']['#value'] == 'apachesolr_multisitesearch') {
$element = &$form['basic']['inline']['keys'];
apachesolr_autocomplete_do_alter($element);
}
}
/**
* Implementation of hook_form_FORM_ID_alter().
*/
function apachesolr_autocomplete_form_search_block_form_alter(&$form, $form_state) {
$element = &$form['search_block_form'];
apachesolr_autocomplete_do_alter($element);
}
/**
* Implementation of hook_form_FORM_ID_alter().
*/
function apachesolr_autocomplete_form_search_theme_form_alter(&$form, $form_state) {
$element = &$form['search_theme_form'];
apachesolr_autocomplete_do_alter($element);
}
/**
* Helper function to do the actual altering of search forms.
*
* @param $element
* The element to alter. Should be passed by reference so that original form
* element will be altered.
* E.g.: apachesolr_autocomplete_do_alter(&$form['xyz'])
*/
function apachesolr_autocomplete_do_alter(&$element) {
if (apachesolr_autocomplete_variable_get_widget() == 'custom') {
// Create elements if they do not exist.
if (!isset($element['#attributes'])) {
$element['#attributes'] = array();
}
if (!isset($element['#attributes']['class'])) {
$element['#attributes']['class'] = 'apachesolr-autocomplete unprocessed';
}
else {
$element['#attributes']['class'] .= ' apachesolr-autocomplete unprocessed';
}
}
else {
$element['#autocomplete_path'] = 'apachesolr_autocomplete';
}
}
/**
* Implementation of hook_form_alter().
*
* Tries to alter exposed forms from views that use apachesolr as the base table
*/
function apachesolr_autocomplete_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == "views_exposed_form" && $form['text']) {
$form['#after_build'][] = 'apachesolr_autocomplete_views_exposed_form';
}
}
/**
* Callback for #after_build to alter apachesolr views exposed text widgets
*
* @see apachesolr_autocomplete_form_alter()
*/
function apachesolr_autocomplete_views_exposed_form($form, $form_element) {
// Is this a views that uses apachesolr as the base table?
if (($form_element['view']->base_table == 'apachesolr') || ($form_element['view']->base_table == 'apachesolr_node')) {
// Add autocomplete to all text-type form elements
foreach ($form as $id => $data) {
if ($data['#type'] == 'textfield') {
$form[$id]['#attributes']['class'] .= ' apachesolr-autocomplete unprocessed';
}
}
}
return $form;
}
/**
* Implementation of hook_menu().
*/
function apachesolr_autocomplete_menu() {
$items = array();
$items['apachesolr_autocomplete'] = array(
'page callback' => 'apachesolr_autocomplete_callback',
'access callback' => 'user_access',
'access arguments' => array('search content'),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Callback for url apachesolr_autocomplete/autocomplete.
* @param $keys
* The user-entered query.
*/
function apachesolr_autocomplete_callback($keys = '') {
if (apachesolr_autocomplete_variable_get_widget() == 'custom') {
// Keys for custom widget come from $_GET.
$keys = $_GET['query'];
}
$suggestions = array();
$suggestions = array_merge($suggestions, apachesolr_autocomplete_suggest_word_completion($keys, 5));
if (apachesolr_autocomplete_variable_get_suggest_keywords() || apachesolr_autocomplete_variable_get_suggest_spellcheck()) {
$suggestions = array_merge($suggestions, apachesolr_autocomplete_suggest_additional_term($keys, 5));
}
$result = array();
if (apachesolr_autocomplete_variable_get_widget() == 'custom') {
// Place suggestions into new array for returning as JSON.
foreach ($suggestions as $key => $display) {
$result[] = array(
"key" => substr($key,1),
"display" => $display
);
}
}
else {
foreach ($suggestions as $key => $display) {
$result[substr($key,1)] = $display;
}
}
drupal_json($result);
exit();
}
/**
* Implementation of hook_theme().
*/
function apachesolr_autocomplete_theme() {
return array(
'apachesolr_autocomplete_highlight' => array(
'file' => 'apachesolr_autocomplete.module',
'arguments' => array(
'keys' => NULL,
'suggestion' => NULL,
'count' => NULL,
),
),
'apachesolr_autocomplete_spellcheck' => array(
'file' => 'apachesolr_autocomplete.module',
'arguments' => array(
'suggestion' => NULL,
),
),
);
}
/**
* Themes each returned suggestion.
*/
function theme_apachesolr_autocomplete_highlight($keys, $suggestion, $count = 0) {
static $first = true;
$html = '';
$html .= '<div class="apachesolr_autocomplete suggestion">';
$html .= '<strong>' . drupal_substr($suggestion, 0, strlen($keys)) . '</strong>' . drupal_substr($suggestion, strlen($keys));
$html .= '</div>';
if ($count) {
if ($first) {
$html .= "<div class='apachesolr_autocomplete message' style='float:right'>";
$html .= t('!count results', array('!count' => $count));
$html .= "</div><br style='clear:both'>";
$first = false;
} else {
$html .= "<div class='apachesolr_autocomplete message count'>$count</div><br style='clear:both'>";
}
}
return $html;
}
/**
* Themes the spellchecker's suggestion.
*/
function theme_apachesolr_autocomplete_spellcheck($suggestion) {
return '<span class="apachesolr_autocomplete message">' . t('Did you mean') .':</span> ' . $suggestion;
}
/**
* Return the basic set of parameters for the Solr query.
*
* @param $suggestions_to_return
* Number of facets to return.
* @return array
*/
function apachesolr_autocomplete_basic_params($suggestions_to_return) {
return array(
'facet' => 'true',
'facet.field' => array('spell'),
// We ask for $suggestions_to_return * 5 facets, because we want
// not-too-frequent terms (will be filtered below). 5 is just my best guess.
'facet.limit' => $suggestions_to_return * 5,
'facet.mincount' => 1,
'start' => 0,
'rows' => 0,
);
}
/**
* Helper function that suggests ways to complete partial words.
*
* For example, if $keys = "learn", this might return suggestions like:
* learn, learning, learner, learnability.
* The suggested terms are returned in order of frequency (most frequent first).
*
*/
function apachesolr_autocomplete_suggest_word_completion($keys, $suggestions_to_return = 5) {
/**
* Split $keys into two:
* $first_part will contain all complete words (delimited by spaces). Can be empty.
* $last_part is the (assumed incomplete) last word. If this is empty, don't suggest.
* Example:
* $keys = "learning dis" : $first_part = "learning", $last_part = "dis"
*/
preg_match('/^(:?(.* |))([^ ]+)$/', $keys, $matches);
$first_part = @$matches[2];
// Make sure $last_part contains meaningful characters
$last_part = preg_replace('/[' . PREG_CLASS_SEARCH_EXCLUDE . ']+/u', '', $matches[3]);
if ($last_part == '') {
return array();
}
// Ask Solr to return facets that begin with $last_part; these will be the suggestions.
$params = apachesolr_autocomplete_basic_params($suggestions_to_return);
$params['facet.prefix'] = $last_part;
// Get array of themed suggestions.
$result = apachesolr_autocomplete_suggest($first_part, $params, 'apachesolr_autocomplete_highlight', $keys, $suggestions_to_return);
if ($result && $result['suggestions']) {
return $result['suggestions'];
} else {
return array();
}
}
/**
* Helper function that suggests additional terms to search for.
*
* For example, if $keys = "learn", this might return suggestions like:
* learn student, learn school, learn mathematics.
* The suggested terms are returned in order of frequency (most frequent first).
*/
function apachesolr_autocomplete_suggest_additional_term($keys, $suggestions_to_return = 5) {
$keys = trim($keys);
if ($keys == '') {
return array();
}
// Return no suggestions when $keys consists of only word delimiters
if (drupal_strlen(preg_replace('/[' . PREG_CLASS_SEARCH_EXCLUDE . ']+/u', '', $keys)) < 1) {
return array();
}
// Ask Solr to return facets from the 'spell' field to use as suggestions.
$params = apachesolr_autocomplete_basic_params($suggestions_to_return);
// Initialize arrays
$suggestions = array();
$replacements = array();
// Get array of themed suggestions.
$result = apachesolr_autocomplete_suggest($keys, $params, 'apachesolr_autocomplete_highlight', $keys, $suggestions_to_return);
if ($result && apachesolr_autocomplete_variable_get_suggest_keywords()) {
if (isset($result['suggestions']) && sizeof($result['suggestions'])) {
$suggestions = array_merge($suggestions, $result['suggestions']);
}
}
// Suggest using the spellchecker
if (apachesolr_autocomplete_variable_get_suggest_spellcheck()) {
if (is_object($result['response']->spellcheck)
&& is_object($result['response']->spellcheck->suggestions)) {
$spellcheck_suggestions = get_object_vars($result['response']->spellcheck->suggestions);
foreach($spellcheck_suggestions as $word => $value) {
$replacements[$word] = $value->suggestion[0];
}
if (count($replacements)) {
$new_keywords = strtr($keys, $replacements);
if ($new_keywords != $keys) {
// Place spellchecker suggestion before others
$suggestions = array_merge(array('*' . $new_keywords => theme('apachesolr_autocomplete_spellcheck', $new_keywords)), $suggestions);
}
}
}
}
return $suggestions;
}
function apachesolr_autocomplete_suggest($keys, $params, $theme_callback, $orig_keys, $suggestions_to_return = 5) {
$matches = array();
$suggestions = array();
$keys = trim($keys);
// We need the keys array to make sure we don't suggest words that are already
// in the search terms.
$keys_array = explode(' ', $keys);
$keys_array = array_filter($keys_array);
// Query Solr for $keys so that suggestions will always return results.
$query = apachesolr_drupal_query($keys);
// Allow other modules to modify query.
apachesolr_modify_query($query, $params, 'apachesolr_autocomplete');
if (!$query) {
return array();
}
$params += apachesolr_search_spellcheck_params($query);
// Try to contact Solr.
try {
$solr = apachesolr_get_solr();
apachesolr_search_add_boost_params($params, $query, $solr);
}
catch (Exception $e) {
watchdog('Apache Solr', $e->getMessage(), NULL, WATCHDOG_ERROR);
return array();
}
// Query Solr
$response = $solr->search($keys, $params['start'], $params['rows'], $params);
foreach ($params['facet.field'] as $field) {
foreach ($response->facet_counts->facet_fields->{$field} as $terms => $count) {
$terms = preg_replace('/[_-]+/', ' ', $terms);
foreach (explode(' ', $terms) as $term) {
if ($term = trim(preg_replace('/['. PREG_CLASS_SEARCH_EXCLUDE .']+/u', '', $term))) {
if (isset($matches[$term])) {
$matches[$term] += $count;
}
else {
$matches[$term] = $count;
}
}
}
}
}
if (sizeof($matches) > 0) {
// Eliminate suggestions that are stopwords or are already in the query.
$matches_clone = $matches;
$stopwords = apachesolr_autocomplete_get_stopwords();
foreach ($matches_clone as $term => $count) {
if ((strlen($term) > 3) && !in_array($term, $stopwords) && !array_search($term, $keys_array)) {
// Longer strings get higher ratings.
#$matches_clone[$term] += strlen($term);
}
else {
unset($matches_clone[$term]);
unset($matches[$term]);
}
}
// Don't suggest terms that are too frequent (in >90% of results).
$max_occurence = $response->response->numFound * 0.90;
foreach ($matches_clone as $match => $count) {
if ($count > $max_occurence) {
unset($matches_clone[$match]);
}
}
// The $count in this array is actually a score. We want the highest ones first.
arsort($matches_clone);
// Shorten the array to the right ones.
$matches_clone = array_slice($matches_clone, 0, $suggestions_to_return, TRUE);
// Add current search as suggestion if results > 0
if ($response->response->numFound > 0 && $keys != '') {
// Add * to array element key to force into a string, else PHP will
// renumber keys that look like numbers on the returned array.
$suggestions['*' . $keys] = theme('apachesolr_autocomplete_highlight', $keys, $keys, $response->response->numFound);
}
// Build suggestions using returned facets
foreach ($matches_clone as $match => $count) {
if ($keys != $match) {
$suggestion = trim($keys . ' ' . $match);
// On cases where there are more than 3 keywords, omit displaying
// the count because of the mm settings in solrconfig.xml
if (substr_count($suggestion, ' ') >= 2) {
$count = 0;
}
if ($suggestion != '') {
// Add * to array element key to force into a string, else PHP will
// renumber keys that look like numbers on the returned array.
$suggestions['*' . $suggestion] = theme('apachesolr_autocomplete_highlight', $orig_keys, $suggestion, $count);
}
}
}
}
return array(
'suggestions' => $suggestions,
'response' => &$response
);
}
/**
* Gets the current stopwords list configured in Solr.
*/
function apachesolr_autocomplete_get_stopwords() {
static $words = array(), $flag = false;
if ($flag) {
return $words;
}
$stopwords_url = "/admin/file/?file=stopwords.txt";
$host = variable_get('apachesolr_host', 'localhost');
$port = variable_get('apachesolr_port', 8983);
$path = variable_get('apachesolr_path', '/solr');
$url = "http://{$host}:{$port}{$path}{$stopwords_url}";
$result = drupal_http_request($url);
if ($result->code != 200) {
return array();
}
$words = array();
foreach (explode("\n", $result->data) as $line) {
if (drupal_substr($line, 0, 1) == "#") {
continue;
}
if ($word = trim($line)) {
$words[] = $word;
}
}
$flag = true;
return $words;
}
/**
* Wrapper around variable_get() for variable apachesolr_autocomplete_widget.
*/
function apachesolr_autocomplete_variable_get_widget() {
return variable_get('apachesolr_autocomplete_widget', 'custom');
}
/**
* Wrapper around variable_get() for variable apachesolr_autocomplete_suggest_keywords.
*/
function apachesolr_autocomplete_variable_get_suggest_keywords() {
return variable_get('apachesolr_autocomplete_suggest_keywords', 1);
}
/**
* Wrapper around variable_get() for variable apachesolr_autocomplete_suggest_spellcheck.
*/
function apachesolr_autocomplete_variable_get_suggest_spellcheck() {
return variable_get('apachesolr_autocomplete_suggest_spellcheck', 1);
}
/**
* Alter the apachesolr.module "advanced settings" form.
*/
function apachesolr_autocomplete_form_apachesolr_settings_alter(&$form, $form_state) {
$form['advanced']['apachesolr_autocomplete_widget'] = array(
'#type' => 'radios',
'#title' => t('Autocomplete widget to use'),
'#description' => t('The custom widget provides instant search upon selection, whereas the Drupal widget needs the user to hit Enter or click on the Search button. If you are having problems, try switching to the default Drupal autocomplete widget.'),
'#options' => array('custom' => t('Custom autocomplete widget'), 'drupal' => t('Drupal core autocomplete widget')),
'#default_value' => apachesolr_autocomplete_variable_get_widget(),
);
$form['advanced']['apachesolr_autocomplete_suggest_keywords'] = array(
'#type' => 'checkbox',
'#title' => t('Enable additional keyword suggestions on the autocomplete widget'),
'#description' => t('Suggest words to add to the currently typed-in words. E.g.: typing "blue" might suggest "blue bike" or "blue shirt".'),
'#default_value' => apachesolr_autocomplete_variable_get_suggest_keywords(),
);
$form['advanced']['apachesolr_autocomplete_suggest_spellcheck'] = array(
'#type' => 'checkbox',
'#title' => t('Enable spellchecker suggestions on the autocomplete widget'),
'#description' => t('Suggest corrections to the currently typed-in words. E.g.: typing "rec" or "redd" might suggest "red".'),
'#default_value' => apachesolr_autocomplete_variable_get_suggest_spellcheck(),
);
}