PHPackages                             joacub/elastic-search - 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. [API Development](/categories/api)
4. /
5. joacub/elastic-search

ActiveLibrary[API Development](/categories/api)

joacub/elastic-search
=====================

A module that helps integration of elastic search

0.0.2(12y ago)061BSD3PHPPHP &gt;=5.5

Since Sep 17Pushed 9y ago1 watchersCompare

[ Source](https://github.com/joacub/MCNElasticSearch)[ Packagist](https://packagist.org/packages/joacub/elastic-search)[ Docs](https://github.com/macnibblet/MCNElasticSearch)[ RSS](/packages/joacub-elastic-search/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependencies (6)Versions (6)Used By (0)

MCNElasticSearch
================

[](#mcnelasticsearch)

[![Build Status](https://camo.githubusercontent.com/b657a29fe9ce949f674e742bec0d6cfb174fdf52dcc6cb532df3b116573bd507/68747470733a2f2f7472617669732d63692e6f72672f6d61636e6962626c65742f4d434e456c61737469635365617263682e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/macnibblet/MCNElasticSearch)[![Scrutinizer Quality Score](https://camo.githubusercontent.com/fa9eefd2575e4a6516ce2ec86e6d04d2be34eefdd0d6ab248e23ca032340697a/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6d61636e6962626c65742f4d434e456c61737469635365617263682f6261646765732f7175616c6974792d73636f72652e706e673f733d64353932303135363361643537363332356532356464643735393838353138663630363666343866)](https://scrutinizer-ci.com/g/macnibblet/MCNElasticSearch/)[![Code Coverage](https://camo.githubusercontent.com/cb85a950214d8bfa39a7984e95bd0c631cc42b36c2f4b04bdeb64b093274cd28/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6d61636e6962626c65742f4d434e456c61737469635365617263682f6261646765732f636f7665726167652e706e673f733d35646161316466646439383533323531333064323939643666306439356536353633633235346435)](https://scrutinizer-ci.com/g/macnibblet/MCNElasticSearch/)

This is a reasonably simply module that will assist keeping your elastic search index up to date with your database.

You wish to...
--------------

[](#you-wish-to)

- You wish to have a utility for updating / deleting the mapping
- You wish to keep your elastic search synchronized with your ORM
- You wish to have a service that provides you with a simple interface to search and return doctrine entities

Step 1, setup mapping
---------------------

[](#step-1-setup-mapping)

Start by copying the file `config/MCNElasticSearch.global.php` to your `config/autoload/` directory. The types array is a associative array name =&gt; mapping information. For all options in mapping check the `MCNElasticSearch\Options\TypeMappingOptions` currently only basic options are available but PRs are welcome!

Example configuration

```
return [
    'MCNElasticSearch' => [
        'metadata' => [

            /**
             * List of object mappings
             */
            'objects' => [
                'Company\Entity\CompanyEntity' => [
                    'hydrator' => 'company',
                    'type'     => 'companies',
                    'index'    => 'example',
                ]
            ],

            /**
             * List of types E.g "SQL Tables"
             */
            'types' => [
                'companies' => [
                    'index'      => 'example',
                    'source'     => ['enabled' => false],
                    'properties' => [
                        'id'      => ['type' => 'integer'],
                        'name'    => ['type' => 'string'],
                        'address' => [
                            'type'       => 'object',
                            'properties' => [

                                'id'   => ['type' => 'integer'],
                                'type' => ['type' => 'string', 'not_analyzed' => true],

                                'street'         => ['type' => 'string'],
                                'zipcode'        => ['type' => 'integer'],
                                'country'        => ['type' => 'string'],
                                'coordinates'    => ['type' => 'geo_point'],
                            ]
                        ]
                    ]
                ]
            ]
        ]
    ]
];
```

Now that you have setup your mapping we need to run it against our elastic search `php public/index.php es mapping create`And if you wish to delete it `php public/index.php es mapping delete`

Step 2, Setup a synchronizer
----------------------------

[](#step-2-setup-a-synchronizer)

Now we need to implement the synchronizer, and this is dead simple!

```
class ElasticSearchSynchronizer extends \MCNElasticSearch\Listener\AbstractDoctrineORMSynchronizer
{
    /**
     * Check that an object is of the proper instance
     *
     * @param mixed $object
     *
     * @return bool
     */
    public function isValid($object)
    {
        return $object instanceof CompanyEntity;
    }
}
```

You will also need to setup a factory and pass an instance of `MCNElasticSearch\Service\DocumentService` but that is hopefully something that can be removed in the future!

Now we need to tell doctrine publish events to your synchronizer. So in your doctrine configuration you need to add

```
    'eventmanager' => [
        'orm_default' => [
            'subscribers' => [
                ElasticSearchSynchronizer::class
            ]
        ]
    ],
```

Step 3, Perform a search
------------------------

[](#step-3-perform-a-search)

Now im going to continue on the previous example, and take a piece of code from my API written using `PhlyRestfully`and perform a search against my companies type sorting by distance and filtering away all companies further away then 1000km

```
class CompanyResource implements ListenerAggregateInterface
{
    use ListenerAggregateTrait;

    /**
     * @var \Company\Service\CompanyServiceInterface
     */
    protected $companyService;

    /**
     * @var \MCNElasticSearch\Service\SearchServiceInterface
     */
    protected $searchService;

    /**
     * @param CompanyServiceInterface $companyService
     * @param SearchServiceInterface $searchService
     */
    public function __construct(CompanyServiceInterface $companyService, SearchServiceInterface $searchService)
    {
        $this->searchService  = $searchService;
        $this->companyService = $companyService;
    }

    /**
     * Attach one or more listeners
     *
     * Implementors may add an optional $priority argument; the EventManager
     * implementation will pass this to the aggregate.
     *
     * @param EventManagerInterface $events
     *
     * @return void
     */
    public function attach(EventManagerInterface $events)
    {
        $this->listeners[] = $events->attach('fetchAll', [$this, 'fetchAll']);
    }

    /**
     * @param ResourceEvent $event
     * @return \PhlyRestfully\ApiProblem|\Zend\Paginator\Paginator
     */
    public function fetchAll(ResourceEvent $event)
    {
        $coordinates =       $event->getQueryParam('coordinates');
        $maxDistance = (int) $event->getQueryParam('distance', 1000);

        $sort = [
            '_geo_distance' => [
                'companies.address.coordinates' => $coordinates,
                'unit'  => 'km',
                'order' => 'asc'
            ]
        ];

        $geoDistanceFilter = new GeoDistance('companies.address.coordinates', $coordinates, $maxDistance . 'km');

        $query = new Query();
        $query->addSort($sort);
        $query->setFilter($geoDistanceFilter);

        return $this->searchService->search(CompanyEntity::class, $query, SearchServiceInterface::HYDRATE_DOCTRINE_OBJECT);
    }
}
```

Step 4, Hallelujah moment
-------------------------

[](#step-4-hallelujah-moment)

Profits!

###  Health Score

24

—

LowBetter than 31% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~1 days

Total

2

Last Release

4668d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/8c5ea80353eb117dbabb0def7d2a43079815acee0d55f690a6476c03e458c203?d=identicon)[joacub](/maintainers/joacub)

---

Top Contributors

[![joacub](https://avatars.githubusercontent.com/u/2091228?v=4)](https://github.com/joacub "joacub (7 commits)")

---

Tags

modulezf2elastic searchZend Framework

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/joacub-elastic-search/health.svg)

```
[![Health](https://phpackages.com/badges/joacub-elastic-search/health.svg)](https://phpackages.com/packages/joacub-elastic-search)
```

###  Alternatives

[mcn/elastic-search

A module that helps integration of elastic search

191.1k](/packages/mcn-elastic-search)[rcsofttech/audit-trail-bundle

Enterprise-grade, high-performance Symfony audit trail bundle. Automatically track Doctrine entity changes with split-phase architecture, multiple transports (HTTP, Queue, Doctrine), and sensitive data masking.

1175.2k](/packages/rcsofttech-audit-trail-bundle)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
