/** * @param NodeInterface $node * @param string $term */ public function indexAction(NodeInterface $node, $term) { $request = array('suggests' => array('text' => $term, 'term' => array('field' => '_all'))); $response = $this->elasticSearchClient->getIndex()->request('GET', '/_suggest', array(), json_encode($request))->getTreatedContent(); $suggestions = array_map(function ($option) { return $option['text']; }, $response['suggests'][0]['options']); $this->view->assign('value', $suggestions); }
/** * Return the total number of hits for the query. * * @return integer * @api */ public function count() { $timeBefore = microtime(true); $request = $this->getRequest(); foreach ($this->unsupportedFieldsInCountRequest as $field) { if (isset($request[$field])) { unset($request[$field]); } } $response = $this->elasticSearchClient->getIndex()->request('GET', '/_count', [], json_encode($request)); $timeAfterwards = microtime(true); $treatedContent = $response->getTreatedContent(); $count = $treatedContent['count']; if ($this->logThisQuery === true) { $this->logger->log('Count Query Log (' . $this->logMessage . '): ' . json_encode($request) . ' -- execution time: ' . ($timeAfterwards - $timeBefore) * 1000 . ' ms -- Total Results: ' . $count, LOG_DEBUG); } return $count; }
/** * Remove old indices which are not active anymore (remember, each bulk index creates a new index from scratch, * making the "old" index a stale one). * * @return array<string> a list of index names which were removed */ public function removeOldIndices() { $aliasName = $this->searchClient->getIndexName(); // The alias name is the unprefixed index name $currentlyLiveIndices = array_keys($this->searchClient->request('GET', '/*/_alias/' . $aliasName)->getTreatedContent()); $indexStatus = $this->searchClient->request('GET', '/_status')->getTreatedContent(); $allIndices = array_keys($indexStatus['indices']); $indicesToBeRemoved = array(); foreach ($allIndices as $indexName) { if (strpos($indexName, $aliasName . '-') !== 0) { // filter out all indices not starting with the alias-name, as they are unrelated to our application continue; } if (array_search($indexName, $currentlyLiveIndices) !== FALSE) { // skip the currently live index names from deletion continue; } $indicesToBeRemoved[] = $indexName; } if (count($indicesToBeRemoved) > 0) { $this->searchClient->request('DELETE', '/' . implode(',', $indicesToBeRemoved) . '/'); } return $indicesToBeRemoved; }