Skip to content

Commit 3d634a5

Browse files
committed
feat(collections): lazy load all collections in EntityFactory
Defer collection hydration until first access to reduce unnecessary work during entity creation. ElasticCollection now uses setElasticIds() to leverage existing bulk Terms query initialization. EntityCollection uses closure-based lazy init via setInitializer()/ensureInitialized(), designed for easy migration to PHP 8.4 native lazy objects.
1 parent e26c63c commit 3d634a5

12 files changed

+5540
-31
lines changed

doc/18_mapping_attributes.md

Lines changed: 1106 additions & 0 deletions
Large diffs are not rendered by default.

doc/19_event_system.md

Lines changed: 1081 additions & 0 deletions
Large diffs are not rendered by default.

doc/20_sti_guide.md

Lines changed: 487 additions & 0 deletions
Large diffs are not rendered by default.

doc/21_identity_map.md

Lines changed: 483 additions & 0 deletions
Large diffs are not rendered by default.

doc/22_value_objects.md

Lines changed: 617 additions & 0 deletions
Large diffs are not rendered by default.

doc/23_console_commands.md

Lines changed: 360 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,360 @@
1+
# Console Commands Reference
2+
3+
This document provides a complete reference for all CLI commands available in Spameri/Elastic.
4+
5+
## Prerequisites
6+
7+
Console commands require a Symfony Console implementation. Add to your configuration:
8+
9+
```neon
10+
extensions:
11+
console: Contributte\Console\DI\ConsoleExtension
12+
```
13+
14+
---
15+
16+
## Index Management Commands
17+
18+
### spameri:elastic:initialize-index
19+
20+
Creates indexes and initializes them with settings and mappings from your `IndexConfigInterface` implementations.
21+
22+
```bash
23+
php bin/console spameri:elastic:initialize-index [options] [--] [<entityName>...]
24+
```
25+
26+
**Arguments:**
27+
28+
| Argument | Description |
29+
|----------|-------------|
30+
| `entityName` | Optional. One or more index names to initialize. If omitted, all configured indexes are initialized. |
31+
32+
**Options:**
33+
34+
| Option | Short | Description |
35+
|--------|-------|-------------|
36+
| `--force` | `-f` | Delete existing index before creating. **Warning: This deletes all data!** |
37+
38+
**Examples:**
39+
40+
```bash
41+
# Initialize all indexes
42+
php bin/console spameri:elastic:initialize-index
43+
44+
# Initialize specific index
45+
php bin/console spameri:elastic:initialize-index video
46+
47+
# Initialize multiple indexes
48+
php bin/console spameri:elastic:initialize-index video person
49+
50+
# Force recreate (delete and create fresh)
51+
php bin/console spameri:elastic:initialize-index -f video
52+
```
53+
54+
**Behavior:**
55+
56+
1. Locates `IndexConfigInterface` implementations for specified indexes
57+
2. If `-f` flag: deletes existing index first
58+
3. Creates index with mappings from `provide()` method
59+
4. Reports success or failure for each index
60+
61+
---
62+
63+
### spameri:elastic:create-index
64+
65+
Creates a single index with its mapping configuration.
66+
67+
```bash
68+
php bin/console spameri:elastic:create-index <indexName>
69+
```
70+
71+
**Arguments:**
72+
73+
| Argument | Description |
74+
|----------|-------------|
75+
| `indexName` | **Required.** Name of the index to create. |
76+
77+
**Example:**
78+
79+
```bash
80+
php bin/console spameri:elastic:create-index video
81+
```
82+
83+
**Notes:**
84+
85+
- Fails if index already exists (use `initialize-indexes -f` to recreate)
86+
- Requires corresponding `IndexConfigInterface` implementation registered in DI
87+
88+
---
89+
90+
### spameri:elastic:delete-index
91+
92+
Deletes an index and all its data.
93+
94+
```bash
95+
php bin/console spameri:elastic:delete-index <indexName>
96+
```
97+
98+
**Arguments:**
99+
100+
| Argument | Description |
101+
|----------|-------------|
102+
| `indexName` | **Required.** Name of the index to delete. |
103+
104+
**Example:**
105+
106+
```bash
107+
php bin/console spameri:elastic:delete-index video
108+
```
109+
110+
**Warning:** This permanently deletes all data in the index. There is no undo.
111+
112+
---
113+
114+
## Data Migration Commands
115+
116+
### spameri:elastic:dump-index
117+
118+
Exports all documents from an index to a JSON file for backup or migration.
119+
120+
```bash
121+
php bin/console spameri:elastic:dump-index <indexName>
122+
```
123+
124+
**Arguments:**
125+
126+
| Argument | Description |
127+
|----------|-------------|
128+
| `indexName` | **Required.** Name of the index to dump. |
129+
130+
**Output:**
131+
132+
Creates a file named `<indexName>_dump.json` in the current directory containing all documents in bulk format.
133+
134+
**Example:**
135+
136+
```bash
137+
php bin/console spameri:elastic:dump-index video
138+
# Creates: video_dump.json
139+
```
140+
141+
**Use Cases:**
142+
143+
- Backup before mapping changes
144+
- Migrate data between environments
145+
- Archive index data
146+
147+
---
148+
149+
### spameri:elastic:load-dump
150+
151+
Imports documents from a dump file back into ElasticSearch.
152+
153+
```bash
154+
php bin/console spameri:elastic:load-dump <filePath> [<step>]
155+
```
156+
157+
**Arguments:**
158+
159+
| Argument | Description |
160+
|----------|-------------|
161+
| `filePath` | **Required.** Path to the dump JSON file. |
162+
| `step` | Optional. Batch size for bulk operations. Default: 500. |
163+
164+
**Examples:**
165+
166+
```bash
167+
# Load with default batch size
168+
php bin/console spameri:elastic:load-dump video_dump.json
169+
170+
# Load with smaller batches (for large documents)
171+
php bin/console spameri:elastic:load-dump video_dump.json 100
172+
173+
# Load with larger batches (faster for small documents)
174+
php bin/console spameri:elastic:load-dump video_dump.json 1000
175+
```
176+
177+
**Notes:**
178+
179+
- Index must exist before loading
180+
- Large datasets benefit from adjusted step size
181+
- Progress is displayed during import
182+
183+
---
184+
185+
## Alias Management Commands
186+
187+
### spameri:elastic:add-alias
188+
189+
Adds an alias to an index for zero-downtime reindexing and environment abstraction.
190+
191+
```bash
192+
php bin/console spameri:elastic:add-alias <indexName> <aliasName>
193+
```
194+
195+
**Arguments:**
196+
197+
| Argument | Description |
198+
|----------|-------------|
199+
| `indexName` | **Required.** Name of the index. |
200+
| `aliasName` | **Required.** Alias to add. |
201+
202+
**Example:**
203+
204+
```bash
205+
php bin/console spameri:elastic:add-alias video_v2 video
206+
```
207+
208+
**Use Cases:**
209+
210+
- Zero-downtime reindexing: create new index, add alias, remove old
211+
- Environment abstraction: use alias in code, point to versioned indexes
212+
- A/B testing: switch alias between index versions
213+
214+
---
215+
216+
### spameri:elastic:remove-alias
217+
218+
Removes an alias from an index.
219+
220+
```bash
221+
php bin/console spameri:elastic:remove-alias <indexName> <aliasName>
222+
```
223+
224+
**Arguments:**
225+
226+
| Argument | Description |
227+
|----------|-------------|
228+
| `indexName` | **Required.** Name of the index. |
229+
| `aliasName` | **Required.** Alias to remove. |
230+
231+
**Example:**
232+
233+
```bash
234+
php bin/console spameri:elastic:remove-alias video_v1 video
235+
```
236+
237+
---
238+
239+
## Common Workflows
240+
241+
### Initial Setup
242+
243+
```bash
244+
# 1. Create all indexes from configuration
245+
php bin/console spameri:elastic:initialize-index
246+
```
247+
248+
### Development Reset
249+
250+
```bash
251+
# Force recreate all indexes (deletes data)
252+
php bin/console spameri:elastic:initialize-index -f
253+
```
254+
255+
### Mapping Migration (Preserving Data)
256+
257+
```bash
258+
# 1. Backup existing data
259+
php bin/console spameri:elastic:dump-index video
260+
261+
# 2. Delete and recreate with new mapping
262+
php bin/console spameri:elastic:initialize-index -f video
263+
264+
# 3. Restore data
265+
php bin/console spameri:elastic:load-dump video_dump.json
266+
```
267+
268+
### Zero-Downtime Reindexing
269+
270+
```bash
271+
# 1. Create new index version
272+
php bin/console spameri:elastic:create-index video_v2
273+
274+
# 2. Import data to new index (via your import system)
275+
276+
# 3. Switch alias
277+
php bin/console spameri:elastic:remove-alias video_v1 video
278+
php bin/console spameri:elastic:add-alias video_v2 video
279+
280+
# 4. Delete old index when ready
281+
php bin/console spameri:elastic:delete-index video_v1
282+
```
283+
284+
---
285+
286+
## Quick Reference
287+
288+
| Command | Description |
289+
|---------|-------------|
290+
| `spameri:elastic:initialize-index` | Create/recreate indexes from config |
291+
| `spameri:elastic:create-index` | Create single index |
292+
| `spameri:elastic:delete-index` | Delete index and data |
293+
| `spameri:elastic:dump-index` | Export index to JSON |
294+
| `spameri:elastic:load-dump` | Import from JSON dump |
295+
| `spameri:elastic:add-alias` | Add alias to index |
296+
| `spameri:elastic:remove-alias` | Remove alias from index |
297+
298+
---
299+
300+
## Troubleshooting
301+
302+
### Index already exists
303+
304+
```
305+
Error: Index video already exists
306+
```
307+
308+
**Solution:** Use `-f` flag to force recreate, or delete manually first:
309+
310+
```bash
311+
php bin/console spameri:elastic:delete-index video
312+
php bin/console spameri:elastic:create-index video
313+
```
314+
315+
### No mapping found
316+
317+
```
318+
Error: No IndexConfigInterface found for index: video
319+
```
320+
321+
**Solution:** Ensure you have an `IndexConfigInterface` implementation registered:
322+
323+
```php
324+
class VideoMapping implements \Spameri\Elastic\Settings\IndexConfigInterface
325+
{
326+
public function indexName(): string
327+
{
328+
return 'video';
329+
}
330+
// ...
331+
}
332+
```
333+
334+
```neon
335+
services:
336+
- App\Model\Settings\VideoMapping
337+
```
338+
339+
### Connection refused
340+
341+
```
342+
Error: Connection refused [tcp://127.0.0.1:9200]
343+
```
344+
345+
**Solution:** Check ElasticSearch is running and configuration is correct:
346+
347+
```neon
348+
spameriElasticSearch:
349+
host: 127.0.0.1
350+
port: 9200
351+
```
352+
353+
---
354+
355+
## Next Steps
356+
357+
- [Configuration](02_configuration.md) - DI extension setup
358+
- [Index Mapping](05_new_index_with_mapping.md) - Creating index configurations
359+
- [Migrations](24_migrations.md) - Schema and data migrations
360+
- [Import System](15_import_system.md) - Bulk data imports

0 commit comments

Comments
 (0)