PHPackages                             catch-of-the-day/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. catch-of-the-day/elastica-bundle

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

catch-of-the-day/elastica-bundle
================================

Elasticsearch PHP integration for your Symfony2 project using Elastica

v2.1.3(12y ago)0280MITPHPPHP &gt;=5.3.2

Since Mar 27Pushed 12y ago28 watchersCompare

[ Source](https://github.com/CatchoftheDay/FOSElasticaBundle)[ Packagist](https://packagist.org/packages/catch-of-the-day/elastica-bundle)[ Docs](https://github.com/CatchoftheDay/FOSElasticaBundle)[ RSS](/packages/catch-of-the-day-elastica-bundle/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependencies (10)Versions (8)Used By (0)

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

### Installation

[](#installation)

#### Bundle and Dependencies

[](#bundle-and-dependencies)

For Symfony 2.0.x projects, you must use a 1.x release of this bundle. Please check the bundle [tags](https://github.com/FriendsOfSymfony/FOSElasticaBundle/tags) or the [Packagist](https://packagist.org/packages/friendsofsymfony/elastica-bundle)page for information on Symfony and Elastica compatibility.

Add FOSElasticaBundle to your application's `composer.json` file:

```
{
    "require": {
        "friendsofsymfony/elastica-bundle": "~2.0"
    }
}
```

Install the bundle and its dependencies with the following command:

```
$ php composer.phar update friendsofsymfony/elastica-bundle
```

You may rely on Composer to fetch the appropriate version of Elastica. Lastly, enable the bundle in your application kernel:

```
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new FOS\ElasticaBundle\FOSElasticaBundle(),
    );
}
```

#### Elasticsearch

[](#elasticsearch)

Instructions for installing and deploying Elasticsearch may be found [here](http://www.elasticsearch.org/guide/reference/setup/installation/).

### 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
fos_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.

```
fos_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: `fos_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:

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

```

The service id will be `fos_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.

```
fos_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: `fos_elastica.index.website.user`. It is an instance of `Elastica_Type`.

### Declaring parent field

[](#declaring-parent-field)

```
fos_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)

```
fos_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 fos: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.

```
fos_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 "fos\_elastica.provider" and attributes for the index and type for which the service will provide.

```

```

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

```
