Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:
$ composer require fazland/elastica-bundle
This command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation.
Instructions for installing and deploying Elasticsearch may be found here.
Then, enable the bundle by adding the following line in the app/AppKernel.php
file of your project:
<?php
// app/AppKernel.php
// ...
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
// ...
new Fazland\ElasticaBundle\FazlandElasticaBundle(),
);
// ...
}
}
The basic minimal configuration for FazlandElasticaBundle is one client with one Elasticsearch index. In almost all cases, an application will only need a single index. An index can be considered comparable to a Doctrine Entity Manager, where the index will hold multiple type definitions.
#app/config/config.yml
fazland_elastica:
clients:
default: { host: localhost, port: 9200 }
indexes:
app: ~
In this example, an Elastica index (an instance of Elastica\Index
) is available as a
service with the key fazland_elastica.index.app
.
You may want the index app
to be named something else on ElasticSearch depending on
if your application is running in a different env or other conditions that suit your
application. To set your customer index to a name that depends on the environment of your
Symfony application, use the example below:
#app/config/config.yml
fazland_elastica:
indexes:
app:
index_name: app_%kernel.environment%
In this case, the service fazland_elastica.index.app
will relate to an ElasticSearch index
that varies depending on your kernel's environment. For example, in dev it will relate to
app_dev
.
By default, FazlandElasticaBundle requires each type that is to be indexed to be mapped. It is possible to use a serializer to avoid this requirement. To use a serializer, see the serializer documentation
An Elasticsearch type needs to be defined with each field of a related PHP object that will end up being indexed.
fazland_elastica:
indexes:
app:
types:
user:
properties:
username: ~
firstName: ~
lastName: ~
email: ~
Each defined type is made available as a service, and in this case the service key is
fazland_elastica.index.app.user
and is an instance of Elastica\Type
.
FazlandElasticaBundle requires a provider for each type that will notify when an object that maps to a type has been modified. The bundle ships with support for Doctrine and Propel objects.
Below is an example for the Doctrine ORM.
user:
properties:
username: ~
firstName: ~
lastName: ~
email: ~
persistence:
# the driver can be orm, mongodb, phpcr or propel
# listener and finder are not supported by
# propel and should be removed
driver: orm
model: Acme\ApplicationBundle\Entity\User
provider: ~
listener: ~
finder: ~
There are a significant number of options available for types, that can be found here
When using the providers and listeners that come with the bundle, any new or modified object will be indexed automatically. In some cases, where the database is modified externally, the Elasticsearch index must be updated manually. This can be achieved by running the console command:
$ php app/console fazland:elastica:populate
The command will also create all indexes and types defined if they do not already exist on the Elasticsearch server.
Usage documentation for the bundle is available here