elastic search multi client configuration

This commit is contained in:
Christopher Timm 2014-09-12 16:42:56 +02:00
parent ea529109d3
commit 980771c34a
8 changed files with 111 additions and 323 deletions

View File

@ -157,7 +157,7 @@ class tx_dlf_elasticsearch {
*
* @return tx_dlf_solr Instance of this class
*/
public static function getInstance() {
public static function getInstance($conf) {
// // Save parameter for logging purposes.
// $_core = $core;
@ -183,20 +183,20 @@ class tx_dlf_elasticsearch {
// }
// Check if there is an instance in the registry already.
if (is_object(self::$registry) && self::$registry instanceof self) {
if (is_object(self::$registry[$conf]) && self::$registry[$conf] instanceof self) {
// Return singleton instance if available.
return self::$registry;
return self::$registry[$conf];
}
print_r($conf);print_r("<br><br>");
// Create new instance...
$instance = new self();
$instance = new self($conf);
// ...and save it to registry.
if ($instance->ready) {
self::$registry[] = $instance;
self::$registry[$conf] = $instance;
// Return new instance.
return $instance;
@ -265,11 +265,7 @@ class tx_dlf_elasticsearch {
// $results = $this->service->search((string) $query, 0, $this->limit, $this->params);
$results = $this->service->search((string) $query);
$this->cPid = 9;
// print_r($results);
// $this->numberOfHits = count($results->response->docs);
//$this->cPid = 9;
$this->numberOfHits = $results['hits']['total'];
@ -301,150 +297,21 @@ class tx_dlf_elasticsearch {
// Keep track of relevance.
$i = 0;
// print_r($results['hits']['hits']);
// print_r('<br>');
foreach ($results['hits']['hits'] as $doc){
$toplevel[] = array (
'u' => $doc['_source']['PID'],
's' => array(),
'p' => '',
'p' => ''
);
print_r($doc['_source']);
}
// Process results.
/*foreach ($results->response->docs as $doc) {
// Split toplevel documents from subparts.
if ($doc->toplevel == 1) {
// Prepare document's metadata for sorting.
$docSorting = array ();
foreach ($sorting as $index_name => $solr_name) {
if (!empty($doc->$solr_name)) {
$docSorting[$index_name] = (is_array($doc->$solr_name) ? $doc->$solr_name[0] : $doc->$solr_name);
}
}
// Preserve relevance ranking.
if (!empty($toplevel[$doc->uid]['s']['relevance'])) {
$docSorting['relevance'] = $toplevel[$doc->uid]['s']['relevance'];
}
$toplevel[$doc->uid] = array (
'u' => $doc->uid,
's' => $docSorting,
'p' => (!empty($toplevel[$doc->uid]['p']) ? $toplevel[$doc->uid]['p'] : array ())
);
} else {
$toplevel[$doc->uid]['p'][] = $doc->id;
if (!in_array($doc->uid, $checks)) {
$checks[] = $doc->uid;
}
}
// Add relevance to sorting values.
if (empty($toplevel[$doc->uid]['s']['relevance'])) {
$toplevel[$doc->uid]['s']['relevance'] = str_pad($i, 6, '0', STR_PAD_LEFT);
}
$i++;
}*/
// Check if the toplevel documents have metadata.
/*foreach ($checks as $check) {
if (empty($toplevel[$check]['u'])) {
// Get information for toplevel document.
$result = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
'tx_dlf_documents.uid AS uid,tx_dlf_documents.metadata_sorting AS metadata_sorting',
'tx_dlf_documents',
'tx_dlf_documents.uid='.intval($check).tx_dlf_helper::whereClause('tx_dlf_documents'),
'',
'',
'1'
);
// Process results.
if ($GLOBALS['TYPO3_DB']->sql_num_rows($result)) {
$resArray = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result);
// Prepare document's metadata for sorting.
$sorting = unserialize($resArray['metadata_sorting']);
if (!empty($sorting['type']) && tx_dlf_helper::testInt($sorting['type'])) {
$sorting['type'] = tx_dlf_helper::getIndexName($sorting['type'], 'tx_dlf_structures', $this->cPid);
}
if (!empty($sorting['owner']) && tx_dlf_helper::testInt($sorting['owner'])) {
$sorting['owner'] = tx_dlf_helper::getIndexName($sorting['owner'], 'tx_dlf_libraries', $this->cPid);
}
if (!empty($sorting['collection']) && tx_dlf_helper::testInt($sorting['collection'])) {
$sorting['collection'] = tx_dlf_helper::getIndexName($sorting['collection'], 'tx_dlf_collections', $this->cPid);
}
// Preserve relevance ranking.
if (!empty($toplevel[$check]['s']['relevance'])) {
$sorting['relevance'] = $toplevel[$check]['s']['relevance'];
}
$toplevel[$check] = array (
'u' => $resArray['uid'],
's' => $sorting,
'p' => $toplevel[$check]['p']
);
} else {
// Clear entry if there is no (accessible) toplevel document.
unset ($toplevel[$check]);
}
}
}*/
// Save list of documents.
$list = t3lib_div::makeInstance('tx_dlf_list');
$list->reset();
// $toplevel[1] = array (
// 'u' => 'qucosa:1077',
// 's' => array(),
// 'p' => '',
// );
$list->add(array_values($toplevel));
// Set metadata for search.
@ -457,7 +324,7 @@ class tx_dlf_elasticsearch {
'select' => $query,
'userid' => 0,
'params' => $this->params,
'core' => $this->core,
// 'core' => $this->core,
'pid' => $this->cPid,
'order' => 'relevance',
'order.asc' => TRUE,
@ -634,12 +501,10 @@ class tx_dlf_elasticsearch {
*
* @return void
*/
protected function __construct() {
protected function __construct($elasticsearchConf) {
$extensionPath = t3lib_extMgm::extPath('dlf');
require_once $extensionPath . 'lib/ElasticSearchPhpClient/vendor/autoload.php';
//require_once(\TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName('EXT:'.self::$extKey.'/lib/ElasticSearchPhpClient/src/ElasticSearch/Client.php'));
@ -653,15 +518,17 @@ class tx_dlf_elasticsearch {
$port = tx_dlf_helper::intInRange($conf['elasticSearchPort'], 1, 65535, 9200);
// index
$index = $conf['elasticSearchIndex'];
// $index = $conf['elasticSearchIndex'];
// //type
// $type = $conf['elasticSearchType'];
// index
$this->index = $elasticsearchConf[0];
//type
$type = $conf['elasticSearchType'];
$this->type = $elasticsearchConf[1];
// configuration array for elasticsearch client
$params = array();
// $params['hosts'] =
if ($conf['elasticSearchUser'] && $conf['elasticSearchPass']) {
@ -674,77 +541,21 @@ class tx_dlf_elasticsearch {
}
// establish connection
$this->service = Client::connection(array(
'servers' => $host.':'.$port,
'protocol' => 'http',
'index' => 'fedora',
'type' => 'object'
'index' => $this->index,
'type' => $this->type
// 'index' => $index,
// 'type' => $type
));
// $result = $this->service->search('name:Charlie');
// print_r("test");
// print_r($result);
// Instantiation successful!
$this->ready = TRUE;
//$this->service = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('Client', $params);
// $this->service->setIndex();
// $this->service->setType();
// $this->service->connection($host.':'.$port);
//$es = Client::connection('http://192.168.2.230:9200/slub/goobi');
// // Load class.
// if (!class_exists('Apache_Solr_Service')) {
// require_once(t3lib_div::getFileAbsFileName('EXT:'.self::$extKey.'/lib/SolrPhpClient/Apache/Solr/Service.php'));
// }
// // Get Solr credentials.
// $conf = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf'][self::$extKey]);
// $host = ($conf['solrHost'] ? $conf['solrHost'] : 'localhost');
// // Prepend username and password to hostname.
// if ($conf['solrUser'] && $conf['solrPass']) {
// $host = $conf['solrUser'].':'.$conf['solrPass'].'@'.$host;
// }
// // Set port if not set.
// $port = tx_dlf_helper::intInRange($conf['solrPort'], 1, 65535, 8180);
// // Append core name to path.
// $path = trim($conf['solrPath'], '/').'/'.$core;
// // Instantiate Apache_Solr_Service class.
// $this->service = t3lib_div::makeInstance('Apache_Solr_Service', $host, $port, $path);
// // Check if connection is established.
// if ($this->service->ping() !== FALSE) {
// // Do not collapse single value arrays.
// $this->service->setCollapseSingleValueArrays = FALSE;
// // Set core name.
// $this->core = $core;
// // Instantiation successful!
// $this->ready = TRUE;
// }
}
}

View File

@ -316,45 +316,29 @@ class tx_dlf_list implements ArrayAccess, Countable, Iterator, t3lib_Singleton {
// connection established
if($this->elasticsearchConnect()) {
print_r($this->es);
// get result from record id
$this->es->service->setIndex("fedora")->setType("object");
$result = $this->es->service->get($record['uid']);
print_r($result);
$metadata = array();
// store metadata
foreach ($result as $key => $entry) {
print_r($key);print_r($entry);print_r("<br>");
$test = array_walk_recursive($result, function($entry, $key) use(&$metadata) {
$metadata[$key] = array($key => $entry);
}
});
// print_r("<br><br>");
// print_r($result);
// foreach ($result as $key => $entry) {
// $metadata[$key] = array($key => $entry);
// }
}
// Direkter aufruf des dokuments in elasticsearch
// $metadata['Author'] = array('Author' => 'DanBrown');
// $metadata['title'] = array('title' => 'Titel');
// $metadata['author'] = 'Autor';
// $metadata['LABEL'] = array('LABEL' => 'DasIstEinLabel');
$record['metadata'] = $metadata;
// $record['subparts'][1] = array (
// 'uid' => 1,
// 'page' => 9,
// 'thumbnail' => '',
// 'metadata' => $metadata
// );
}
// Unset subparts without any metadata.
foreach ($record['subparts'] as $id => $subpart) {
if (!is_array($subpart)) {
unset ($record['subparts'][$id]);
}
}
// Save record for later usage.
@ -390,6 +374,8 @@ class tx_dlf_list implements ArrayAccess, Countable, Iterator, t3lib_Singleton {
}
/**
* This moves the element at the given position up or down
*
@ -670,39 +656,29 @@ class tx_dlf_list implements ArrayAccess, Countable, Iterator, t3lib_Singleton {
protected function elasticsearchConnect() {
if(!$this->es){
$this->es = tx_dlf_elasticsearch::getInstance();
// Load index configuration.
$result = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
'tx_dlf_elasticsearchindexes.index_name, tx_dlf_elasticsearchindexes.type_name',
'tx_dlf_elasticsearchindexes',
'tx_dlf_elasticsearchindexes.pid='.intval($this->metadata['options']['pid']).tx_dlf_helper::whereClause('tx_dlf_elasticsearchindexes'),
'',
'',
''
);
while ($resArray = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result)) {
$conf[0] = $resArray['index_name'];
$conf[1] = $resArray['type_name'];
}
if (!$this->es = tx_dlf_elasticsearch::getInstance($conf)) {
return FALSE;
}
}
// // Get Solr instance.
// if (!$this->solr) {
// // Connect to Solr server.
// if ($this->solr = tx_dlf_solr::getInstance($this->metadata['options']['core'])) {
// // Load index configuration.
// $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
// 'tx_dlf_metadata.index_name AS index_name,tx_dlf_metadata.tokenized AS tokenized,tx_dlf_metadata.indexed AS indexed',
// 'tx_dlf_metadata',
// 'tx_dlf_metadata.is_listed=1 AND tx_dlf_metadata.pid='.intval($this->metadata['options']['pid']).tx_dlf_helper::whereClause('tx_dlf_metadata'),
// '',
// 'tx_dlf_metadata.sorting ASC',
// ''
// );
// while ($resArray = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result)) {
// $this->solrConfig[$resArray['index_name']] = $resArray['index_name'].'_'.($resArray['tokenized'] ? 't' : 'u').'s'.($resArray['indexed'] ? 'i' : 'u');
// }
// } else {
// return FALSE;
// }
// }
return TRUE;
}

View File

@ -58,8 +58,8 @@ elasticSearchHost = localhost
# cat=Elasticsearch; type=int[0-65535]; label=LLL:EXT:dlf/locallang.xml:config.elasticSearchPort
elasticSearchPort = 9200
# cat=Elasticsearch; type=string; label=LLL:EXT:dlf/locallang.xml:config.elasticSearchPath
elasticSearchPath = /myindex/
# cat=Elasticsearch; type=string; label=LLL:EXT:dlf/locallang.xml:config.elasticSearchIndex
elasticSearchIndex = myindex
# cat=Elasticsearch; type=string; label=LLL:EXT:dlf/locallang.xml:config.elasticSearchType
elasticSearchType = /myindex/

View File

@ -178,7 +178,7 @@ $TCA['tx_dlf_elasticsearchindexes'] = array (
'iconfile' => t3lib_extMgm::extRelPath($_EXTKEY).'res/icons/txdlfsolrcores.png',
'rootLevel' => -1,
'dividers2tabs' => 2,
'searchFields' => 'label,index_name',
'searchFields' => 'label,index_name, type_name',
),
'feInterface' => array (
'fe_admin_fieldList' => '',

View File

@ -172,6 +172,7 @@ CREATE TABLE tx_dlf_elasticsearchindexes (
deleted tinyint(4) DEFAULT '0' NOT NULL,
label tinytext NOT NULL,
index_name tinytext NOT NULL,
type_name tinytext NOT NULL,
PRIMARY KEY (uid),
KEY parent (pid),

View File

@ -171,7 +171,7 @@
<label index="config.elasticSearchConnect">Elasticsearch Connection</label>
<label index="config.elasticSearchHost">Elasticsearch Server Host: (default is "localhost")</label>
<label index="config.elasticSearchPort">Elasticsearch Server Port: (default is "9200")</label>
<label index="config.elasticSearchPath">Elasticsearch Server Path: </label>
<label index="config.elasticSearchIndex">Elasticsearch Server Index: </label>
<label index="config.elasticSearchType">Elasticsearch Server Type: </label>
<label index="config.elasticSearchUser">Elasticsearch Server User: (default is "")</label>
<label index="config.elasticSearchPass">Elasticsearch Server Password: (default is "")</label>
@ -361,7 +361,7 @@
<label index="config.elasticSearchConnect">Elasticsearch Verbindung</label>
<label index="config.elasticSearchHost">Elasticsearch Server Host: (Standard ist "localhost")</label>
<label index="config.elasticSearchPort">Elasticsearch Server Port: (Standard ist "9200")</label>
<label index="config.elasticSearchPath">Elasticsearch Server Pfad: </label>
<label index="config.elasticSearchIndex">Elasticsearch Server Index: </label>
<label index="config.elasticSearchType">Elasticsearch Server Type: </label>
<label index="config.elasticSearchUser">Elasticsearch Server Benutzername: (Standard ist "")</label>
<label index="config.elasticSearchPass">Elasticsearch Server Kennwort: (Standard ist "")</label>

View File

@ -372,23 +372,6 @@ class tx_dlf_search extends tx_dlf_plugin {
// Disable caching for this plugin.
$this->setCache(FALSE);
$es = tx_dlf_elasticsearch::getInstance();
// print_r("Search Main");
// print_r($es->service);
//
// print_r("CONFIGURATION PLUGIN SEARCH");
// print_r($this->conf);
if($this->conf['searchengine'] == "elasticsearch"){
print_r("ELASTICSEARCH AUSGEWÄHLT FÜR DIE SUCHE");
// print_r($this->piVars['query']);
$result2 = $es->search($this->piVars['query']);
// print_r('<br>');
// print_r($result2);
} else {
print_r("SOLR AUSGEWÄHLT FÜR DIE SUCHE");
}
// Quit without doing anything if required variables are not set.
if (empty($this->conf['solrcore'])) {
@ -461,15 +444,26 @@ class tx_dlf_search extends tx_dlf_plugin {
return $this->pi_wrapInBaseClass($content);
} else {
// switch between elasticsearch and solr
if($this->conf['searchengine'] == "elasticsearch"){
print_r("ELASTICSEARCH AUSGEWÄHLT FÜR DIE SUCHE");
// print_r($this->piVars['query']);
$result2 = $es->search($this->piVars['query']);
//print_r($result2->getResults());
//print_r($this->piVars);
// get elasticsearch configuration
$result = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
'tx_dlf_elasticsearchindexes.index_name,tx_dlf_elasticsearchindexes.type_name',
'tx_dlf_elasticsearchindexes',
'tx_dlf_elasticsearchindexes.pid='.$this->conf['pages'].tx_dlf_helper::whereClause('tx_dlf_elasticsearchindexes'),
'',
'',
'1'
);
if ($GLOBALS['TYPO3_DB']->sql_num_rows($result)) {
// Get title information.
$elasticsearchConf = $GLOBALS['TYPO3_DB']->sql_fetch_row($result);
}
$es = tx_dlf_elasticsearch::getInstance($elasticsearchConf);
// Build label for result list.
$label = $this->pi_getLL('search', '', TRUE);
@ -484,10 +478,12 @@ class tx_dlf_search extends tx_dlf_plugin {
$query = $this->piVars['query'];
$results = $es->search($query);
// set elasticsearch configuration
$es->service->setIndex($elasticsearchConf[0]);
$es->service->setType($elasticsearchConf[1]);
print_r("<br>");
print_r($results2);
// search for specified query
$results = $es->search($query);
$results->metadata = array (
'label' => $label,
@ -496,33 +492,29 @@ class tx_dlf_search extends tx_dlf_plugin {
);
$results->save();
// // Clean output buffer.
// t3lib_div::cleanOutputBuffers();
// Clean output buffer.
t3lib_div::cleanOutputBuffers();
// // Keep some plugin variables.
// $linkConf['parameter'] = $this->conf['targetPid'];
// Keep some plugin variables.
$linkConf['parameter'] = $this->conf['targetPid'];
// if (!empty($this->piVars['order'])) {
if (!empty($this->piVars['order'])) {
// $linkConf['additionalParams'] = t3lib_div::implodeArrayForUrl($this->prefixId, array ('order' => $this->piVars['order'], 'asc' => (!empty($this->piVars['asc']) ? '1' : '0')), '', TRUE, FALSE);
$linkConf['additionalParams'] = t3lib_div::implodeArrayForUrl($this->prefixId, array ('order' => $this->piVars['order'], 'asc' => (!empty($this->piVars['asc']) ? '1' : '0')), '', TRUE, FALSE);
// }
}
// // Send headers.
// header('Location: '.t3lib_div::locationHeaderUrl($this->cObj->typoLink_URL($linkConf)));
// Send headers.
header('Location: '.t3lib_div::locationHeaderUrl($this->cObj->typoLink_URL($linkConf)));
// // Flush output buffer and end script processing.
// ob_end_flush();
// Flush output buffer and end script processing.
ob_end_flush();
// exit;
exit;
// print_r('<br>');
// print_r($result2);
} else {
print_r("SOLR AUSGEWÄHLT FÜR DIE SUCHE");
// Instantiate search object.

View File

@ -803,7 +803,7 @@ $TCA['tx_dlf_solrcores'] = array (
$TCA['tx_dlf_elasticsearchindexes'] = array (
'ctrl' => $TCA['tx_dlf_elasticsearchindexes']['ctrl'],
'interface' => array (
'showRecordFieldList' => 'label,index_name',
'showRecordFieldList' => 'label,index_name,type_name',
),
'feInterface' => $TCA['tx_dlf_elasticsearchindexes']['feInterface'],
'columns' => array (
@ -820,7 +820,15 @@ $TCA['tx_dlf_elasticsearchindexes'] = array (
'label' => 'LLL:EXT:dlf/locallang.xml:tx_dlf_elasticsearchindexes.index_name',
'config' => array (
'type' => 'input',
'form_type' => 'none',
'size' => 30,
'max' => 255,
'eval' => 'alphanum,unique',
),
),
'type_name' => array (
'label' => 'LLL:EXT:dlf/locallang.xml:tx_dlf_elasticsearchindexes.index_name',
'config' => array (
'type' => 'input',
'size' => 30,
'max' => 255,
'eval' => 'alphanum,unique',
@ -828,7 +836,7 @@ $TCA['tx_dlf_elasticsearchindexes'] = array (
),
),
'types' => array (
'0' => array ('showitem' => '--div--;LLL:EXT:dlf/locallang.xml:tx_dlf_elasticsearchindexes.tab1, label;;;;1-1-1, index_name;;;;2-2-2'),
'0' => array ('showitem' => '--div--;LLL:EXT:dlf/locallang.xml:tx_dlf_elasticsearchindexes.tab1, label;;;;1-1-1, index_name;;;;2-2-2, type_name;;;;2-2-2'),
),
'palettes' => array (
'1' => array ('showitem' => ''),