|
| 1 | +# Elasticsearch Support |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +Elasticsearch is a distributed, RESTful search and analytics engine capable of solving a growing number of use cases: |
| 6 | +application search, security analytics, metrics, logging, etc. |
| 7 | + |
| 8 | +API Platform comes natively with the **reading** support for Elasticsearch. It uses internally the official PHP client |
| 9 | +for Elasticsearch: [Elasticsearch-PHP](https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/index.html). |
| 10 | + |
| 11 | +Be careful, API Platform only supports Elasticsearch >= 6.5.0. |
| 12 | + |
| 13 | +## Enabling reading support |
| 14 | + |
| 15 | +To enable the reading support for Elasticsearch, simply require the Elasticsearch-PHP package using Composer: |
| 16 | + |
| 17 | + $ composer require elasticsearch/elasticsearch:^6.0 |
| 18 | + |
| 19 | +Then, enable it inside the API Platform configuration: |
| 20 | + |
| 21 | +```yaml |
| 22 | +# api/config/packages/api_platform.yaml |
| 23 | +parameters: |
| 24 | + # ... |
| 25 | + env(ELASTICSEARCH_HOST): 'http://localhost:9200' |
| 26 | + |
| 27 | +api_platform: |
| 28 | + # ... |
| 29 | + |
| 30 | + mapping: |
| 31 | + paths: ['%kernel.project_dir%/src/Model'] |
| 32 | + |
| 33 | + elasticsearch: |
| 34 | + hosts: ['%env(ELASTICSEARCH_HOST)%'] |
| 35 | + |
| 36 | + #... |
| 37 | +``` |
| 38 | + |
| 39 | +## Creating models |
| 40 | + |
| 41 | +First of all, API Platform follows the best practices of Elasticsearch: |
| 42 | +* a single index per resource should be used because Elasticsearch is going to [drop support for index types and will allow only a single type per |
| 43 | +index](https://www.elastic.co/guide/en/elasticsearch/reference/current/removal-of-types.html); |
| 44 | +* index name should be the short resource name in lower case; |
| 45 | +* the default `_doc` type should be used; |
| 46 | +* all fields should be lower case and should use snake case for combining words. |
| 47 | + |
| 48 | +This involves having mappings and models which absolutely match each other. |
| 49 | + |
| 50 | +Here is an example of mappings for 2 resources, `User` and `Tweet`, and their models: |
| 51 | + |
| 52 | +```json |
| 53 | +PUT user |
| 54 | +{ |
| 55 | + "mappings": { |
| 56 | + "_doc": { |
| 57 | + "properties": { |
| 58 | + "id": { |
| 59 | + "type": "keyword" |
| 60 | + }, |
| 61 | + "gender": { |
| 62 | + "type": "keyword" |
| 63 | + }, |
| 64 | + "age": { |
| 65 | + "type": "integer" |
| 66 | + }, |
| 67 | + "first_name": { |
| 68 | + "type": "text" |
| 69 | + }, |
| 70 | + "last_name": { |
| 71 | + "type": "text" |
| 72 | + }, |
| 73 | + "tweets": { |
| 74 | + "type": "nested", |
| 75 | + "properties": { |
| 76 | + "id": { |
| 77 | + "type": "keyword" |
| 78 | + }, |
| 79 | + "date": { |
| 80 | + "type": "date", |
| 81 | + "format": "yyyy-MM-dd HH:mm:ss" |
| 82 | + }, |
| 83 | + "message": { |
| 84 | + "type": "text" |
| 85 | + } |
| 86 | + }, |
| 87 | + "dynamic": "strict" |
| 88 | + } |
| 89 | + }, |
| 90 | + "dynamic": "strict" |
| 91 | + } |
| 92 | + } |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +```json |
| 97 | +PUT tweet |
| 98 | +{ |
| 99 | + "mappings": { |
| 100 | + "_doc": { |
| 101 | + "properties": { |
| 102 | + "id": { |
| 103 | + "type": "keyword" |
| 104 | + }, |
| 105 | + "author": { |
| 106 | + "properties": { |
| 107 | + "id": { |
| 108 | + "type": "keyword" |
| 109 | + }, |
| 110 | + "gender": { |
| 111 | + "type": "keyword" |
| 112 | + }, |
| 113 | + "age": { |
| 114 | + "type": "integer" |
| 115 | + }, |
| 116 | + "first_name": { |
| 117 | + "type": "text" |
| 118 | + }, |
| 119 | + "last_name": { |
| 120 | + "type": "text" |
| 121 | + } |
| 122 | + }, |
| 123 | + "dynamic": "strict" |
| 124 | + }, |
| 125 | + "date": { |
| 126 | + "type": "date", |
| 127 | + "format": "yyyy-MM-dd HH:mm:ss" |
| 128 | + }, |
| 129 | + "message": { |
| 130 | + "type": "text" |
| 131 | + } |
| 132 | + }, |
| 133 | + "dynamic": "strict" |
| 134 | + } |
| 135 | + } |
| 136 | +} |
| 137 | +``` |
| 138 | + |
| 139 | +```php |
| 140 | +<?php |
| 141 | +// api/src/Model/User.php |
| 142 | + |
| 143 | +namespace App\Model; |
| 144 | + |
| 145 | +use ApiPlatform\Core\Annotation\ApiProperty; |
| 146 | +use ApiPlatform\Core\Annotation\ApiResource; |
| 147 | + |
| 148 | +/** |
| 149 | + * @ApiResource |
| 150 | + */ |
| 151 | +class User |
| 152 | +{ |
| 153 | + /** |
| 154 | + * @ApiProperty(identifier=true) |
| 155 | + * |
| 156 | + * @var string |
| 157 | + */ |
| 158 | + public $id; |
| 159 | + |
| 160 | + /** |
| 161 | + * @var string |
| 162 | + */ |
| 163 | + public $gender; |
| 164 | + |
| 165 | + /** |
| 166 | + * @var int |
| 167 | + */ |
| 168 | + public $age; |
| 169 | + |
| 170 | + /** |
| 171 | + * @var string |
| 172 | + */ |
| 173 | + public $firstName; |
| 174 | + |
| 175 | + /** |
| 176 | + * @var string |
| 177 | + */ |
| 178 | + public $lastName; |
| 179 | + |
| 180 | + /** |
| 181 | + * @var Tweet[] |
| 182 | + */ |
| 183 | + public $tweets = []; |
| 184 | +} |
| 185 | +``` |
| 186 | + |
| 187 | +```php |
| 188 | +<?php |
| 189 | +// api/src/Model/Tweet.php |
| 190 | + |
| 191 | +namespace App\Model; |
| 192 | + |
| 193 | +use ApiPlatform\Core\Annotation\ApiProperty; |
| 194 | +use ApiPlatform\Core\Annotation\ApiResource; |
| 195 | + |
| 196 | +/** |
| 197 | + * @ApiResource |
| 198 | + */ |
| 199 | +class Tweet |
| 200 | +{ |
| 201 | + /** |
| 202 | + * @ApiProperty(identifier=true) |
| 203 | + * |
| 204 | + * @var string |
| 205 | + */ |
| 206 | + public $id; |
| 207 | + |
| 208 | + /** |
| 209 | + * @var User |
| 210 | + */ |
| 211 | + public $author; |
| 212 | + |
| 213 | + /** |
| 214 | + * @var \DateTimeInterface |
| 215 | + */ |
| 216 | + public $date; |
| 217 | + |
| 218 | + /** |
| 219 | + * @var string |
| 220 | + */ |
| 221 | + public $message; |
| 222 | +} |
| 223 | +``` |
| 224 | + |
| 225 | +API Platform will automatically disable write operations and snake case document fields will automatically be converted to |
| 226 | +camel case object properties during serialization. |
| 227 | + |
| 228 | +Keep in mind that it is your responsibility to populate your Elasticsearch index. To do so, you can use [Logstash](https://www.elastic.co/products/logstash), |
| 229 | +a custom [data persister](data-persisters.md#creating-a-custom-data-persister) or any other mechanism that fits for your |
| 230 | +project (such as an [ETL](https://en.wikipedia.org/wiki/Extract,_transform,_load)). |
| 231 | + |
| 232 | +You're done! The API is now ready to use. |
| 233 | + |
| 234 | +### Creating custom mapping |
| 235 | + |
| 236 | +If you don't follow the Elasticsearch recommendations, you may want a custom mapping between API Platform resources and |
| 237 | +Elasticsearch indexes/types. |
| 238 | + |
| 239 | +For example, consider an index being similar to a database in an SQL database and a type being equivalent to a table. |
| 240 | +So the `User` and `Tweet` resources of the previous example would become `user` and `tweet` types in an index named `app`: |
| 241 | + |
| 242 | +```yaml |
| 243 | +# api/config/packages/api_platform.yaml |
| 244 | +parameters: |
| 245 | + # ... |
| 246 | + env(ELASTICSEARCH_HOST): 'http://localhost:9200' |
| 247 | + |
| 248 | +api_platform: |
| 249 | + # ... |
| 250 | + |
| 251 | + mapping: |
| 252 | + paths: ['%kernel.project_dir%/src/Model'] |
| 253 | + |
| 254 | + elasticsearch: |
| 255 | + hosts: ['%env(ELASTICSEARCH_HOST)%'] |
| 256 | + mapping: |
| 257 | + App\Model\User: |
| 258 | + index: app |
| 259 | + type: user |
| 260 | + App\Model\Tweet: |
| 261 | + index: app |
| 262 | + type: tweet |
| 263 | + |
| 264 | + #... |
| 265 | +``` |
| 266 | + |
| 267 | +## Filtering |
| 268 | + |
| 269 | +See how to use Elasticsearch filters and how to create Elasticsearch custom filters in [the Filters chapter](filters.md). |
| 270 | + |
| 271 | +## Creating custom extensions |
| 272 | + |
| 273 | +See how to create Elasticsearch custom extensions in [the Extensions chapter](extensions.md). |
0 commit comments