PHP code example of jarir-ahmed / search
1. Go to this page and download the library: Download jarir-ahmed/search library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
jarir-ahmed / search example snippets
use JarirAhmed\Search\SearchManager;
use JarirAhmed\Search\Engines\InMemoryEngine;
use JarirAhmed\Search\Query;
$search = new SearchManager(new InMemoryEngine());
$search->bulk([
'1' => ['title' => 'Blue Running Shoes', 'category' => 'footwear', 'price' => 80],
'2' => ['title' => 'Red Leather Boots', 'category' => 'footwear', 'price' => 150],
]);
// Simple string search
$result = $search->search('blue');
$result->ids(); // ['1']
$result->total(); // 1
$result->documents(); // [['title' => 'Blue Running Shoes', ...]]
// Rich query: boosts, fuzziness, filter, sort, pagination
$result = $search->search(
Query::create('runing') // typo
->fields(['title' => 2.0])
->fuzziness(1) // matches "running"
->filter('category', 'footwear')
->sortBy('price', 'asc')
->from(0)->size(20)
);
use JarirAhmed\Search\SearchManager;
use JarirAhmed\Search\Engines\ElasticsearchEngine;
use JarirAhmed\Search\Transport\CurlTransport;
$transport = new CurlTransport('http://localhost:9200', [
'username' => 'elastic',
'password' => 'changeme',
// or 'apiKey' => '...'
'timeout' => 5,
]);
$search = new SearchManager(new ElasticsearchEngine($transport, 'products'));
$search->index('1', ['title' => 'Blue Running Shoes', 'category' => 'footwear']);
$result = $search->search(Query::create('blue')->fields(['title' => 2.0]));
new CurlTransport('https://APPID-dsn.algolia.net', ['headers' => [
'X-Algolia-Application-Id: APPID',
'X-Algolia-API-Key: KEY',
]]); // Algolia
new CurlTransport('http://127.0.0.1:7700', ['headers' => ['Authorization: Bearer KEY']]); // Meilisearch
new CurlTransport('http://127.0.0.1:8108', ['headers' => ['X-TYPESENSE-API-KEY: KEY']]); // Typesense
use JarirAhmed\Search\Engines\DatabaseEngine;
$engine = new DatabaseEngine(new PDO('sqlite:search.db')); // or MySQL / Postgres
$engine->index('1', ['title' => 'Blue Shoes', 'category' => 'footwear']);
$hits = $engine->search(Query::create('blue')->filter('category', 'footwear'));
use JarirAhmed\Search\Engines\VectorEngine;
$engine = new VectorEngine(); // documents carry an "embedding" field
$engine->index('cat', ['label' => 'cat', 'embedding' => [1.0, 0.0, 0.0]]);
$engine->index('car', ['label' => 'car', 'embedding' => [0.0, 0.0, 1.0]]);
$result = $engine->search(Query::create('')->vector([0.95, 0.05, 0.0]));
$result->ids(); // ['cat', ...] ranked by similarity