aternovtsii/search-bundle

Symfony bundle for easy integration with elastic/opensearch search engine and doctrine

Installs: 143

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 1

Open Issues: 0

Type:symfony-bundle

v1.0.1 2024-04-01 13:50 UTC

This package is auto-updated.

Last update: 2024-04-01 13:51:42 UTC


README

composer require aternovtsii/search-bundle

To enable bundle add the following to your config/bundles.php file

<?php

return [
    // ...
    ATSearchBundle\ATSearchBundle::class => ['all' => true],
];

To enable additional doctrine filters add the following to your config/doctrine.yaml file

doctrine:
    orm:
        dql:
            numeric_functions:
                IFNULL: ATSearchBundle\Doctrine\Extensions\Query\IfNull
                JSON_CONTAINS: ATSearchBundle\Doctrine\Extensions\Query\JsonContains
                RAND: ATSearchBundle\Doctrine\Extensions\Query\Rand

Configure bundle in your config/packages/at_search.yaml file

at_search:
    search:
        enabled: true
        client: OpenSearch\Client # OpenSearch\Client or Elasticsearch\Client
        mappings:
            App:
                namespace: App\Entity
                dir: '%kernel.project_dir%/src/Entity'

To create OpenSearch Client add the following to your config/services.yaml file

    OpenSearch\Client:
        factory: ['OpenSearch\ClientBuilder', 'fromConfig']
        arguments:
            $config:
                hosts: ['%env(OPENSEARCH_URL)%']

To create ElasticSearch Client add the following to your config/services.yaml file

    Elasticsearch\Client:
        factory: ['Elasticsearch\ClientBuilder', 'fromConfig']
        arguments:
            $config:
                hosts: ['%env(ELASTICSEARCH_URL)%']

Usage example

<?php

namespace App\Entity;

use ATSearchBundle\Annotation as ATSearch;
use Doctrine\ORM\Mapping as ORM;

#[ATSearch\Index]
#[ORM\Entity]
class User
{
    #[ATSearch\FieldId]
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    public ?int $id = null;

    #[ATSearch\FieldString]
    #[ORM\Column(length: 128)]
    public ?string $firstName = null;

    #[ATSearch\FieldMultiString(subFields: 'email')]
    #[ORM\OneToMany(mappedBy: 'user', targetEntity: Email::class)]
    public Collection $emails;

To enable default reindex based on Doctrine events add the following to your config/packages/at_search.yaml file

at_search:
    search:
        enable_update_events: true # default false