PHP code example of geekybones / typesense-bundle

1. Go to this page and download the library: Download geekybones/typesense-bundle 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/ */

    

geekybones / typesense-bundle example snippets


// config/bundles.php
return [
    GeekyBones\TypesenseBundle\TypesenseBundle::class => ['all' => true],
];

use GeekyBones\TypesenseBundle\Attribute\TypesenseCollection;
use GeekyBones\TypesenseBundle\Attribute\TypesenseField;

#[TypesenseCollection(name: 'products')]
#[ORM\Entity]
class Product
{
    #[ORM\Id, ORM\GeneratedValue, ORM\Column]
    private ?int $id = null;

    #[ORM\Column]
    #[TypesenseField(queryBy: true, weight: 3)]
    private string $name;

    #[ORM\Column]
    #[TypesenseField(facet: true)]
    private ?string $brand = null;

    #[ORM\Column]
    #[TypesenseField(sort: true)]
    private int $price = 0;
}

use GeekyBones\TypesenseBundle\Contract\SearcherInterface;

final class SearchController
{
    public function __construct(private SearcherInterface $searcher) {}

    public function __invoke(string $q): array
    {
        $result = $this->searcher->smartSearch(Product::class, $q);

        return ['found' => $result->found, 'items' => $result->documents()];
    }
}