PHPackages                             ayaou/db-search-bundle - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Search &amp; Filtering](/categories/search)
4. /
5. ayaou/db-search-bundle

ActiveSymfony-bundle[Search &amp; Filtering](/categories/search)

ayaou/db-search-bundle
======================

This bundle helps indexing and searching entities in a database

v1.1.0(1y ago)258MITPHPPHP &gt;=8.0

Since Feb 16Pushed 1y ago1 watchersCompare

[ Source](https://github.com/ayaou/db-search-bundle)[ Packagist](https://packagist.org/packages/ayaou/db-search-bundle)[ Docs](https://github.com/ayaou/db-search-bundle)[ RSS](/packages/ayaou-db-search-bundle/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (2)Dependencies (5)Versions (4)Used By (0)

🔍 DB Search Bundle
==================

[](#-db-search-bundle)

📌 Overview
----------

[](#-overview)

The **DB Search Bundle** helps you search text inside your database using Doctrine and Symfony.

✅ No need for Elasticsearch or other search engines!

✅ Works with your existing database!

⚠️ This is a **simple** and **lightweight** search solution for small to medium-sized projects. It does not support advanced features like fuzzy matching or search ranking.

📥 Installation
--------------

[](#-installation)

First, install the bundle using Composer:

```
composer require ayaou/db-search-bundle
```

Then, create the database table for indexing:

```
php bin/console make:migration
php bin/console doctrine:migrations:migrate
```

🗃️ Database Schema
------------------

[](#️-database-schema)

The migration creates a table called `db_search_index`. Here is an example for MySQL/MariaDB:

```
CREATE TABLE db_search_index (
    id INT AUTO_INCREMENT NOT NULL,
    entity_class VARCHAR(255) NOT NULL,
    entity_id VARCHAR(255) NOT NULL,
    index_name VARCHAR(255) NOT NULL,
    indexed_text LONGTEXT NOT NULL,
    indexed_at DATETIME DEFAULT NULL COMMENT '(DC2Type:datetime_immutable)',
    INDEX IDX_934BE820F5A3A655 (index_name),
    INDEX IDX_934BE82041BF2C66 (entity_class),
    INDEX IDX_934BE82081257D5D (entity_id),
    FULLTEXT INDEX IDX_934BE82097A6C9C3 (indexed_text),
    PRIMARY KEY(id)
) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB;
```

⚙️ Configuration
----------------

[](#️-configuration)

Define your indexes and entities in `config/packages/db_search.yaml`:

```
# config/packages/db_search.yaml
db_search:
  indexes:
    main:
      entities:
        App\Entity\MyEntity:
          fields:
            - title
            - description
        App\Entity\AnotherEntity:
          fields:
            - fullName
            - formattedAddress
```

⚠️ **Important:** Each entity **must** have a `getId()` method that returns a unique ID.

### 🏷️ Indexing Rules

[](#️-indexing-rules)

✅ You can create multiple indexes for different entities or fields.

✅ Each entity can have multiple fields indexed.

✅ Fields can be **public properties** or **public methods**.

✅ Indexed fields must return **text, numbers, or boolean values** (string, int, float, bool).

🔎 How Indexing &amp; Search Works
---------------------------------

[](#-how-indexing--search-works)

For each field you configure in an entity, the bundle will:

1. Take the field values.
2. Concatenate them into one long text string, separated by spaces.
3. Store the result in the `indexed_text` column.

When you search for a keyword, the bundle will:

1. Look for the keyword inside the `indexed_text` column.
2. Return matching results based on the index configuration.

🔎 Usage
-------

[](#-usage)

### Searching the Database

[](#searching-the-database)

Use the `Searcher` service (`@db_search.searcher`) to search in the database:

```
