Teguh Arief

Creation Search Engine on Laravel Sites Using Solr

Illustrated guide for creation search engine on Laravel sites using Solr.

Teguh Arief

Published on: September 24, 2020

Share:

In today's internet world, a robust search engine is an indispensable tool. This article focuses on the creation search engine on Laravel sites using Solr, demonstrating how easily Solr can be integrated to power database searches within your Laravel applications. We'll guide you through implementing Solr and its PHP bridge, the Solarium library, within the Laravel framework.

Solr configuration

First, configure Solr by setting up `config/solr.php`:

 [
        'localhost' => [
              'host' => env('SOLR_HOST', '192.168.99.100'),
              'port' => env('SOLR_PORT', '8983'),
              #'path' => env('SOLR_PATH', '/solr/'),
              'core' => env('SOLR_CORE', 'mycore')
       ]
    ]
];

Install the PHP Solarium library

To facilitate the creation search engine on Laravel sites using Solr, you'll need the Solarium library. Install it via Composer:

composer require solarium/solarium

Boot up the Solarium Client

Next, create a service provider to boot the Solarium Client:

php artisan make:provider SolariumServiceProvider

Edit `app/Providers/SolariumServiceProvider.php` to bind the Solarium Client:

app->bind(Client::class, function ($app) {
            return new Client($app['config']['solr']);
        });
    }

    public function provides()
    {
        return [Client::class];
    }
}

Now, open `app/config/app.php` and add the `SolariumServiceProvider` to your application providers:

return [
    'providers' => [
        // List off others providers...
        App\Providers\SolariumServiceProvider::class,
    ]
];

The integration

For the creation search engine on Laravel sites using Solr, you'll integrate the search functionality through a controller. Create a new controller:

php artisan make:controller SolariumController

Add the search logic to `app/Http/Controllers/SolariumController.php`:

client = $client;
    }

    public function search()
    {
        $input = Input::get('q');
        $query = $this->client->createSelect();
        $query->setQuery('*:*');
        $query->setQuery('name:"' . $input . '"');
        $query->setStart(0);
        $query->setRows(10);
        $resultset = $this->client->select($query);

        return view('search', compact('resultset'));

    }
}

Add the search results view to `resources/views/search.blade.php`:

        @if (count($resultset) > 0)
            @foreach ($resultset as $data)
                {{ $data->name }}
            @endforeach
        @else
            Data Not Found.
        @endif

Finally, add the search route to your `app/Http/routes.php` file:

Route::get('/search', 'SolariumController@search');

Related Posts

An illustration depicting the process of importing MySQL databases using Solr DataImportHandler.

Importing MySQL Databases Using Solr DataImportHandler

Streamline MySQL database imports with Solr DataImportHandler! This guide covers setup, config, and execution for efficient data indexing.

Read More
An illustration demonstrating the process of running Nginx virtual host for Laravel sites using Docker.

Running Nginx Virtual Host for Laravel Sites Using Docker

Run Laravel sites on Nginx virtual hosts using Docker. Step-by-step guide for setting up your local development environment efficiently.

Read More
Building a NestJS CRUD application with MongoDB and Mongoose for a superior backend.

NestJS CRUD with MongoDB using Mongoose

Learn to build a robust backend with NestJS, covering CRUD operations using MongoDB and Mongoose, from setup to creating, reading, and updating.

Read More