2013-12-17 11:09:58 +01:00
|
|
|
Step 1: Setting up the bundle
|
|
|
|
=============================
|
|
|
|
|
2014-09-25 10:52:51 +02:00
|
|
|
A: Download the Bundle
|
|
|
|
----------------------
|
2013-12-17 11:09:58 +01:00
|
|
|
|
2014-09-25 10:52:51 +02:00
|
|
|
Open a command console, enter your project directory and execute the
|
|
|
|
following command to download the latest stable version of this bundle:
|
2013-12-17 11:09:58 +01:00
|
|
|
|
|
|
|
```bash
|
2014-09-25 10:52:51 +02:00
|
|
|
$ composer require friendsofsymfony/elastica-bundle "~3.0"
|
2013-12-17 11:09:58 +01:00
|
|
|
```
|
|
|
|
|
2014-09-25 10:52:51 +02:00
|
|
|
This command requires you to have Composer installed globally, as explained
|
|
|
|
in the [installation chapter](https://getcomposer.org/doc/00-intro.md)
|
|
|
|
of the Composer documentation.
|
2013-12-17 11:09:58 +01:00
|
|
|
|
2014-09-25 10:52:51 +02:00
|
|
|
### Elasticsearch
|
2013-12-17 11:09:58 +01:00
|
|
|
|
2014-09-25 10:52:51 +02:00
|
|
|
Instructions for installing and deploying Elasticsearch may be found [here](http://www.elasticsearch.org/guide/reference/setup/installation/).
|
2013-12-17 11:09:58 +01:00
|
|
|
|
2014-09-25 10:52:51 +02:00
|
|
|
Step 2: Enable the Bundle
|
|
|
|
-------------------------
|
2013-12-17 11:09:58 +01:00
|
|
|
|
2014-09-25 10:52:51 +02:00
|
|
|
Then, enable the bundle by adding the following line in the `app/AppKernel.php`
|
|
|
|
file of your project:
|
2013-12-17 11:09:58 +01:00
|
|
|
|
|
|
|
```php
|
|
|
|
<?php
|
|
|
|
// app/AppKernel.php
|
|
|
|
|
2014-09-25 10:52:51 +02:00
|
|
|
// ...
|
|
|
|
class AppKernel extends Kernel
|
2013-12-17 11:09:58 +01:00
|
|
|
{
|
2014-09-25 10:52:51 +02:00
|
|
|
public function registerBundles()
|
|
|
|
{
|
|
|
|
$bundles = array(
|
|
|
|
// ...
|
|
|
|
new FOS\ElasticaBundle\FOSElasticaBundle(),
|
|
|
|
);
|
|
|
|
|
2013-12-17 11:09:58 +01:00
|
|
|
// ...
|
2014-09-25 10:52:51 +02:00
|
|
|
}
|
2013-12-17 11:09:58 +01:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2014-09-25 10:52:51 +02:00
|
|
|
C: Basic Bundle Configuration
|
2013-12-17 11:09:58 +01:00
|
|
|
-----------------------------
|
|
|
|
|
|
|
|
The basic minimal configuration for FOSElasticaBundle is one client with one Elasticsearch
|
2014-03-26 00:07:50 +01:00
|
|
|
index. In almost all cases, an application will only need a single index. An index can
|
2013-12-17 11:09:58 +01:00
|
|
|
be considered comparable to a Doctrine Entity Manager, where the index will hold multiple
|
|
|
|
type definitions.
|
|
|
|
|
|
|
|
```yaml
|
|
|
|
#app/config/config.yml
|
|
|
|
fos_elastica:
|
|
|
|
clients:
|
|
|
|
default: { host: localhost, port: 9200 }
|
|
|
|
indexes:
|
2015-01-21 23:10:46 +01:00
|
|
|
app: ~
|
2013-12-17 11:09:58 +01:00
|
|
|
```
|
|
|
|
|
2014-03-26 00:07:50 +01:00
|
|
|
In this example, an Elastica index (an instance of `Elastica\Index`) is available as a
|
2015-01-21 23:10:46 +01:00
|
|
|
service with the key `fos_elastica.index.app`.
|
2013-12-17 11:09:58 +01:00
|
|
|
|
2015-01-21 23:10:46 +01:00
|
|
|
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:
|
2013-12-17 11:09:58 +01:00
|
|
|
|
|
|
|
```yaml
|
|
|
|
#app/config/config.yml
|
|
|
|
fos_elastica:
|
|
|
|
indexes:
|
2015-01-21 23:10:46 +01:00
|
|
|
app:
|
|
|
|
index_name: app_%kernel.env%
|
2013-12-17 11:09:58 +01:00
|
|
|
```
|
|
|
|
|
2015-01-21 23:10:46 +01:00
|
|
|
In this case, the service `fos_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`.
|
2013-12-17 11:09:58 +01:00
|
|
|
|
2014-09-25 10:52:51 +02:00
|
|
|
D: Defining index types
|
2013-12-17 11:09:58 +01:00
|
|
|
-----------------------
|
|
|
|
|
2014-03-26 00:07:50 +01:00
|
|
|
By default, FOSElasticaBundle requires each type that is to be indexed to be mapped.
|
2013-12-17 11:09:58 +01:00
|
|
|
It is possible to use a serializer to avoid this requirement. To use a serializer, see
|
|
|
|
the [serializer documentation](serializer.md)
|
|
|
|
|
|
|
|
An Elasticsearch type needs to be defined with each field of a related PHP object that
|
|
|
|
will end up being indexed.
|
|
|
|
|
|
|
|
```yaml
|
|
|
|
fos_elastica:
|
|
|
|
indexes:
|
2015-01-21 23:10:46 +01:00
|
|
|
app:
|
2013-12-17 11:09:58 +01:00
|
|
|
types:
|
|
|
|
user:
|
|
|
|
mappings:
|
|
|
|
username: ~
|
|
|
|
firstName: ~
|
|
|
|
lastName: ~
|
|
|
|
email: ~
|
|
|
|
```
|
|
|
|
|
|
|
|
Each defined type is made available as a service, and in this case the service key is
|
2015-01-21 23:10:46 +01:00
|
|
|
`fos_elastica.index.app.user` and is an instance of `Elastica\Type`.
|
2013-12-17 11:09:58 +01:00
|
|
|
|
|
|
|
FOSElasticaBundle requires a provider for each type that will notify when an object
|
2014-03-26 00:07:50 +01:00
|
|
|
that maps to a type has been modified. The bundle ships with support for Doctrine and
|
2013-12-17 11:09:58 +01:00
|
|
|
Propel objects.
|
|
|
|
|
2014-03-26 00:07:50 +01:00
|
|
|
Below is an example for the Doctrine ORM.
|
2013-12-17 11:09:58 +01:00
|
|
|
|
|
|
|
```yaml
|
|
|
|
user:
|
|
|
|
mappings:
|
|
|
|
username: ~
|
|
|
|
firstName: ~
|
|
|
|
lastName: ~
|
|
|
|
email: ~
|
|
|
|
persistence:
|
|
|
|
# the driver can be orm, mongodb or propel
|
|
|
|
# listener and finder are not supported by
|
|
|
|
# propel and should be removed
|
|
|
|
driver: orm
|
|
|
|
model: Acme\ApplicationBundle\Entity\User
|
|
|
|
provider: ~
|
2014-05-24 12:49:12 +02:00
|
|
|
listener:
|
|
|
|
immediate: ~
|
2013-12-17 11:09:58 +01:00
|
|
|
finder: ~
|
|
|
|
```
|
|
|
|
|
|
|
|
There are a significant number of options available for types, that can be
|
|
|
|
[found here](types.md)
|
|
|
|
|
2014-09-25 10:52:51 +02:00
|
|
|
E: Populating the Elasticsearch index
|
2013-12-17 11:09:58 +01:00
|
|
|
-------------------------------------
|
|
|
|
|
|
|
|
When using the providers and listeners that come with the bundle, any new or modified
|
2014-03-26 00:07:50 +01:00
|
|
|
object will be indexed automatically. In some cases, where the database is modified
|
2013-12-17 11:09:58 +01:00
|
|
|
externally, the Elasticsearch index must be updated manually. This can be achieved by
|
|
|
|
running the console command:
|
|
|
|
|
|
|
|
```bash
|
|
|
|
$ php app/console fos:elastica:populate
|
|
|
|
```
|
|
|
|
|
|
|
|
The command will also create all indexes and types defined if they do not already exist
|
|
|
|
on the Elasticsearch server.
|
|
|
|
|
2014-09-25 10:52:51 +02:00
|
|
|
F: Usage
|
2013-12-17 11:09:58 +01:00
|
|
|
--------
|
|
|
|
|
|
|
|
Usage documentation for the bundle is available [here](usage.md)
|