apisearch-io/php-client
Official PHP client for the ApiSearch.io search-as-a-service platform. Provides a simple API to index documents, manage indices and queries, and retrieve fast, relevant search results from your PHP or Laravel applications.
Installation
composer require apisearch-io/php-client
Ensure your Laravel project meets PHP 8.0+ requirements.
First Use Case: Searching Documents
Initialize the client in config/services.php:
'apisearch' => [
'api_key' => env('APISEARCH_API_KEY'),
'endpoint' => env('APISEARCH_ENDPOINT', 'https://api.apisearch.io'),
],
Then register the service provider in config/app.php:
'providers' => [
// ...
Apisearch\Client\ApisearchServiceProvider::class,
],
Basic Search Query Inject the client via Laravel’s DI:
use Apisearch\Client\ApisearchClient;
public function search(ApisearchClient $client)
{
$results = $client->search('documents', [
'query' => 'laravel framework',
'limit' => 10,
]);
return response()->json($results);
}
Index Management
$client->index('products')->createOrUpdate([
'id' => '123',
'title' => 'Laravel Package',
'description' => 'A PHP package for Laravel...',
]);
$client->index('products')->bulk([
['id' => '1', 'name' => 'Item 1'],
['id' => '2', 'name' => 'Item 2'],
]);
Advanced Queries
$results = $client->search('products', [
'query' => 'package',
'filter' => [
'price' => ['gt' => 10],
'category' => 'laravel',
],
]);
$results = $client->search('products', [
'aggregations' => [
'categories' => ['field' => 'category'],
],
]);
Integration with Laravel Ecosystem
use Apisearch\Client\Jobs\IndexDocument;
IndexDocument::dispatch($client, 'products', $documentData);
$cacheKey = 'apisearch_products_laravel';
$results = Cache::remember($cacheKey, now()->addHours(1), function () use ($client) {
return $client->search('products', ['query' => 'laravel']);
});
Webhook Handling
HandleIncomingWebhook:
public function handle(Request $request)
{
$payload = $request->json()->all();
// Process webhook (e.g., sync local DB)
}
Rate Limiting
$client->setMiddleware(function ($request, $next) {
if ($request->hasHeader('x-rate-limit-remaining') && $request->header('x-rate-limit-remaining') == '0') {
sleep(1);
}
return $next($request);
});
Pagination Quirks
offset and limit for cursor-based pagination:
$results = $client->search('products', [
'query' => 'laravel',
'limit' => 20,
'offset' => 0, // Start from 0
]);
Schema Validation
validate: true in bulk operations:
$client->index('products')->bulk($data, ['validate' => true]);
$client->setDebug(true); // Logs requests/responses to storage/logs/apisearch.log
php artisan tinker
>>> $client = app(Apisearch\Client\ApisearchClient::class);
>>> $client->ping(); // Returns API status
Custom HTTP Client Override the default Guzzle client:
$client->setHttpClient(new CustomGuzzleClient());
Event Listeners
Subscribe to index events (e.g., IndexCreated):
event(new IndexCreated('products', $indexData));
Local Testing
Use the APISEARCH_ENDPOINT env var to point to a local instance:
APISEARCH_ENDPOINT=http://localhost:9200
How can I help you explore Laravel packages today?