PHPackages                             ostah/laravel-vespa - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. ostah/laravel-vespa

ActiveLibrary[API Development](/categories/api)

ostah/laravel-vespa
===================

A Laravel package for integrating with Vespa

08PHP

Since Aug 18Pushed 1y ago1 watchersCompare

[ Source](https://github.com/mohmmedwee/laravel-vespa)[ Packagist](https://packagist.org/packages/ostah/laravel-vespa)[ RSS](/packages/ostah-laravel-vespa/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

Laravel Vespa Client
--------------------

[](#laravel-vespa-client)

This package provides a Laravel service provider and client for interacting with a Vespa search engine. It includes features like query building, rate limiting, caching, plugin integration, and more.

### Table of Contents

[](#table-of-contents)

- [Installation](#installation)
- [Configuration](#configuration)
- [Usage](#usage)
    - [Basic Search](#basic-search)
    - [Query Builder](#query-builder)
    - [Caching](#caching)
    - [Custom Plugin](#custom-plugin)
    - [Facades](#facades)
- [Testing](#testing)
- [License](#license)

### Installation

[](#installation)

To get started with the Laravel Vespa Client, you'll need to follow these steps:

1. **Install the package via Composer:**

    ```
    composer require ostah/laravel-vespa
    ```
2. **Publish the configuration file:**

    This will allow you to customize the Vespa settings for your application.

    ```
    php artisan vendor:publish --tag=config --provider="YourVendor\Vespa\VespaServiceProvider"
    ```
3. **Add the necessary environment variables:**

    Add the following lines to your `.env` file:

    ```
    VESPA_URL=http://localhost:8080
    VESPA_API_KEY=your-api-key-here
    VESPA_HTTP_TIMEOUT=30
    VESPA_RATE_LIMIT=100
    VESPA_THROTTLE_LIMIT=10
    VESPA_DEFAULT_LANGUAGE=en
    VESPA_LOG_CHANNEL=default
    VESPA_AUDIT_LOG_CHANNEL=audit
    VESPA_ERROR_LOG_CHANNEL=vespa_errors
    ```

### Configuration

[](#configuration)

The configuration for the Vespa client is stored in `config/vespa.php`. This file includes settings for the Vespa URL, API key, rate limiting, throttling, and logging. You can adjust these settings to match your application's needs.

Here's an example of what the `config/vespa.php` file looks like:

```
return [

    'url' => env('VESPA_URL', 'http://localhost:8080'),

    'api_key' => env('VESPA_API_KEY', null),

    'timeout' => env('VESPA_HTTP_TIMEOUT', 30),

    'rate_limit' => env('VESPA_RATE_LIMIT', 100),

    'throttle_limit' => env('VESPA_THROTTLE_LIMIT', 10),

    'language' => env('VESPA_DEFAULT_LANGUAGE', 'en'),

    'log_channel' => env('VESPA_LOG_CHANNEL', 'default'),
    'audit_log_channel' => env('VESPA_AUDIT_LOG_CHANNEL', 'audit'),
    'error_log_channel' => env('VESPA_ERROR_LOG_CHANNEL', 'vespa_errors'),
];
```

### Usage

[](#usage)

The Laravel Vespa Client provides various ways to interact with the Vespa search engine. Below are some examples of how to use the client in your application.

#### Basic Search

[](#basic-search)

You can perform a basic search using the `VespaClient`:

```
use YourVendor\Vespa\VespaClient;

$vespa = app(VespaClient::class);

$response = $vespa->search('your search query');

dd($response);
```

#### Query Builder

[](#query-builder)

The `VespaQueryBuilder` class allows you to construct complex queries with a fluent API:

```
use YourVendor\Vespa\VespaQueryBuilder;

$builder = new VespaQueryBuilder();
$query = $builder->select(['title', 'description'])
                 ->from('documents')
                 ->where('status', 'published')
                 ->orderBy('created_at', 'desc')
                 ->limit(10)
                 ->getQuery();

$response = $vespa->searchWithBuilder($builder);

dd($response);
```

#### Caching

[](#caching)

To cache search results, you can use the `cachedSearch` method:

```
$response = $vespa->cachedSearch('your search query', [], 600); // Cache for 10 minutes

dd($response);
```

#### Custom Plugin

[](#custom-plugin)

You can create custom plugins to modify queries before they are sent to Vespa. Here’s how you can use a custom plugin:

```
use YourVendor\Vespa\Plugins\ExamplePlugin;

$plugin = new ExamplePlugin(['append' => 'AND status:published']);
$vespa->registerPlugin($plugin);

$response = $vespa->search('your search query');

dd($response);
```

#### Facades

[](#facades)

If you prefer using facades, you can access the `VespaClient` like this:

```
use Vespa;

$response = Vespa::search('your search query');

dd($response);
```

### Testing

[](#testing)

To ensure that the Vespa Client functions correctly, a test suite is provided. The tests cover basic search functionality, query building, caching, plugin integration, rate limiting, and throttling.

To run the tests, use PHPUnit:

```
vendor/bin/phpunit
```

Here’s an example of a basic test class:

```
