- Check (SOLR Prequisite)[https://lucene.apache.org/solr/guide/8_6/solr-system-requirements.html]
Note
: for Windows user you might want to use Cygwin to run solr commands. - Download (SOLR)[https://lucene.apache.org/solr/downloads.html]
Note
: for windows user download the.zip
file - Unzip the file
- Run
bin/solr start -p 8983
- Create collection run
bin/solr create_collection -c collection1 -p 8983
PS: during this documentation SOLR version is 8.6.1
- Create new project by running
laravel new solarium
- Create a file name
solarium.php
inside config folder
<?php
return [
'endpoint' => [
'localhost' => [
'host' => env('SOLR_HOST', '127.0.0.1'),
'port' => env('SOLR_PORT', '8983'),
'path' => env('SOLR_PATH', '/'),
'core' => env('SOLR_CORE', 'collection1')
]
]
];
- Install php solarium package
composer require solarium/solarium
- Create service provider for the solarium
php artisan make:provider SolariumServiceProvider
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Solarium\Client;
use Solarium\Core\Client\Adapter\Curl;
use Symfony\Component\EventDispatcher\EventDispatcher;
class SolariumServiceProvider extends ServiceProvider
{
protected $defer = true;
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$adapter = new Curl();
$eventDispatcher = new EventDispatcher();
$this->app->bind(Client::class, function ($app) use ($adapter, $eventDispatcher) {
return new Client($adapter, $eventDispatcher, $app['config']['solarium']);
});
}
public function provides()
{
return [Client::class];
}
}
- In
config/app.php
include the created SolariumProvider in the other application providers
// List off others providers...
App\Providers\SolariumServiceProvider::class,
- Create controller
php artisan make:controller SolariumController
<?php
namespace App\Http\Controllers;
class SolariumController extends Controller
{
protected $client;
public function __construct(\Solarium\Client $client)
{
$this->client = $client;
}
public function ping()
{
// create a ping query
$ping = $this->client->createPing();
// execute the ping query
try {
$this->client->ping($ping);
return response()->json('OK');
} catch (\Solarium\Exception $e) {
return response()->json('ERROR', 500);
}
}
}
- Create ping route under routes/web.php
Route::get('/ping', 'SolariumController@ping');
- Access ping route it should reponse OK
Getting following error in laravel 5.8, php 7.3 & solr 5.5.5 & solarium 6.0
Solarium \ Exception \ HttpException (404)
Solr HTTP error: Neither collection nor core set. (404) #0 /var/www/html/solr-acat/vendor/solarium/solarium/src/Core/Client/Adapter/AdapterHelper.php(38): Solarium\Core\Client\Endpoint->getBaseUri() #1 /var/www/html/solr-acat/vendor/solarium/solarium/src/Core/Client/Adapter/Curl.php(80): Solarium\Core\Client\Adapter\AdapterHelper::buildUri(Object(Solarium\Core\Client\Request), Object(Solarium\Core\Client\Endpoint)) #2 /var/www/html/solr-acat/vendor/solarium/solarium/src/Core/Client/Adapter/Curl.php(186): Solarium\Core\Client\Adapter\Curl->createHandle(Object(Solarium\Core\Client\Request), Object(Solarium\Core\Client\Endpoint)) #3 /var/www/html/solr-acat/vendor/solarium/solarium/src/Core/Client/Adapter/Curl.php(39): Solarium\Core\Client\Adapter\Curl->getData(Object(Solarium\Core\Client\Request),