/** * Batch sync data to ElasticSearch * * This function is timed at a max runtime of 30sec * * @param array $options the options for elgg_get_entities() * @param int $crontime the starttime of the cron in order to limit max runtime * @param string $getter the getter function to use for \ElggBatch * * @return bool|void */ protected static function batchSync($options, $crontime, $getter = '') { if (empty($options) || !is_array($options)) { return; } if (empty($getter)) { $getter = 'elgg_get_entities_from_private_settings'; } if (!is_callable($getter)) { return false; } $client = elasticsearch_get_client(); if (empty($client)) { return; } $crontime = sanitise_int($crontime, false); if (empty($crontime)) { $crontime = time(); } if (time() - $crontime >= 30) { return false; } set_time_limit(40); $ia = elgg_set_ignore_access(true); $time_left = true; $batch_size = 100; $options['callback'] = false; $options['limit'] = $batch_size; while ($time_left && ($rows = call_user_func($getter, $options))) { $guids = array(); foreach ($rows as $row) { $guids[] = (int) $row->guid; } $result = $client->bulkIndexDocuments($guids); if (empty($result)) { break; } $items = elgg_extract('items', $result); foreach ($items as $item) { $guid = (int) elgg_extract('_id', elgg_extract('index', $item)); $status = elgg_extract('status', elgg_extract('index', $item)); if ($status !== 200) { continue; } if (empty($guid)) { continue; } set_private_setting($guid, ELASTICSEARCH_INDEXED_NAME, time()); } if (time() - $crontime >= 30) { $time_left = false; break; } } // restore access elgg_set_ignore_access($ia); return $time_left; }
protected static function getClientForHooks($params) { if (elasticsearch_get_setting('search') !== 'yes') { return false; } $client = elasticsearch_get_client(); if (!$client) { return false; } self::prepareSearchParamsForHook($client, $params); $client->search_params->addEntityAccessFilter(); return $client; }
/** * Find Elgg GUIDs not present in Elasticsearch * * @param int[] $guids Elgg GUIDs * * @return int[] */ protected static function findUnindexedGUIDs($guids = []) { if (empty($guids) || !is_array($guids)) { return []; } $client = elasticsearch_get_client(); if (empty($client)) { return; } $search_params = ['index' => $client->getIndex(), 'search_type' => 'scan', 'scroll' => '2m', 'size' => count($guids), 'body' => ['query' => ['filtered' => ['filter' => ['terms' => ['guid' => $guids]]]]]]; try { $scroll_setup = $client->search($search_params); } catch (\Exception $e) { return []; } // now scroll through all results $result = []; $scroll_params = ['scroll_id' => elgg_extract('_scroll_id', $scroll_setup), 'scroll' => '2m']; try { while ($scroll_result = $client->scroll($scroll_params)) { $search_result = new SearchResult($scroll_result, $search_params); $elasticsearch_guids = $search_result->toGuids(); $guids_not_in_elasticsearch = array_diff($guids, $elasticsearch_guids); if (empty($guids_not_in_elasticsearch)) { continue; } $result = array_merge($result, $guids_not_in_elasticsearch); } } catch (\Exception $e) { // end off scroll } return array_unique($result); }
<?php $client = elasticsearch_get_client(); if (!$client) { return; } $query = get_input('q'); if (empty($query)) { return; } $suggestions = $client->getSuggestions(); if (empty($suggestions)) { return; } $suggestions = $suggestions['suggestions'][0]['options']; if (empty($suggestions)) { return; } $suggestion = $suggestions[0]['text']; $url = elgg_view('output/url', ['text' => $suggestion, 'href' => elgg_http_add_url_query_elements(current_page_url(), ['q' => $suggestion]), 'class' => 'elasticsearch-suggest-link']); $query = elgg_format_element('span', ['class' => 'elasticsearch-suggest-original-query'], $query); echo elgg_echo('elasticsearch:suggest', [$url, $query]);
/** * Handle the disable of an ElggEntity * * @param \ElggEntity $entity the entity * * @return void */ protected static function disableEntity(\ElggEntity $entity) { $client = elasticsearch_get_client(); if (empty($client)) { return; } // remove from index self::deleteEntity($entity); // remove indexed ts, so when reenabled it will get indexed automatically $entity->removePrivateSetting(ELASTICSEARCH_INDEXED_NAME); }