PHP code example of atomicptr / symfony-indexed-search

1. Go to this page and download the library: Download atomicptr/symfony-indexed-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/ */

    

atomicptr / symfony-indexed-search example snippets


Atomicptr\Symfony\IndexedSearch\IndexedSearchBundle::class => ['all' => true],

#[ORM\Entity]
#[Searchable(filters: ['type' => 'product'])] // fixed filters for every product
class Product
{
    #[ORM\Column]
    #[SearchableText(weight: Weight::Highest)] // Highest, High, Normal (default) or Low
    private string $name;

    #[ORM\Column(type: 'text')]
    #[SearchableText(format: TextFormat::Html)] // optional, strips the HTML
    private string $description;

    #[ORM\Column]
    #[SearchableFilter('tenant')]
    private int $tenantId;

    #[ORM\Column]
    #[SearchableDate] // optional, for sorting by date
    private DateTimeImmutable $createdAt;

    #[ORM\Column]
    #[SearchableWhen] // optional, only indexed while true
    private bool $published;
}

$result = $searchService->search(
    'red sho',
    filters: ['tenant' => 42, 'type' => ['product', 'article']],
    sort: SearchSort::Relevance, // or RelevanceWithRecency, Newest, Oldest
    page: 1,
    hydrate: true,
);

foreach ($result->hits as $hit) {
    $hit->entity; // your Product
}

$result->entities(); // or just the Products

$result->total;
$result->hasNextPage;

#[SearchableDocuments]
public function searchDocuments(): iterable
{
    yield Document::create('en')
        ->withText($this->name, Weight::Highest)
        ->withFilter('lang', 'en');

    yield Document::create('de')
        ->withText($this->nameGerman, Weight::Highest)
        ->withFilter('lang', 'de');
}