Home > Web Development > Laravel Tutorials > How to easily integrate Typesense in Laravel

How to easily integrate Typesense in Laravel

Diego Cortés
Diego Cortés
January 20, 2025
How to easily integrate Typesense in Laravel

In web application development, having an efficient search tool can make a difference in user experience. Typesense is a document-oriented search engine that offers speed and ease of use. In this article, we present a step-by-step guide to integrating Typesense into your Laravel application without complications.

What is Typesense?

Typesense is an open-source search engine that enables fast and relevant searches across large volumes of data. Its goal is to provide a simple and quick search API, ideal for applications that require efficient data handling. This makes it an attractive option for developers looking to optimize the search experience in their web applications.

Prerequisites

Before you begin, ensure that you have the following installed:

  • Laravel: The version you are using.
  • Composer: To manage PHP dependencies.
  • Typesense: You can either use a local installation or a cloud service.

Installing Typesense

To integrate Typesense with Laravel, you first need to install the Typesense PHP package. Open your terminal and run the following command:

composer require typesense/typesense-php

This will download and install the necessary package for your application to communicate with Typesense.

Configuring Typesense

Now that you have installed the package, it’s time to configure the Typesense client. Navigate to the .env file of your Laravel project and add the following variables:

TYPESENSE_API_KEY=your_api_key
TYPESENSE_HOST=localhost
TYPESENSE_PORT=8108
TYPESENSE_PROTOCOL=http

Be sure to replace your_api_key with the API key you have configured on your Typesense server.

To create an instance of the client at the application level, you can do this in one of your configuration files or create a new TypesenseServiceProvider.php file in the app/Providers folder.

// app/Providers/TypesenseServiceProvider.php
namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Typesense\Client;

class TypesenseServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->singleton(Client::class, function ($app) {
            return new Client([
                'api_key' => env('TYPESENSE_API_KEY'),
                'nodes' => [
                    [
                        'host' => env('TYPESENSE_HOST'),
                        'port' => env('TYPESENSE_PORT'),
                        'protocol' => env('TYPESENSE_PROTOCOL'),
                    ]
                ],
                'connection_timeout_seconds' => 2, // Timeout in seconds
            ]);
        });
    }
}

Remember to register your service provider in the config/app.php file under the providers section.

Creating an Index

Once you have your client configured, you can now create an index. This can be done in a controller. Below is a basic example of how to create an index for your data.

use Typesense\Client;

public function createIndex(Client $client)
{
    $client->collections->create([
        'name' => 'products',
        'fields' => [
            ['name' => 'name', 'type' => 'string', 'facet' => false],
            ['name' => 'price', 'type' => 'float', 'facet' => false],
            ['name' => 'category', 'type' => 'string', 'facet' => true],
        ],
    ]);
}

Indexing Documents

With the index created, the next step is to index your documents. For this, you can create an array and send it to the index you just created.

public function indexDocuments(Client $client)
{
    $client->collections['products']->documents->create([
        'id' => '1',
        'name' => 'Product 1',
        'price' => 100.0,
        'category' => 'Electronics',
    ]);
}

Performing Searches

Finally, you can perform searches on your index using the following code:

$results = $client->collections['products']->documents->search('Product');

This will return the documents that match your query.

Conclusion

Integrating Typesense into your Laravel application is a straightforward process and will enhance the search experience for your users. If you want to learn more about related topics or explore other implementations, I invite you to keep reading more news and tutorials on my blog.

Let me know if you need any further assistance!

Diego Cortés
Diego Cortés
Full Stack Developer, SEO Specialist with Expertise in Laravel & Vue.js and 3D Generalist

Categories

Page loaded in 30.89 ms