PHPackages                             mcn/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. mcn/elastic-search

ActiveLibrary[API Development](/categories/api)

mcn/elastic-search
==================

A module that helps integration of elastic search

1.0.1(11y ago)191.1k6[4 issues](https://github.com/macnibblet/MCNElasticSearch/issues)BSD3PHPPHP &gt;=5.5

Since Sep 17Pushed 11y ago4 watchersCompare

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

READMEChangelogDependencies (6)Versions (8)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);
    }
}
```

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

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

Profits!

###  Health Score

33

—

LowBetter than 72% of packages

Maintenance16

Infrequent updates — may be unmaintained

Popularity25

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 67.6% 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 ~185 days

Total

4

Last Release

4106d ago

Major Versions

0.0.2 → 1.0.02014-12-15

### Community

Maintainers

![](https://www.gravatar.com/avatar/825f7193bec88275f660c65faab1790ed620948f5dd7a6c96515d1f12609127d?d=identicon)[mac\_nibblet](/maintainers/mac_nibblet)

---

Top Contributors

[![awartoft](https://avatars.githubusercontent.com/u/1127626?v=4)](https://github.com/awartoft "awartoft (23 commits)")[![Danielss89](https://avatars.githubusercontent.com/u/632956?v=4)](https://github.com/Danielss89 "Danielss89 (7 commits)")[![boukeversteegh](https://avatars.githubusercontent.com/u/53926?v=4)](https://github.com/boukeversteegh "boukeversteegh (4 commits)")

---

Tags

modulezf2elastic searchZend Framework

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

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

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

###  Alternatives

[wheelpros/fitment-platform-api

Magento 2 (Open Source)

12.1k1.2k](/packages/wheelpros-fitment-platform-api)[flow-php/flow

PHP ETL - Extract Transform Load - Data processing framework

84735.1k](/packages/flow-php-flow)

PHPackages © 2026

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