forked from VeggieMeat/search_api_elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search_api_elasticsearch.module
272 lines (233 loc) · 7.75 KB
/
search_api_elasticsearch.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
<?php
/**
* @file
* Provides an elasticsearch-based service class for the Search API.
*/
/**
* Implements hook_menu().
*/
function search_api_elasticsearch_menu() {
$items['admin/config/search/search_api/index/%search_api_index/analysis'] = array(
'title' => 'Analysis',
'description' => 'Configure Elasticsearch analysis',
'page callback' => 'drupal_get_form',
'page arguments' => array('search_api_elasticsearch_analysis_form', 5),
'access arguments' => array('administer search_api'),
'file' => 'includes/search_api_elasticsearch.admin.inc',
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_INLINE | MENU_CONTEXT_PAGE,
);
return $items;
}
/**
* Implements hook_views_api().
*/
function search_api_elasticsearch_views_api() {
return array(
'api' => '3.0',
);
}
/**
* Get server ID by name.
*
* @param integer $server_machine_name
*/
function search_api_elasticsearch_get_server_id_by_name($server_machine_name) {
$query = '';
$result = '';
try {
$query = db_select('search_api_server', 'sas');
$query->addField('sas', 'id');
$query->condition('sas.machine_name', $server_machine_name, '=');
$result = $query->execute()->fetchAssoc();
}
catch (Exception $e) {
watchdog('Elasticsearch', $e->getMessage(), array(), WATCHDOG_ERROR);
return FALSE;
}
if (isset($result) && !empty($result)) {
return reset($result);
}
else {
return FALSE;
}
}
/**
* Ajax callback.
*
* @param array $form
* @param array $form_state
*
* @return array
*/
function search_api_elasticsearch_ajax_callback($form, &$form_state) {
return $form['options'];
}
/**
* Attach the Ajax attributes.
*
* @param array $form
*/
function search_api_elasticsearch_attach_ajax_callback(&$form) {
$form['options']['#prefix'] = '<div id="elasticsearch-add-index">';
$form['options']['#suffix'] = '</div>';
$form['server']['#ajax'] = array(
'callback' => 'search_api_elasticsearch_ajax_callback',
'wrapper' => 'elasticsearch-add-index',
'method' => 'replace',
'effect' => 'fade',
);
}
/**
* Implements hook_theme_registry_alter().
*/
function search_api_elasticsearch_theme_registry_alter(&$theme_registry) {
if (!empty($theme_registry['search_api_index']['function'])) {
$theme_registry['search_api_index']['function'] = 'search_api_elasticsearch_theme_search_api_index';
}
}
/**
* Implements hook_theme().
*/
function search_api_elasticsearch_theme($existing, $type, $theme, $path) {
return array(
'search_api_elasticsearch_index' => array(
'variables' => array(
'module' => 'search_api_elasticsearch',
'settings' => NULL,
),
'template' => 'search-api-elasticsearch-index',
'path' => drupal_get_path('module', 'search_api_elasticsearch') . '/templates',
)
);
}
/**
* Template preprocess index template view.
*
* @param array $variables
*/
function template_preprocess_search_api_elasticsearch_index(&$variables) {
$settings = $variables['settings'];
$variables['read_only'] = '';
$variables['name'] = $settings['name'];
if ($settings['enabled']) {
$variables['enabled'] = t('Enabled (!disable_link)', array('!disable_link' => l(t('disable'), 'admin/config/search/search_api/index/' . $settings['machine_name'] . '/disable')));
}
elseif ($settings['server'] && $settings['server']->enabled) {
$variables['enabled'] = t('Disabled (!enable_link)', array('!enable_link' => l(t('enable'), 'admin/config/search/search_api/index/' . $settings['machine_name'] . '/enable', array('query' => array('token' => drupal_get_token($settings['machine_name']))))));
}
else {
$variables['enabled'] = t('Disabled');
}
$variables['machine_name'] = check_plain($settings['machine_name']);
$type = search_api_get_item_type_info($settings['item_type']);
$type = $type['name'];
$variables['item_type'] = check_plain($type);
if (!empty($settings['description'])) {
$variables['description'] = nl2br(check_plain($settings['description']));
}
if (!empty($settings['server'])) {
$variables['server'] = l($settings['server']->name, 'admin/config/search/search_api/server/' . $settings['server']->machine_name);
if (!empty($settings['server']->description)) {
$variables['server_description'] = nl2br(check_plain($settings['server']->description));
}
}
if (!$settings['read_only'] && !empty($settings['options'])) {
$variables['read_only'] = $settings['read_only'];
if (empty($settings['options']['cron_limit'])) {
$variables['cron_limit'] = t("Don't index during cron runs");
}
elseif ($settings['options']['cron_limit'] < 0) {
$variables['cron_limit'] = t('Cron batch size: Unlimited');
}
else {
$variables['cron_limit'] = t('Cron batch size: :size', array(':size' => $settings['options']['cron_limit']));
}
if (isset($settings['options']['number_of_shards'])) {
$variables['number_of_shards'] = t('Number of shards: :shards', array(':shards' => $settings['options']['number_of_shards']));
}
if (isset($settings['options']['number_of_replicas'])) {
$variables['number_of_replicas'] = t('Number of replicas: :replicas', array(':replicas' => $settings['options']['number_of_replicas']));
}
}
elseif ($settings['read_only']) {
$variables['read_only'] = t('This index is read-only.');
}
$variables['configuration_status'] = theme('entity_status', array('status' => $settings['status']));
}
/**
* Helper function. Returns themed index view.
*
* @param array $variables
*/
function search_api_elasticsearch_theme_search_api_index(array $variables) {
return theme('search_api_elasticsearch_index', array('module' => 'search_api_elasticsearch', 'settings' => $variables));
}
/**
* Submit handler
* @param array $form
* @param array $form_state
*/
function search_api_elasticsearch_edit_index_submit(array $form, array &$form_state) {
form_state_values_clean($form_state);
$merge_types = array();
$elastic_server_id = search_api_elasticsearch_get_server_id_by_name($form_state['index']->server);
$elastic_server = search_api_server_load($elastic_server_id);
$index = $form_state['index'];
$elastic_server->updateSettings($index, $merge_types);
}
/**
* Submit handler
* @param array $form
* @param array $form_state
*/
function search_api_elasticsearch_add_index_submit(array $form, array &$form_state) {
form_state_values_clean($form_state);
}
/**
* Ajax callback for adding more nodes.
*
* @param array $form
* @param array $form_state
*/
function _search_api_elasticsearch_configuration_form_ajax($form, &$form_state) {
unset($form['options']['form']['add_more']);
if (module_exists('search_api_facetapi')) {
unset($form['options']['form']['facet_settings']);
}
return $form['options']['form'];
}
/**
* Ajax callback for remove nodes.
* @param array $form
* @param array $form_state
*/
function _search_api_elasticsearch_configuration_form_remove_ajax($form, &$form_state) {
unset($form['options']['form']['add_more']);
if (module_exists('search_api_facetapi')) {
unset($form['options']['form']['facet_settings']);
}
return $form['options']['form'];
}
/**
* Submit Handler for the configuration form.
*
* @param array $form
* @param array $form_state
*/
function _search_api_elasticsearch_configuration_form_submit_custom($form, &$form_state) {
$form_state['rebuild'] = TRUE;
}
/**
* Submit handler on removing elements
*
* @param array $form
* @param array $form_state
*/
function _search_api_elasticsearch_configuration_form_remove_custom($form, &$form_state) {
$form_state['values']['remove_delta'] == 'none';
if (isset($form_state['triggering_element']['#remove_delta'])) {
$form_state['values']['remove_delta'] = $form_state['triggering_element']['#remove_delta'];
}
$form_state['rebuild'] = TRUE;
}