PHPackages                             tritoq/elastica-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. [Database &amp; ORM](/categories/database)
4. /
5. tritoq/elastica-bundle

ActiveSymfony-bundle[Database &amp; ORM](/categories/database)

tritoq/elastica-bundle
======================

Elasticsearch PHP integration for your Symfony2 project using Elastica

2.0.x-dev(13y ago)07MITPHPPHP &gt;=5.3.2

Since Mar 27Pushed 12y ago1 watchersCompare

[ Source](https://github.com/nezkal/FOQElasticaBundle)[ Packagist](https://packagist.org/packages/tritoq/elastica-bundle)[ Docs](https://github.com/Exercise/FOQElasticaBundle)[ RSS](/packages/tritoq-elastica-bundle/feed)WikiDiscussions master Synced 3d ago

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

**Note:** This bundle has moved to [FOSElasticaBundle](https://github.com/FriendsOfSymfony/FOSElasticaBundle) under the [FriendsOfSymfony](https://github.com/FriendsOfSymfony) organization.

---

[Elastica](https://github.com/ruflin/Elastica) integration in Symfony2

Installation
------------

[](#installation)

### Install elasticsearch

[](#install-elasticsearch)

### Install Elastica

[](#install-elastica)

#### Download

[](#download)

**With submodule**

`git submodule add git://github.com/ruflin/Elastica vendor/elastica`

**With clone**

`git clone git://github.com/ruflin/Elastica vendor/elastica`

**Using the vendors script**

Add the following lines to your deps file:

```
[Elastica]
    git=git://github.com/ruflin/Elastica.git
    target=elastica

```

#### Register autoloading

[](#register-autoloading)

```
// app/autoload.php

$loader->registerPrefixes(array(
    ...
    'Elastica' => __DIR__.'/../vendor/elastica/lib',
));

```

### Install ElasticaBundle

[](#install-elasticabundle)

Use the master branch with Symfony2 master only, use the 2.0 branch with Symfony2.0.x releases.

#### Download

[](#download-1)

**With submodule**

`git submodule add git://github.com/Exercise/FOQElasticaBundle vendor/bundles/FOQ/ElasticaBundle`

**With clone**

`git clone git://github.com/Exercise/FOQElasticaBundle vendor/bundles/FOQ/ElasticaBundle`

**With the vendors script**

Add the following lines to your deps file:

```
[FOQElasticaBundle]
    git=git://github.com/Exercise/FOQElasticaBundle.git
    target=bundles/FOQ/ElasticaBundle

```

For the 2.0 branch for use with Symfony2.0.x releases add the following:

```
[FOQElasticaBundle]
    git=git://github.com/Exercise/FOQElasticaBundle.git
    target=bundles/FOQ/ElasticaBundle
    version=origin/2.0

```

Run the vendors script:

```
$ php bin/vendors install
```

#### Register autoloading

[](#register-autoloading-1)

```
// app/autoload.php

$loader->registerNamespaces(array(
    ...
    'FOQ' => __DIR__.'/../vendor/bundles',
));

```

#### Register the bundle

[](#register-the-bundle)

```
// app/AppKernel.php

public function registerBundles()
{
    return array(
        // ...
        new FOQ\ElasticaBundle\FOQElasticaBundle(),
        // ...
    );
}

```

### Basic configuration

[](#basic-configuration)

#### Declare a client

[](#declare-a-client)

Elasticsearch client is comparable to a database connection. Most of the time, you will need only one.

```
#app/config/config.yml
foq_elastica:
    clients:
        default: { host: localhost, port: 9200 }

```

#### Declare an index

[](#declare-an-index)

Elasticsearch index is comparable to Doctrine entity manager. Most of the time, you will need only one.

```
foq_elastica:
    clients:
        default: { host: localhost, port: 9200 }
    indexes:
        website:
            client: default

```

Here we created a "website" index, that uses our "default" client.

Our index is now available as a service: `foq_elastica.index.website`. It is an instance of `Elastica_Index`.

If you need to have different index name from the service name, for example, in order to have different indexes for different environments then you can use the `index_name` key to change the index name. The service name will remain the same across the environments:

```
foq_elastica:
    clients:
        default: { host: localhost, port: 9200 }
    indexes:
        website:
            client: default
            index_name: website_qa

```

The service id will be `foq_elastica.index.website` but the underlying index name is website\_qa.

#### Declare a type

[](#declare-a-type)

Elasticsearch type is comparable to Doctrine entity repository.

```
foq_elastica:
    clients:
        default: { host: localhost, port: 9200 }
    indexes:
        website:
            client: default
            types:
                user:
                    mappings:
                        username: { boost: 5 }
                        firstName: { boost: 3 }
                        lastName: { boost: 3 }
                        aboutMe: ~

```

Our type is now available as a service: `foq_elastica.index.website.user`. It is an instance of `Elastica_Type`.

### Declaring parent field

[](#declaring-parent-field)

```
foq_elastica:
    clients:
        default: { host: localhost, port: 9200 }
    indexes:
        website:
            client: default
            types:
                comment:
                    mappings:
                        post: {_parent: { type: "post", identifier: "id" } }
                        date: { boost: 5 }
                        content: ~

```

### Declaring `nested` or `object`

[](#declaring-nested-or-object)

```
foq_elastica:
    clients:
        default: { host: localhost, port: 9200 }
    indexes:
        website:
            client: default
            types:
                post:
                    mappings:
                        date: { boost: 5 }
                        title: { boost: 3 }
                        content: ~
                        comments:
                            type: "nested"
                            properties:
                                date: { boost: 5 }
                                content: ~

```

### Populate the types

[](#populate-the-types)

```
php app/console foq:elastica:populate

```

This command deletes and creates the declared indexes and types. It applies the configured mappings to the types.

This command needs providers to insert new documents in the elasticsearch types. There are 2 ways to create providers. If your elasticsearch type matches a Doctrine repository or a Propel query, go for the persistence automatic provider. Or, for complete flexibility, go for manual provider.

#### Persistence automatic provider

[](#persistence-automatic-provider)

If we want to index the entities from a Doctrine repository or a Propel query, some configuration will let ElasticaBundle do it for us.

```
foq_elastica:
    clients:
        default: { host: localhost, port: 9200 }
    indexes:
        website:
            client: default
            types:
                user:
                    mappings:
                        username: { boost: 5 }
                        firstName: { boost: 3 }
                        # more mappings...
                    persistence:
                        driver: orm # orm, mongodb, propel are available
                        model: Application\UserBundle\Entity\User
                        provider: ~

```

Three drivers are actually supported: orm, mongodb, and propel.

##### Use a custom Doctrine query builder

[](#use-a-custom-doctrine-query-builder)

You can control which entities will be indexed by specifying a custom query builder method.

```
                    persistence:
                        driver: orm
                        model: Application\UserBundle\Entity\User
                        provider:
                            query_builder_method: createIsActiveQueryBuilder

```

Your repository must implement this method and return a Doctrine query builder.

> **Propel** doesn't support this feature yet.

##### Change the batch size

[](#change-the-batch-size)

By default, ElasticaBundle will index documents by packets of 100. You can change this value in the provider configuration.

```
                    persistence:
                        driver: orm
                        model: Application\UserBundle\Entity\User
                        provider:
                            batch_size: 100

```

##### Change the document identifier field

[](#change-the-document-identifier-field)

By default, ElasticaBundle will use the `id` field of your entities as the elasticsearch document identifier. You can change this value in the persistence configuration.

```
                    persistence:
                        driver: orm
                        model: Application\UserBundle\Entity\User
                        identifier: id

```

#### Manual provider

[](#manual-provider)

Create a service with the tag "foq\_elastica.provider" and attributes for the index and type for which the service will provide.

```

```

Its class must implement `FOQ\ElasticaBundle\Provider\ProviderInterface`.

```
