Make multiple solr connection with same/different core in codeigniter using solarium.
- Setup Apache solr
- Setup Codeigniter framework
- Setup Solarium
- Make Multiple connection in codeigniter
- Download solr from this link.
- Unzip the package.
- Upload the solr folders and files to your server.
- Set JRE path into solr/bin/solr.cmd
- Solr commands for start, stop and create core for here,
For Windows:
Check solr status
solr status
Start solr connection
solr start (Without port. It default set 8983)
solr start -p 8088 (With port 8088)
Stop solr connection
solr stop -p 8088 (With port 8088)
solr stop -all (All connection)
Create and manage core
> Create Core:
solr create -c test_core
> Delete Core:
solr delete -c test_core
> Import Document into the core:
First put the file into solr folder. like here.
cd solr-8.0.0\example\example1.xml
cd solr-8.0.0\example\example2.xml
java -Dc=directors -jar post.jar *.xml // It upload both example1.xml and example2.xml
java -Dc=directors -jar post.jar example1.xml // It upload both example1.xml only
For Ubentu:
Check solr status
solr sudo service solr status
Start solr connection
sudo service solr start
Stop solr connection
sudo service solr stop
Create and manage core
> Create Core:
sudo su - solr -c "/opt/solr/bin/solr create -c test_core -n data_driven_schema_configs"
> Delete Core:
sudo su - solr -c "/opt/solr/bin/solr delete -c test_core -n data_driven_schema_configs"
Note
- check status, solr start/stop and Core create/delete command are run `serverpath/solr/bin`
- Import document is run `serverpath/solr/example/exampledocs`
- Download codeigniter from this link.
- Unzip the package.
- Upload the CodeIgniter folders and files to your server. Normally the index.php file will be at your root.
- Open the application/config/config.php file with a text editor and set your base URL. If you intend to use encryption or sessions, set your encryption key.
- If you intend to use a database, open the application/config/database.php file with a text editor and set your database settings.
- run your application via server_url (Example: www.example.com or localhost/project_name).
Befor setup solarium, we have following requirements,
- JRE (Java Runtime Environment)
- Solr
- Composer
- Codeigniter
- WAMP/LAMP/XAMPP
- Install solarium pakage via composer.
- composer require solarium/solarium
- Add below autoload.php file into the codeigniter index.php.
- include_once './vendor/autoload.php';
- Create config file name solarium.php. And add below code.
<?php
$config['endpoint1'] = array( // endpoint1 is a connection variable. It not a default keyword.
'endpoint' => array(
'localhost' => array(
'host' => 'host_name', // localhost or www.host.com
'port' => 'port_value', //Default 8983 or 8080 etc,
'path' => '/solr/',
'core' => 'solr_core_name' // core1 or movie1 or etc,
)
)
);
?>
- Start solarium in your controller page.
First load solr client in __construct()
$this->config->load('solarium');
$this->endpoint1 = new Solarium\Client($this->config->item('endpoint1')); // This is used to make connection with solr using 'endpoint1' config variable.
And add below function in your controller
$query = $this->endpoint1->createSelect();
$result = $this->endpoint1->select($query);
The full controller page here,
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Test extends CI_Controller {
public function __construct() {
parent::__construct();
$this->config->load('solarium');
$this->endpoint1 = new Solarium\Client($this->config->item('endpoint1')); // This is used to make connection with solr using 'endpoint1' config variable.
}
public function test() {
$query = $this->endpoint1->createSelect();
$result = $this->endpoint1->select($query);
echo 'NumFound: '.$result->getNumFound() . PHP_EOL;
foreach ($result as $document) {
echo '<hr/><table>';
foreach($document AS $field => $value)
{
// this converts multivalue fields to a comma-separated string
if(is_array($value)) $value = implode(', ', $value);
echo '<tr><th>' . $field . '</th><td>' . $value . '</td></tr>';
}
echo '</table>';
}
}
}
?>
- Run the controller and check the outputs. (Example: server_path/Test/test).
To implement multiple solr connection is very easy and simple. Just add Another set of connection endpoints into the config file like solarium.php.
For Example,
$config['endpoint2'] = array( // endpoint2 is a secound connection variable.
'endpoint' => array(
'localhost' => array(
'host' => 'host_name', // localhost or www.host.com
'port' => 'port_value', //Default 8983 or 8080 etc,
'path' => '/solr/',
'core' => 'solr_core_name' // core1 or movie1 or etc,
)
)
);
Now your config file is,
<?php
$config['endpoint1'] = array( // endpoint1 is a FIRST connection.
'endpoint' => array(
'localhost' => array(
'host' => 'host_name', // localhost or www.host.com
'port' => 'port_value', //Default 8983 or 8080 etc,
'path' => '/solr/',
'core' => 'solr_core_name' // core1 or movie1 or etc,
)
)
);
$config['endpoint2'] = array( // endpoint2 is a secound connection.
'endpoint' => array(
'localhost' => array(
'host' => 'host_name', // localhost or www.host.com
'port' => 'port_value', //Default 8983 or 8080 etc,
'path' => '/solr/',
'core' => 'solr_core_name' // core1 or movie1 or etc,
)
)
);
?>
We use this second connection in controller page using following steps,
- Add New connection endpoints into the __construct()
$this->endpoint2 = new Solarium\Client($this->config->item('endpoint1')); // This is used to make connection with solr using 'endpoint2' config variable.
- Add the below function into the controller page,
$query = $this->endpoint2->createSelect();
$result = $this->endpoint2->select($query);
- Full controller page is here,
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Test extends CI_Controller {
public function __construct() {
parent::__construct();
$this->config->load('solarium');
$this->endpoint1 = new Solarium\Client($this->config->item('endpoint1')); // This is used to make connection with solr using 'endpoint1' config variable.
$this->endpoint2 = new Solarium\Client($this->config->item('endpoint2')); // This is used to make connection with solr using 'endpoint1' config variable.
}
public function test_endpoint1() {
$query = $this->endpoint1->createSelect();
$result = $this->endpoint1->select($query);
echo 'NumFound: '.$result->getNumFound() . PHP_EOL;
foreach ($result as $document) {
echo '<hr/><table>';
foreach($document AS $field => $value)
{
// this converts multivalue fields to a comma-separated string
if(is_array($value)) $value = implode(', ', $value);
echo '<tr><th>' . $field . '</th><td>' . $value . '</td></tr>';
}
echo '</table>';
}
}
public function test_endpoint2() {
$query = $this->endpoint2->createSelect();
$result = $this->endpoint2->select($query);
echo 'NumFound: '.$result->getNumFound() . PHP_EOL;
foreach ($result as $document) {
echo '<hr/><table>';
foreach($document AS $field => $value)
{
// this converts multivalue fields to a comma-separated string
if(is_array($value)) $value = implode(', ', $value);
echo '<tr><th>' . $field . '</th><td>' . $value . '</td></tr>';
}
echo '</table>';
}
}
}
?>
- Run the controller and check the outputs. (Example: server_path/Test/test_endpoint1/ and server_path/Test/test_endpoint2/)